一、导入Maven依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> <version>2.1.7.RELEASE</version>
</dependency>
二、配置文件
spring:
mail:
host: smtp.qq.com
username: ------邮箱--
password: -----邮箱密钥--
default-encoding: UTF-8
port: 465
三、实例代码
我这里是直接提取QQ邮箱号和邮箱密钥为常量,建议你们通过配置文件的哪里注入进入
直接调用 sendMail 方法传递参数即可
package cn.yangsansui.auth.tools; import java.io.File; import java.io.UnsupportedEncodingException; import java.security.Security; import java.util.Date; import java.util.Map; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Authenticator; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import javax.mail.internet.MimeUtility; public class EmailTools { private static final String FROM_MAIL_SMTP = "smtp.qq.com"; private static final String FROM_MAIL_NAME = "QQ邮箱"; private static final String FROM_MAIL_PASS = "QQ邮箱密钥"; /** * 发送邮件(灵活度高,通用版) * * @param to 收件人, 多个Email以英文逗号分隔 * @param cc 抄送, 多个Email以英文逗号分隔 * @param subject 主题 * @param content 内容 * @return */ public static boolean sendMail(String to, String cc, String subject, String content) { try { Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; final Properties p = System.getProperties(); p.setProperty("mail.smtp.host", FROM_MAIL_SMTP); p.setProperty("mail.smtp.auth", "true"); p.setProperty("mail.smtp.user", FROM_MAIL_NAME); p.setProperty("mail.smtp.pass", FROM_MAIL_PASS); p.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY); p.setProperty("mail.smtp.socketFactory.fallback", "false"); //邮箱发送服务器端口,这里设置为465端口 p.setProperty("mail.smtp.port", "465"); p.setProperty("mail.smtp.socketFactory.port", "465"); // 根据邮件会话属性和密码验证器构造一个发送邮件的session Session session = Session.getInstance(p, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(p.getProperty("mail.smtp.user"), p.getProperty("mail.smtp.pass")); } }); session.setDebug(true); Message message = new MimeMessage(session); //消息发送的主题 message.setSubject(subject); //接受消息的人 message.setReplyTo(InternetAddress.parse(FROM_MAIL_NAME)); //消息的发送者 message.setFrom(new InternetAddress(p.getProperty("mail.smtp.user"), "农夫")); // 创建邮件的接收者地址,并设置到邮件消息中 String[] split = to.split(","); InternetAddress[] tos = new InternetAddress[split.length]; for (int i = 0; i < split.length; i++) { tos[i] = new InternetAddress(split[i]); } // 设置抄送人 if (cc != null && cc.length() > 0) { message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc)); } message.setRecipients(Message.RecipientType.TO, tos); // 消息发送的时间 message.setSentDate(new Date()); Multipart mainPart = new MimeMultipart(); // 创建一个包含HTML内容的MimeBodyPart BodyPart html = new MimeBodyPart(); // 设置HTML内容 html.setContent(content, "text/html; charset=utf-8"); mainPart.addBodyPart(html); // 将MiniMultipart对象设置为邮件内容 message.setContent(mainPart); message.setContent(mainPart); message.saveChanges(); Transport.send(message); } catch (MessagingException e) { e.printStackTrace(); return false; } catch (UnsupportedEncodingException e) { e.printStackTrace(); return false; } return true; } }