前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用Spring Boot发送邮件

使用Spring Boot发送邮件

作者头像
Java架构师必看
发布2021-10-18 10:40:13
4940
发布2021-10-18 10:40:13
举报
文章被收录于专栏:Java架构师必看

pom包配置

代码语言:javascript
复制
<dependency>
   <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>
  • 配置文件
代码语言:javascript
复制
#163邮箱配置
spring.mail.host=smtp.163.com #邮箱服务器地址
spring.mail.username=xxx.163.com #用户名
spring.mail.password=ooo #开启POP3之后设置的客户端授权码
spring.mail.default-encoding=UTF-8 #编码
# 超时时间(可选)
spring.mail.properties.mail.smtp.connectiontimeout=5000
spring.mail.properties.mail.smtp.timeout=3000
spring.mail.properties.mail.smtp.writetimeout=5000
#126邮箱配置
spring.mail.host=smtp.126.com
spring.mail.username=xxx.126.com
spring.mail.password=ooo #开启POP3之后设置的客户端授权码
spring.mail.default-encoding=UTF-8
#qq邮箱配置
spring.mail.host=smtp.qq.com
spring.mail.username=xxx@qq.com
spring.mail.password=ooo #开启POP3之后设置的客户端授权码
spring.mail.default-encoding=UTF-8

1、这里的password不是登录密码,是开启POP3之后设置的客户端授权码 2、 默认端口25,使用465端口时,需要添加配置,如果465不能发送成功,试试587端口:

代码语言:javascript
复制
spring.mail.port=465
spring.mail.properties.mail.smtp.ssl.enable=true
  • JavaMailSender

Spring已经帮我们内置了JavaMailSender,可以直接在项目中引用

  • 简单的文本邮件
代码语言:javascript
复制
/**
 * MailService实现类
 */
@Component
public class MailServiceImpl implements MailService {
    @Autowired
    private JavaMailSender mailSender;
    @Value("${spring.mail.username}")
    private String from;
    @Override
    public void sendSimpleMail(String to, String subject, String content) throws MailException {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from); // 邮件发送者
        message.setTo(to); // 邮件接受者
        message.setSubject(subject); // 主题
        message.setText(content); // 内容
        mailSender.send(message);
    }
}
  • 富文本邮件

发送富文本邮件需要使用MimeMessageHelper类,MimeMessageHelper支持发送复杂邮件模板,支持文本、附件、HTML、图片等。

  • 发送带图片的邮件
代码语言:javascript
复制
@Override
public void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId) throws MessagingException {
    MimeMessage message = mailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(message, true);
    helper.setFrom(from);
    helper.setTo(to);
    helper.setSubject(subject);
    helper.setText(content, true);
    File file = new File(rscPath);
    FileSystemResource res = new FileSystemResource(file);
    helper.addInline(rscId, res);
    mailSender.send(message);
}
  • 如果需要发送多张图片,可以改变传参方式,使用集合添加多个<img src='cid:rscId'>和

helper.addInline(rscId, res);即可实现

  • 单元测试:
代码语言:javascript
复制
@Test
public void test2() {
    String to = "xxx@163.com";
    String subject = "今晚要加班,不用等我了";
    String rscId = "img110";
    String content = "<html><body><img width='250px' src=\'cid:" + rscId + "\'></body></html>
}

这里使用的qq邮箱作为收件人,结果被坑惨了。刚开始收到的邮件都是破图,查看源码发现src也没有没有图片地址,回去看了一波代码,好久之后终于发现破图的原因了:qq邮箱默认把我的图片屏蔽掉了,然后在收件人下方、正文上方有一行黄色的警告,点击信任此邮箱,我的天,终于看见图片了。

  • 发送HTML邮件
代码语言:javascript
复制
@Override
public void sendHtmlMail(String to, String subject, String content) throws MessagingException {
    MimeMessage message = mailSender.createMimeMessage();
    //true 表⽰示需要创建⼀一个 multipart message
    MimeMessageHelper helper = new MimeMessageHelper(message, true);
    helper.setFrom(from);
    helper.setTo(to);
    helper.setSubject(subject);
    helper.setText(content, true);
    mailSender.send(message);
}
  • 单元测试:
代码语言:javascript
复制
@Test
public void test() {
    String to = "xxx@qq.com";
    String subject = "猜猜我今天买了啥";
    String content = "<html><head></head><body><h3>哈哈,什么都没有</h3></body></html>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档