application.yml
spring:
profiles:
active: dev
# active: test
#邮件附件上传文件大小限制
servlet:
multipart:
max-file-size: 50MB #单个文件大小限制
max-request-size: 100MB #总文件大小限制(允许存储文件的文件夹大小)
mail:
default-encoding: UTF-8
host: smtp.qq.com
username: xxxxxxx@qq.com
password: xxxxxxxxx#邮箱设置第三方登录授权码
test-connection: false
properties:
mail.smtp.auth: true
邮箱设置第三方登录授权码获取
邮箱设置第三方登录授权码获取https://blog.csdn.net/xnian_/article/details/134515274?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22134515274%22%2C%22source%22%3A%22xnian_%22%7D
package com.cloud.xxx.utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.security.Security;
import java.util.Date;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
@Slf4j
@Service
public class MailServiceUtil {
@Resource
private JavaMailSender javaMailSender;
/**
* 发件人的邮箱地址<br>
* 即我们配置的邮件服务的邮箱
*/
@Value("${spring.mail.username}")
private String serverMail;
@Value("${spring.mail.password}")
private String password;
@Value("${spring.mail.host}")
private String host;
/**
* 发送邮件
*
* @param toUser 收件人邮件地址
* @param subject 标题
* @param text 正文
*/
public boolean sendMail(String toUser, String subject, String text) {
try {
MimeMessage message = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(serverMail);
helper.setTo(toUser.split(","));
helper.setSubject(subject);
helper.setText(text, true);
javaMailSender.send(message);
log.info("发送邮件to:{},主题:{},内容:{}", toUser, subject, text);
} catch (Exception e) {
log.error("sendMail ", e);
return Boolean.FALSE;
}
return Boolean.TRUE;
}
/**
* 使用加密的方式,利用 587 端口进行传输邮件,开启ssl
* @param to 收件人邮箱
* @param message 邮件正文
* @param title 邮件标题
*/
public boolean sendMail587(String to, String title, String message) {
try {
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
//设置邮件会话参数
Properties props = new Properties();
//邮箱的发送服务器地址
props.setProperty("mail.smtp.host", host);
props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.smtp.socketFactory.fallback", "false");
//邮箱发送服务器端口,这里设置为 587 端口
props.setProperty("mail.smtp.port", "587");
props.setProperty("mail.smtp.socketFactory.port", "587");
props.put("mail.smtp.auth", "true");
//获取到邮箱会话,利用匿名内部类的方式,将发送者邮箱用户名和密码授权给jvm
Session session = Session.getDefaultInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(serverMail, password);
}
});
//通过会话,得到一个邮件,用于发送
Message msg = new MimeMessage(session);
//设置发件人
msg.setFrom(new InternetAddress(serverMail));
//设置收件人,to为收件人,cc为抄送,bcc为密送
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
// msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(to, false));
// msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(to, false));
// 设置邮件标题
msg.setSubject(title);
//设置发送的日期
msg.setSentDate(new Date());
/*//设置邮件消息
msg.setText(message);*/
// 设置邮件的内容体
msg.setContent(message, "text/html;charset=UTF-8");
//调用Transport的send方法去发送邮件
Transport.send(msg);
} catch (Exception e) {
log.error("sendMail ", e);
return Boolean.FALSE;
}
return Boolean.TRUE;
}
}