SpringBoot实现发送邮箱
程序员文章站
2022-05-21 18:28:30
发送邮件java工具服务类@Service@Slf4jpublic class MailService { @Autowired private JavaMailSender mailSender; @Value("${spring.mail.username}") private String SENDER; public String getUserName(){ return SENDER; } /**....
1、在邮箱服务中开启IMAP/SMTP服务和POP3/SMTP服务并获取授权码
2、在application.yml中配置邮箱服务
spring:
#配置邮件服务
mail:
host: smtp.126.com #配置SMTP服务器
default-encoding: utf-8
username: xxxxx@126.com #发送邮件方
password: xxxxxxxxxxxxx #这是刚刚获取的授权码
properties:
mail:
smtp:
ssl:
enable: true
required: true
timeout: 10000
connectiontimeout: 10000
writetimeout: 10000
3、在SpringBoot中导入依赖包
<!-- 发送邮件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
4、编写发送邮件java工具服务类
@Service
@Slf4j
public class MailService {
@Autowired
private JavaMailSender mailSender;
@Value("${spring.mail.username}")
private String SENDER;
public String getUserName(){
return SENDER;
}
/**
* 发送普通邮件
*
* @param to 收件人
* @param subject 主题
* @param content 内容
*/
public void sendSimpleMailMessge(String to, String subject, String content) {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(SENDER);
message.setTo(to);
message.setSubject(subject);
message.setText(content);
try {
mailSender.send(message);
} catch (Exception e) {
log.error("发送简单邮件时发生异常!", e);
}
}
/**
* 发送 HTML 邮件
*
* @param to 收件人
* @param subject 主题
* @param content 内容
*/
public void sendMimeMessge(String to, String subject, String content) {
MimeMessage message = mailSender.createMimeMessage();
try {
//true表示需要创建一个multipart message
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(SENDER);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
mailSender.send(message);
} catch (MessagingException e) {
log.error("发送MimeMessge时发生异常!", e);
}
}
/**
* 发送带附件的邮件
*
* @param to 收件人
* @param subject 主题
* @param content 内容
* @param filePath 附件路径
*/
public void sendMimeMessge(String to, String subject, String content, String filePath) {
MimeMessage message = mailSender.createMimeMessage();
try {
//true表示需要创建一个multipart message
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(SENDER);
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);
mailSender.send(message);
} catch (MessagingException e) {
log.error("发送带附件的MimeMessge时发生异常!", e);
}
}
/**
* 发送带静态文件的邮件
*
* @param to 收件人
* @param subject 主题
* @param content 内容
* @param rscIdMap 需要替换的静态文件
*/
public void sendMimeMessge(String to, String subject, String content, Map<String, String> rscIdMap) {
MimeMessage message = mailSender.createMimeMessage();
try {
//true表示需要创建一个multipart message
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(SENDER);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
for (Map.Entry<String, String> entry : rscIdMap.entrySet()) {
FileSystemResource file = new FileSystemResource(new File(entry.getValue()));
helper.addInline(entry.getKey(), file);
}
mailSender.send(message);
} catch (MessagingException e) {
log.error("发送带静态文件的MimeMessge时发生异常!", e);
}
}
}
其中@Value("${spring.mail.username}")表示从配置文件中读取邮箱名称。
5、测试发送邮件
@Autowired
private MailService mailService;
@RequestMapping("/test/oa/test")
public ModelAndView test(Params params){
ModelAndView mav = new ModelAndView();
mav.setViewName("index");
mailService.sendMimeMessge("xxxxx@163.com","测试","测试成功");
return mav;
}
本文地址:https://blog.csdn.net/weixin_41233219/article/details/109535403