SpringBoot怎么整合mail发邮件。
主要介绍3点:
1、正常发送内容。
2、带附件
3、带正文图片
1、准备邮件服务器
首先准备个邮件服务器,QQ就可以。
我们依次进入首页的设置-账号
开启服务,开启完会显示服务已开启,我们可以点击继续获取授权码
点击后需要你发送手机短信验证。之后会得到一个授权码。
准备工作做完,就可以开始写代码了。
2、整合SpringBoot发邮件
添加pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
继续添加application.yml配置
spring:
mail:
#smtp服务器
host: smtp.qq.com
#发件人
username: 123456@qq.com
# 授权码
password: xxxxxxxxxxxx
#QQ端口号465或587
port: 465
default-encoding: UTF-8
properties:
timeout: 5000
connection-timeout: 5000
write-timeout: 5000
mail:
smtp:
ssl:
enable: true
socketFactoryClass: javax.net.ssl.SSLSocketFactory
#开启调试
debug: true
重点是username和password修改成自己的。
首先是接口:
public interface IEmailService {
/**
*
* @param from 发件人
* @param to 收件人
* @param subject 主题
* @param content 正文
* @param isHtml 是否html模式
* @param cc 抄送
* @param bcc 密送
* @param files 附件
* @param imgFiles 正文图片
*/
void send(String from, String to, String subject, String content, Boolean isHtml, String cc, String bcc, List<File> files, List<File> imgFiles);
}
实现类:
package com.xx.mail;
import cn.hutool.core.collection.CollectionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.io.File;
import java.util.Date;
import java.util.List;
@Slf4j
@Service
publicclass EmailServiceImpl implements IEmailService {
@Autowired
private JavaMailSender javaMailSender;
public void send(String from, String to, String subject, String content, Boolean isHtml, String cc, String bcc, List<File> files, List<File> imgFiles) {
try {
MimeMessageHelper messageHelper = new MimeMessageHelper(javaMailSender.createMimeMessage(), true);
//发件人
messageHelper.setFrom(from);
//收件人
messageHelper.setTo(to.split(","));
//主题
messageHelper.setSubject(subject);
//内容
messageHelper.setText(content, isHtml);
//正文图片
//。。。待定
//抄送
if (!StringUtils.isEmpty(cc)) {
messageHelper.setCc(cc.split(","));
}
//密送
if (!StringUtils.isEmpty(bcc)) {
messageHelper.setCc(bcc.split(","));
}
//附件
if (CollectionUtil.isNotEmpty(files)) {
for (File file : files) {
messageHelper.addAttachment(file.getName(), file);
}
}
// 发送时间
messageHelper.setSentDate(new Date());
//正式发送邮件
javaMailSender.send(messageHelper.getMimeMessage());
} catch (Exception e) {
thrownew RuntimeException("邮件发送失败", e);
}
}
}
发邮件controller
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequiredArgsConstructor
publicclass EmailController {
privatefinal IEmailService emailService;
@PostMapping("send")
public void sendEmail() {
String from="123456@qq.com";
String to="123456@qq.com";
String title="第一封邮件";
String content="<p>我是第一封邮件</p><br> hello!";
emailService.send(from,to,title,content,true,null,null,null,null);
}
}
注意发件人from和收件人要输对,全部输入自己的邮箱地址。
收到到邮件:
3、带附件的
试试带附件的
List<File> files = new ArrayList<>();
File file = new File("E:xxx\\logback.xml");
files.add(file);
emailService.send(from,to,title,content,true,null,null,files,null);
4、带正文图片的
service中加入这段:
//正文图片
if (CollectionUtil.isNotEmpty(imgFiles)) {
for (File file : imgFiles) {
//messageHelper.addInline(file.getName(), file);
// 嵌入图片
if(file.getAbsolutePath().contains(".png") || file.getAbsolutePath().contains(".jpg")){
FileSystemResource image = new FileSystemResource(file);
messageHelper.addInline("image1", image);
}
}
}
controller:
String title="第4封邮件!";
String content="<p>我是第4封邮件</p><br> hello! <img src='cid:image1'>";
List<File> files = new ArrayList<>();
File file = new File("C:\\xx\\1.png");
files.add(file);
emailService.send(from,to,title,content,true,null,null,files,files);
注意src中的image1对应service中的名字。