使用Spring Boot发送邮件

pom包配置

<dependency>
   <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

application文件配置

# 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
[email protected]
spring.mail.password=ooo #开启POP3之后设置的客户端授权码
spring.mail.default-encoding=UTF-8

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

spring.mail.port=465
spring.mail.properties.mail.smtp.ssl.enable=true

JavaMailSender

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

简单的文本邮件

/**
 * 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、图片等。

发送带图片的邮件

@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);
}

如果需要发送多张图片,可以改变传参方式:
使用集合添加多个和helper.addInline(rscId, res);即可实现

单元测试:

@Test
public void test2() {
    
    
    String to = "[email protected]";
    String subject = "今晚要加班,不用等我了";
    String rscId = "img110";
    String content = "<html><body><img width='250px' src=\'cid:" + rscId + "\'></body></html>";
    // 此处为linux系统路径
    String imgPath = "/Users/kx/WechatIMG16.jpeg";
    try {
    
    
        mailService.sendInlineResourceMail(to, subject, content, imgPath, rscId);
        System.out.println("成功了");
    } catch (MessagingException e) {
    
    
        System.out.println("失败了");
        e.printStackTrace();
    }
}

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

发送HTML邮件

@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);
}

单元测试:

@Test
public void test() {
    
    
    String to = "[email protected]";
    String subject = "猜猜我今天买了啥";
    String content = "<html><head></head><body><h3>哈哈,什么都没有</h3></body></html>";
    try {
    
    
        mailService.sendHtmlMail(to, subject, content);
        System.out.println("成功了");
    } catch (MessagingException e) {
    
    
        System.out.println("失败了");
        e.printStackTrace();
    }
}

测试结果:
测试结果

发送带附件的邮件

@Override
public void sendAttachmentsMail(String to, String subject, String content, String filePath) 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);
    FileSystemResource file = new FileSystemResource(new File(filePath));
    String fileName = file.getFilename();
    helper.addAttachment(fileName, file);

    mailSender.send(message);
}

如果有多个附件,同样可以改变传参方式,使用集合多次调用 helper.addAttachment(fileName, file);

单元测试:

@Test
public void test3() {
    
    
    String to = "[email protected]";
    String subject = "这是一个有附件的邮件,记得接受文件";
    String content = "嗯哼?自己看附件";
    String imgPath = "/Users/kx/WechatIMG16.jpeg";
    try {
    
    
        mailService.sendAttachmentsMail(to, subject, content, imgPath);
        System.out.println("成功了");
    } catch (MessagingException e) {
    
    
        System.out.println("失败了");
        e.printStackTrace();
    }
}

测试结果:
测试结果
值得一提的是:之前在找图片是破图的时候,发现如果有图片但是没有对应的,接收到的邮件就会以附件的方式存在

发送失败

邮件发送有着很多的原因,如:配置过程中的端口号/授权码错误、550 DT:SPM主题/内容不规范被当做垃圾邮件、发送过于频繁、网络原因。

猜你喜欢

转载自blog.csdn.net/qq_42547733/article/details/133741742
今日推荐