参考:
https://github.com/fire-basketball/springboot2-emailhttps://github.com/fire-basketball/springboot2-emailhttps://blog.csdn.net/weixin_38611617/article/details/115999647
https://blog.csdn.net/weixin_38611617/article/details/115999647
1,工具类
package com.ruoyi.ibs.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import javax.mail.internet.MimeMessage;
import java.io.File;
/**
*@author created by Jerry
*@date 2022年4月7日---下午2:23:47
*@problem
*@answer
*@action
*/
@Service
public class MailService {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Value("${spring.mail.username}")
private String from;
@Autowired
private JavaMailSender mailSender;
/**
* 发送文本邮件
*
* @param to 接收人
* @param subject 主题
* @param content 邮件内容
*/
public void sendSimpleMail(String to,String subject,String content){
SimpleMailMessage message = new SimpleMailMessage();
message.setTo(to);
message.setSubject(subject);
message.setText(content);
message.setFrom(from);
mailSender.send(message);
logger.info("发送邮件: {},{},{}",to,subject,content);
}
/**
* 发送HTML邮件
*
* @param to
* @param subject
* @param content
*/
public void sendHtmlMail(String to,String subject,String content) throws Exception {
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content,true);
mailSender.send(mimeMessage);
}
/**
* 发送带副本的邮件
*
* @param to
* @param subject
* @param content
*/
public void sendAttachmentMail(String to,String subject,String content,String filepath) throws Exception {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message,true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content,true);
//文件流:获取本地文件
FileSystemResource file = new FileSystemResource(new File(filepath));
String filename = file.getFilename();
//可以发送多个
helper.addAttachment(filename,file);
// helper.addAttachment(filename+"_test",file);
//进行发送
mailSender.send(message);
}
/**
* 发送图片邮件
*
* @param to
* @param subject
* @param content
* @param rscPath
* @param rscId
* @throws Exception
*/
public void sendImageMail(String to,String subject,String content,String rscPath,String rscId){
logger.info("发送静态邮件开始: {},{},{},{},{}",to,subject,content,rscPath,rscId);
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = null;
try{
helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content,true);
FileSystemResource file = new FileSystemResource(new File(rscPath));
helper.addInline(rscId,file);
mailSender.send(message);
logger.info("发送静态图片邮件成功!");
}catch (Exception e){
logger.error("发送静态邮件失败!",e);
}
}
}
2,maven
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
3,spring-boot配置参数
# 邮箱环境配置
spring.mail:
host: smtp.163.com
username: 你的发件邮箱
password: 你的邮箱密码
default-encoding: UTF-8
Linux环境可能25端口受限,用465 SSL,配置如下:
spring.mail:
port: 465
host: 123.126.97.113
username: XXX@163.com
password: DDDD
default-encoding: UTF-8
protocol: smtp
properties:
mail:
smtp:
ssl:
enable: true
socketFactory:
port: 465
class: javax.net.ssl.SSLSocketFactory
4,发送示例
package com.ruoyi.ibs.task;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.github.pagehelper.PageHelper;
import com.ruoyi.ibs.msg.domain.IbsMymsg;
import com.ruoyi.ibs.msg.service.IIbsMymsgService;
import com.ruoyi.ibs.util.MailService;
import com.ruoyi.quartz.task.RyTask;
/**
*@author created by Jerry
*@date 2022年4月7日---下午2:06:05
*@problem
*@answer
*@action
*/
@Component("taskMsg")
public class TaskMsg extends RyTask{
@Autowired
private IIbsMymsgService ibsMymsgService;
@Resource
MailService mailService;
public void SendMail()
{
IbsMymsg ibsMymsg = new IbsMymsg();
ibsMymsg.setMymsgSent("N");
PageHelper.startPage(1, 300);
List<IbsMymsg> list = ibsMymsgService.selectIbsMymsgTaskList(ibsMymsg);
for (Iterator iterator = list.iterator(); iterator.hasNext();) {
IbsMymsg msg = (IbsMymsg) iterator.next();
String nickName = msg.getUser().getNickName();
String gender = msg.getUser().getSex().equals("1")?"女士":"先生";
String toEmail = msg.getUser().getEmail();
String title = msg.getSnot().getNoticeTitle();
String content = msg.getSnot().getNoticeContent();
String temp = nickName + gender + ":\n\t" + content;
mailService.sendSimpleMail(toEmail,title,temp);
msg.setMymsgSent("Y");
ibsMymsgService.updateIbsMymsg(msg);
}
}
}