email邮箱服务创建步骤
引入
<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>
application.yml qq邮箱配置
spring:
mail:
host: smtp.exmail.qq.com
password: QLWG5W9nBmcGAENb # 授权码
username: yuanfeng@qq.com
sendTo: zhangyuanfeng@bitren.onaliyun.com
邮箱服务接口
public interface IEmailService {
void sendSimpleEmailToAppoint(String subject, String content);
void sendAttachmentsMailToAppoint(String subject, String content, String filePath);
void sendInlineResourceMailToAppoint(String subject, String content, String rscPath, String rscId);
void sendHtmlMailToAppoint(String subject, String content);
void sendThymeleafEmailToAppoint(String subject, String content);
}
邮箱服务实现
/**
* 邮件服务
*
* @author yuanfeng.z
* @date 2019/11/15 17:15
*/
@Slf4j
@Service
public class EmailServiceImpl implements IEmailService {
@Value(value = "${spring.mail.username}")
private String sendFrom;
@Value(value = "${spring.mail.sendTo}")
private List<String> sendToList;
@Autowired
private JavaMailSender javaMailSender;
@Autowired
private TemplateEngine templateEngine;
/**
* 发送文本邮件到指定邮箱
*
* @param subject
* @param content
*/
@Override
public void sendSimpleEmailToAppoint(String subject, String content) {
// 构造Email消息
for (String sendTo : sendToList) {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(sendFrom);
message.setTo(sendTo);
message.setSubject(subject);
message.setText(content);
try {
javaMailSender.send(message);
} catch (Exception e) {
log.error("{} send message to {} fail, subject => {}", sendFrom, sendTo, subject);
return;
}
log.info("{} send message to {} success, subject => {}", sendFrom, sendTo, subject);
}
}
/**
* 发送文本和附件邮件到指定邮箱
*
* @param subject
* @param content
* @param filePath 文件路径
*/
@Override
public void sendAttachmentsMailToAppoint(String subject, String content, String filePath) {
MimeMessage message = javaMailSender.createMimeMessage();
for (String sendTo : sendToList) {
try {
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(sendFrom);
helper.setTo(sendTo);
helper.setSubject(subject);
helper.setText(content, true);
FileSystemResource file = new FileSystemResource(new File(filePath));
String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
helper.addAttachment(fileName, file);
javaMailSender.send(message);
} catch (MessagingException e) {
log.error("{} send message to {} fail, subject => {}", sendFrom, sendTo, subject);
return;
}
log.info("{} send message to {} success, subject => {}", sendFrom, sendTo, subject);
}
}
/**
* 发送html邮件
* @param subject
* @param content
*/
@Override
public void sendHtmlMailToAppoint(String subject, String content) {
// html 邮件对象
MimeMessage message = javaMailSender.createMimeMessage();
for (String sendTo : sendToList) {
try {
//true表示需要创建一个multipart message
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(sendFrom);
helper.setTo(sendTo);
helper.setSubject(subject);
helper.setText(content, true);
javaMailSender.send(message);
} catch (MessagingException e) {
log.error("{} send message to {} fail, subject => {}", sendFrom, sendTo, subject);
return;
}
log.info("{} send message to {} success, subject => {}", sendFrom, sendTo, subject);
}
}
/**
* 发送文本和附件邮件到指定邮箱
*
* @param subject
* @param content
* @param rscPath
* @param rscId 任意
*/
@Override
public void sendInlineResourceMailToAppoint(String subject, String content, String rscPath, String rscId) {
MimeMessage message = javaMailSender.createMimeMessage();
for (String sendTo : sendToList) {
try {
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(sendFrom);
helper.setTo(sendTo);
helper.setSubject(subject);
helper.setText(content, true);
FileSystemResource res = new FileSystemResource(new File(rscPath));
helper.addInline(rscId, res);
javaMailSender.send(message);
} catch (MessagingException e) {
log.error("{} send message to {} fail, subject => {}", sendFrom, sendTo, subject);
return;
}
log.info("{} send message to {} success, subject => {}", sendFrom, sendTo, subject);
}
}
/**
* html模板发送邮箱
* @param subject
* @param content
*/
@Override
public void sendThymeleafEmailToAppoint(String subject, String content) {
// 构造Email消息
for (String sendTo : sendToList) {
//创建邮件正文
Context context = new Context();
context.setVariable("id", "006");
String emailContent = templateEngine.process("emailTemplate", context);
try {
this.sendHtmlMailToAppoint(subject, emailContent);
} catch (Exception e) {
log.error("{} send message to {} fail, subject => {}", sendFrom, sendTo, subject);
return;
}
log.info("{} send message to {} success, subject => {}", sendFrom, sendTo, subject);
}
}
}
测试
public class EmailTest extends ZddPublicserviceApplicationTests {
@Autowired
private EmailServiceImpl sendSimpleEmail;
@Test
public void sendSimpleEmailToAppoint() {
sendSimpleEmail.sendSimpleEmailToAppoint("test", "test");
}
@Test
public void sendAttachmentsMailToAppoint() {
String filePath="e:\\tmp\\application.log";
sendSimpleEmail.sendAttachmentsMailToAppoint("主题:带附件的邮件", "有附件,请查收!", filePath);
}
@Test
public void sendInlineResourceMailToAppoint() {
String rscId = "neo006";
String content="<html><body>这是有图片的邮件:<img src=\'cid:" + rscId + "\' ></body></html>";
String imgPath = "C:\\Users\\summer\\Pictures\\favicon.png";
sendSimpleEmail.sendInlineResourceMailToAppoint("主题:这是有图片的邮件", content, imgPath, rscId);
}
}
qq邮箱授权码设置及代码教程
https://www.jiweichengzhu.com/article/124fc2989b984a789208761d332e4cf2
教程
https://www.cnblogs.com/ityouknow/p/6823356.html
https://juejin.im/post/5af25c61518825670a1046e9
网友评论