0%

spring boot2.0集成邮件发送功能

引入依赖

<!-- 注意版本号,测试2.0.3版本有严重问题,启动失败 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
    <version>2.1.1.RELEASE</version>
</dependency>

注册邮箱

  • 163,126,qq都行,这边测试126邮箱;
  • 开启IMAP/SMTP服务;
  • 获取授权密码,注意保存.

配置application.yaml

spring:
  mail:
    # 注册的邮箱
    username: xxxxxx@126.com
    # 密码为授权码
    password: XXXXXXXXXXXXXXX
    # smtp服务器地址
    host: smtp.126.com
    # 安全相关配置,非必须
    properties:
      mail:
        smtp:
          auth: true
          starttls:
            enable: true
            required: true

测试发送

@Service
public class MailService {
    // 这个在IDEA中会提示没有bean,注入失败,不用管~
    @Autowired
    JavaMailSender mailSender;

    public void send(){
        // 普通文本邮件
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        //邮件发送方,需要写完整邮箱地址
        simpleMailMessage.setFrom("XXXXX@126.com");
        //邮件接收方邮箱
        simpleMailMessage.setTo("1154365135@qq.com");
        //邮件标题
        simpleMailMessage.setSubject("测试主题~~");
        //邮件内容
        simpleMailMessage.setText("测试内容~~");
        //发送邮件
        mailSender.send(simpleMailMessage);
    }

    public void sendHtml() throws MessagingException {
        // html邮件
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        //邮件发送方
        helper.setFrom("authservice@126.com");
        //邮件接收方邮箱
        helper.setTo("1154365135@qq.com");
        //邮件标题
        helper.setSubject("测试HTML~~");
        //邮件内容
        helper.setText("<html>\n" +
                "<head>\n" +
                "<meta charset=\"utf-8\">\n" +
                "<title>菜鸟教程(runoob.com)</title>\n" +
                "</head>\n" +
                "<body>\n" +
                "    <h1>我的第一个标题</h1>\n" +
                "    <p style=\"color: blue;\">我的第一个段落。</p>\n" +
                "</body>\n" +
                "</html>", true);
        //发送邮件
        mailSender.send(mimeMessage);
    }

        //附件邮件, 未测试
    public void sendAttachmentMail(String to,String subject,String content,String filePath) throws MessagingException {
        MimeMessage mimeMessage=mailSender.createMimeMessage();
        MimeMessageHelper helper=new MimeMessageHelper(mimeMessage,true);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content,true);
        helper.setFrom(form);
        FileSystemResource file=new FileSystemResource(new File(filePath));
        String fileName=file.getFilename();
        helper.addAttachment(fileName,file);
        //helper.addAttachment(fileName+"02",file);  如果是多个附件的话,可以这样写。但是开发中一般都是把filepath做成一个数组,这样在这里遍历就可以了
        //helper.addAttachment(fileName+"03",file);

        mailSender.send(mimeMessage);
    }

    //图片邮件,未测试
    public void sendInlineResourceMail(String to,String subject,String content,String rscPath,String rscId) throws MessagingException {
        MimeMessage mimeMessage=mailSender.createMimeMessage();
        MimeMessageHelper helper=new MimeMessageHelper(mimeMessage,true);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content,true);
        helper.setFrom(form);
        FileSystemResource file=new FileSystemResource(new File(rscPath));
        helper.addInline(rscId,file);

        mailSender.send(mimeMessage);
    }
}