1.引入jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2.application.properties配置邮件相关信息
spring.mail.default-encoding=UTF-8
spring.mail.host=smtp.qq.com
spring.mail.username=1xxxxxxx6@qq.com
spring.mail.password=cwxxxxxxxxxj
spring.mail.test-connection=true
# 126邮箱SMTP服务器地址:smtp.126.com,端口号:465或者994
# 2163邮箱SMTP服务器地址:smtp.163.com,端口号:465或者994
# yeah邮箱SMTP服务器地址:smtp.yeah.net,端口号:465或者994
# qq邮箱SMTP服务器地址:smtp.qq.com,端口号465或587*
spring.mail.port=587
spring.mail.properties.mail.smtp.socketFactoryClass=javax.net.ssl.SSLSocketFactory
# 表示开启debug模式,邮件发送过程的日志会在控制台打印出来,方便排查问题
spring.mail.properties.mail.debug=true
注意:这里以qq邮箱为主,密码获取通过qq邮箱->设置->账户->POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务->开启服务->POP3/SMTP服务->开启->通过短信发送获取密码,如下图。
3.封装邮件发送代码如下,可以使用测试类或者controller层接口调用,这里以测试类为主。
package com.example.demo.service;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;
import java.io.File;
/**
* MailService
*
* @Description
*/
@Service
public class MailService {
@Resource
private JavaMailSender javaMailSender;
/**
* 发送简单邮件
* @param subject 邮件主题
* @param content 邮件内容
* @param to 收件人
* @param from 发件人
* @param cc 抄送人,多个逗号分隔
* @param bcc 密送人,多个逗号分隔
*/
public void sendSimpleMail(String subject, String content, String to, String from,String cc,String bcc) {
SimpleMailMessage message = new SimpleMailMessage();
message.setSubject(subject);
message.setText(content);
message.setTo(to);
if(StringUtils.isNotBlank(cc)){
message.setCc(cc.split(","));
}
if(StringUtils.isNotBlank(bcc)){
message.setBcc(bcc.split(","));
}
message.setFrom(from);
javaMailSender.send(message);
}
/**
* 发送带附件邮件
* @param subject 邮件主题
* @param content 邮件内容
* @param to 收件人
* @param from 发件人
* @param cc 抄送人,可以有多个
* @param bcc 密送人,可以有多个
* @param attachmentPath 附件路径
*/
public void sendAttachmentMail(String subject, String content, String to, String from,String cc,String bcc,String attachmentPath) {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
try {
MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8");
message.setSubject(subject);
//第2个参数默认为false,若设为true时,邮件内容需要使用html文本
message.setText(content,false);
message.setTo(to);
if(StringUtils.isNotBlank(cc)){
message.setCc(cc.split(","));
}
if(StringUtils.isNotBlank(bcc)){
message.setBcc(bcc.split(","));
}
message.setFrom(from);
if(StringUtils.isNotBlank(attachmentPath)){
File file = new File(attachmentPath);
message.addAttachment(file.getName(),file);
}
javaMailSender.send(mimeMessage);
} catch (Exception e) {
e.printStackTrace();
}
}
}
4.通过测试类进行测试
package com.example.demo;
import com.example.demo.service.MailService;
import org.junit.jupiter.api.Test;
import javax.annotation.Resource;
/**
* MailServiceTest
*
* @Description
*/
@SpringBootTest
public class MailServiceTest {
@Resource
MailService mailService;
@Test
public void sendSimpleMail_test() {
String subject = "测试我的代码-邮件功能";
String content = "疫情防护,人人有责。";
String to = "5xxxxx5@qq.com";
String from = "1xxxxxx6@qq.com";
String cc = "1xxxxxx4@qq.com,";
String bcc = "1xxxx4@qq.com,7xxxxxx1@qq.com,1xxxxxx2@qq.com";
mailService.sendSimpleMail(subject, content, to, from, cc, bcc);
}
@Test
public void sendAttachmentMail_test() {
String subject = "测试我的代码-邮件功能";
String content = "哈哈,给你们分享一本书";
String to = "5xxxxx5@qq.com";
String from = "1xxxx6@qq.com";
String cc = "1xxxx74@qq.com,";
String bcc = "1xxxxx4@qq.com,7xxxxxx1@qq.com,1xxxxxxxx2@qq.com";
String attachmentPath = "D:\\sxxxxdy\\xxx\\xxx\\路xxxx传.pdf";
mailService.sendAttachmentMail(subject, content, to, from, cc, bcc,attachmentPath);
}
}