赞
踩
邮件发送功能学习实践笔记
关于pop、imap、smtp的知识可自行百度了解
此文为smtp邮件发送的工具类总结,其中包含两种方法
1、未开启安全机制的默认端口发送邮件,单个邮箱发送、多个邮箱发送(包含两种传参形式)。
2、开启ssl安全机制更改端口的邮件发送。
这两种方式第一种适用于没有端口限制的环境,第二种适用于像阿里云服务器等限制端口的环境。
关于每个邮箱平台查看授权码的方式可百度
这里以126邮箱为例
登陆后—>设置—>POP3/SMTP/IMAP
在此界面可开启/关闭smtp服务以及获取授权码
选择新增授权密码,通过验证即可获取授权码
后面代码中配置需要用到授权码
所需依赖:
<!-- https://mvnrepository.com/artifact/com.sun.mail/javax.mail -->
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
package com.smtp.email; import javax.mail.*; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.util.List; import java.util.Properties; /** * @Author:李白 * @Date:2023/1/8 12:57 */ public class SmtpEmailUtils { /** * 1发送邮件(单条发送纯文本邮件) * @param mailSend 发件人 * @param mailSendAuthCode 发件人邮箱授权码 * @param mailReceive 收件人 * @param subject 邮件主题 * @param content 邮件内容 * @throws AddressException */ public static void sendMail(String mailSend, String mailSendAuthCode, String mailReceive, String subject, String content) throws MessagingException { //配置发送邮件的属性 Properties properties = new Properties(); //是否需要身份验证 properties.put("mail.smtp.auth","true"); //服务器地址 // properties.put("mail.smtp.host","smtp.qq.com"); properties.put("mail.smtp.host","smtp.126.com"); //登陆账号密码,需开启smtp // properties.put("mail.user","发件人邮箱"); properties.put("mail.user",mailSend); //smtp提供的授权码(非密码) // properties.put("mail.password","发件人邮箱授权码");//qq // properties.put("mail.password","发件人邮箱授权码");//126 properties.put("mail.password",mailSendAuthCode); //构建授权信息,用于进行SMTP身份验证 Authenticator authenticator = new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { //用户名,密码 String userName = properties.getProperty("mail.user"); String password = properties.getProperty("mail.password"); return new PasswordAuthentication(userName, password); } }; //使用环境属性和授权信息,创建邮件会话 Session session = Session.getInstance(properties, authenticator); //创建邮件消息 MimeMessage mimeMessage = new MimeMessage(session); //设置发件人 InternetAddress from = new InternetAddress(properties.getProperty("mail.user")); mimeMessage.setFrom(from); //设置收件人 InternetAddress to = new InternetAddress(mailReceive); mimeMessage.setRecipient(Message.RecipientType.TO,to); //设置抄送 // InternetAddress cc = new InternetAddress("ssss@yahoo.com"); // mimeMessage.setRecipient(Message.RecipientType.CC,cc); //设置秘密抄送,其他收件人不能看到密送的邮件地址 // InternetAddress bcc = new InternetAddress("yyyy@163.com"); // mimeMessage.setRecipient(Message.RecipientType.BCC,bcc); //设置邮件标题 mimeMessage.setSubject(subject); //邮件内容 mimeMessage.setContent(content,"text/html;charset = UTF-8"); //发送邮件 Transport.send(mimeMessage); } /** * 2发送邮件(群发1) * @param mailSend 发件人 * @param mailSendAuthCode 发件人邮箱授权码 * @param mailReceiveList 收件人列表(多个邮箱) * @param subject 邮件主题 * @param content 邮件内容 * @throws AddressException */ public static void sendMails(String mailSend, String mailSendAuthCode, List mailReceiveList, String subject, String content) throws MessagingException { //配置发送邮件的属性 Properties properties = new Properties(); //是否需要身份验证 properties.put("mail.smtp.auth","true"); //服务器地址 // properties.put("mail.smtp.host","smtp.qq.com"); properties.put("mail.smtp.host","smtp.126.com"); //登陆账号密码,需开启smtp // properties.put("mail.user","发件人邮箱"); properties.put("mail.user",mailSend); //smtp提供的授权码(非密码) // properties.put("mail.password","发件人邮箱授权码");//qq // properties.put("mail.password","发件人邮箱授权码");//126 properties.put("mail.password",mailSendAuthCode); //构建授权信息,用于进行SMTP身份验证 Authenticator authenticator = new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { //用户名,密码 String userName = properties.getProperty("mail.user"); String password = properties.getProperty("mail.password"); return new PasswordAuthentication(userName, password); } }; //使用环境属性和授权信息,创建邮件会话 Session session = Session.getInstance(properties, authenticator); //创建邮件消息 MimeMessage mimeMessage = new MimeMessage(session); //设置发件人 InternetAddress from = new InternetAddress(properties.getProperty("mail.user")); mimeMessage.setFrom(from); //设置收件人(多人收--群发) // InternetAddress to = new InternetAddress(mailReceive); // mimeMessage.setRecipient(Message.RecipientType.TO,to); InternetAddress[] tos = new InternetAddress[mailReceiveList.size()]; for (int i = 0; i < mailReceiveList.size(); i++) { tos[i] = new InternetAddress(mailReceiveList.get(i).toString()); } mimeMessage.setRecipients(Message.RecipientType.TO,tos); //设置抄送 // InternetAddress cc = new InternetAddress("ssss@yahoo.com"); // mimeMessage.setRecipient(Message.RecipientType.CC,cc); //设置秘密抄送,其他收件人不能看到密送的邮件地址 // InternetAddress bcc = new InternetAddress("yyyy@163.com"); // mimeMessage.setRecipient(Message.RecipientType.BCC,bcc); //设置邮件标题 mimeMessage.setSubject(subject); //邮件内容 mimeMessage.setContent(content,"text/html;charset = UTF-8"); //发送邮件 Transport.send(mimeMessage); } /** * 3发送邮件(群发2) * @param mailSend 发件人 * @param mailSendAuthCode 发件人邮箱授权码 * @param mailReceives 收件人(群发,多个邮箱以逗号隔开) * @param subject 邮件主题 * @param content 邮件内容 * @throws AddressException */ public static void sendMails(String mailSend, String mailSendAuthCode, String mailReceives, String subject, String content) throws MessagingException { //配置发送邮件的属性环境 Properties properties = new Properties(); //是否需要身份验证 properties.put("mail.smtp.auth","true"); //服务器地址 // properties.put("mail.smtp.host","smtp.qq.com"); properties.put("mail.smtp.host","smtp.126.com"); // properties.put("mail.smtp.port",465); //登陆账号密码,需开启smtp // properties.put("mail.user","发件人邮箱"); properties.put("mail.user",mailSend); //smtp提供的授权码(非密码) // properties.put("mail.password","发件人邮箱授权码");//qq // properties.put("mail.password","发件人邮箱授权码");//126 properties.put("mail.password",mailSendAuthCode); //构建授权信息,用于进行SMTP身份验证 Authenticator authenticator = new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { //用户名,密码 String userName = properties.getProperty("mail.user"); String password = properties.getProperty("mail.password"); return new PasswordAuthentication(userName, password); } }; //使用环境属性和授权信息,创建邮件会话 Session session = Session.getInstance(properties, authenticator); //创建邮件消息 MimeMessage mimeMessage = new MimeMessage(session); //设置发件人 InternetAddress from = new InternetAddress(properties.getProperty("mail.user")); mimeMessage.setFrom(from); //设置收件人(多个收件人,群发,收件人之间以逗号隔开) // InternetAddress tos = new InternetAddress(mailReceives); mimeMessage.setRecipients(Message.RecipientType.TO,mailReceives); //设置抄送 // InternetAddress cc = new InternetAddress("ssss@yahoo.com"); // mimeMessage.setRecipient(Message.RecipientType.CC,cc); //设置秘密抄送,其他收件人不能看到密送的邮件地址 // InternetAddress bcc = new InternetAddress("yyyy@163.com"); // mimeMessage.setRecipient(Message.RecipientType.BCC,bcc); //设置邮件标题 mimeMessage.setSubject(subject); //邮件内容 mimeMessage.setContent(content,"text/html;charset = UTF-8"); //发送邮件 Transport.send(mimeMessage); } /** * 适配服务器 * 开启ssl安全机制,全局打印邮件发送者和内容相关信息 * 该方法开启ssl并改端口为465后,解决了阿里云服务器中运行发送邮件报错问题(连接超时) * @param sender 发件人 * @param senderAuthCode 发件人授权码 * @param receivers 收件人(单个收件人或多个收件人,多个邮箱则用逗号隔开) * @param emailSubject 邮件主题 * @param emailContent 邮件内容 * @throws MessagingException */ public static void sendSslEmail(String sender, String senderAuthCode, String receivers, String emailSubject, String emailContent) throws MessagingException { //配置发送邮件配置的属性环境 Properties properties = new Properties(); //配置邮件服务器地址 properties.setProperty("mail.smtp.host", "smtp.126.com"); //开启授权 properties.setProperty("mail.smtp.auth","true"); //配置端口号 properties.setProperty("mail.smtp.port","465"); // properties.setProperty("mail.smtp.starttls.enable","true"); //开启ssl安全机制 properties.setProperty("mail.smtp.ssl.enable","true"); //构建授权信息,用于smtp身份验证 Authenticator authenticator = new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(sender,senderAuthCode); } }; //使用邮件属性和授权信息,创建会话 Session session = Session.getDefaultInstance(properties, authenticator); //开启debug模式,将信息打印控制台 session.setDebug(true); //构建邮件消息体 Message message = new MimeMessage(session); //设置发件人 message.setFrom(new InternetAddress(sender)); //发送邮件的方式(直发,抄送,秘密抄送等)收件人如果为多个,可在收件人邮箱与邮箱之间用逗号隔开 message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(receivers)); //设置邮件主题 message.setSubject(emailSubject); //设置邮件内容 message.setContent(emailContent,"text/html;charset=UTF-8"); //发送邮件 Transport.send(message); } }
注1:代码中的一些函数导包必须是引入依赖中的包,如果用内置包可能会报错
注2:功能放在阿里云服务器上使用报错(连接超时)原因是阿里云服务器出于安全问题(避免垃圾邮件)关闭了smtp默认使用的25端口,所以才有了开启ssl更改端口为465的这个方法,可以在阿里云服务器上使用
package com.smtp.email; import javax.mail.MessagingException; import java.util.ArrayList; import java.util.List; /** * @Author:寒山李白 * @Date:2023/1/8 13:02 */ public class SmtpEmailTestController { public static void main(String[] args) throws MessagingException { //1 单一邮箱发送----126发给qq SmtpEmailUtils.sendMail("发件人邮箱", "发件人邮箱的授权码", "收件人邮箱","邮件主题名称","邮件内容"); //2 多邮箱发送,名单列表传参 List tosList = new ArrayList(); tosList.add("收件人邮箱1"); tosList.add("收件人邮箱2"); //SmtpEmailUtils.sendMails("发件人邮箱", "发件人邮箱的授权码", tosList,"邮件主题名称","邮件内容"); //3 多邮箱发送,字符串传参,每个邮箱之间用逗号隔开 String tosList2 = "收件人邮箱1,收件人邮箱2"; // SmtpEmailUtils.sendMails("发件人邮箱", "发件人邮箱的授权码", tosList2,"邮件主题名称","邮件内容"); //4 开启ssl,单个邮箱或多个邮箱发送 // SmtpEmailUtils.sendSslEmail("发件人邮箱","发件人邮箱的授权码","收件人邮箱","邮件主题名称","邮件内容"); //开启ssl,单个邮箱或多个邮箱发送 // SmtpEmailUtils.sendSslEmail("发件人邮箱","发件人邮箱的授权码","收件人邮箱1,收件人邮箱2","邮件主题名称","邮件内容"); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。