spring boot发送其他邮件

时间:2022-09-24 11:17:39

 

前面已经讲了使用springboot采用常规的javaweb方式发送邮件使用spring模板发送邮件。但是发送的都是文本文件,现在来说一下使用spring模板发送一些其他的邮件。

1.pom.xml依赖

     <!--spring发送邮件-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <!--模板邮件-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
        </dependency>

2.配置文件:

#####163邮箱########
spring.mail.host=smtp.163.com
spring.mail.username=xxxxxxx@163.com
#163邮箱授权码
spring.mail.password=*********
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true

3.发送各种邮件方法:

@Controller
public class ModelController {

    @Autowired
    private JavaMailSender mailSender;
    @Value("${spring.mail.username}")
    private String Sender; //读取配置文件中的参数
    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer; 


    /**
     * spring发送文本邮件
     */
    @RequestMapping("/mail")
    @ResponseBody
    public String sendMail(){
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(Sender);
        message.setTo("*********@qq.com");
        message.setSubject("主题:简单邮件");
        message.setText("测试邮件内容");
        mailSender.send(message);
        return "发送成功!";
    }

    /**
     * spring发送Html邮件
     */
    @RequestMapping("/mailHtml")
    @ResponseBody
    public String sendHtmlMail(){
        MimeMessage message = null;
        try {
            message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(Sender);
            helper.setTo("*********@qq.com");
            helper.setSubject("标题:发送Html内容");

            StringBuffer sb = new StringBuffer();
            sb.append("<h1>大标题-h1</h1>")
                    .append("<p style='color:#F00'>红色字</p>")
                    .append("<p style='text-align:right'>右对齐</p>");
            helper.setText(sb.toString(), true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        mailSender.send(message);
        return "发送成功!";
    }

    /**
     * spring发送带附件的邮件
     */
    @RequestMapping("/mailAttach")
    @ResponseBody
    public String sendAttachmentsMail(){
        MimeMessage message = null;
        try {
            message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(Sender);
            helper.setTo("*********@qq.com");
            helper.setSubject("主题:带附件的邮件");
            helper.setText("带附件的邮件内容");
            //注意项目路径问题,自动补用项目路径
            FileSystemResource file = new FileSystemResource(new File("F:\\临时\\SpringBoot\\src\\main\\resources\\static\\image\\迪丽热巴.jpg"));
            //加入邮件
            helper.addAttachment("热巴.jpg", file);
        } catch (Exception e){
            e.printStackTrace();
        }
        mailSender.send(message);
        return "发送成功!";
    }

    /**
     * spring发送带静态资源的邮件,资源直接发在邮件内容里,不是附件形式。
     */
    @RequestMapping("/mailLine")
    @ResponseBody
    public String sendInlineMail(){
        MimeMessage message = null;
        try {
            message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(Sender);
            helper.setTo("*********@qq.com");
            helper.setSubject("主题:带静态资源的邮件");
            //第二个参数指定发送的是HTML格式,同时cid:是固定的写法
            helper.setText("<html><body>带静态资源的邮件内容 图片:<img src='cid:picture' /></body></html>", true);

            FileSystemResource file = new FileSystemResource(new File("F:\\临时\\SpringBoot\\src\\main\\resources\\static\\image\\迪丽热巴.jpg"));
            helper.addInline("picture",file);
        } catch (Exception e){
            e.printStackTrace();
        }
        mailSender.send(message);
        return "发送成功!";
    }

    /**
     * spring发送模板邮件
     */
    @RequestMapping("/mailTemplate")
    @ResponseBody
    public String sendTemplateMail(){
        MimeMessage message = null;
        try {
            message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(Sender);
            helper.setTo("*********@qq.com");
            helper.setSubject("主题:模板邮件");

            Map<String, Object> model = new HashedMap();
            model.put("username", "zggdczfr");

            //修改 application.properties 文件中的读取路径
//            FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
//            configurer.setTemplateLoaderPath("classpath:templates");
            //读取 html 模板
            Template template = freeMarkerConfigurer.getConfiguration().getTemplate("mail.html");
            String html = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
            helper.setText(html, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        mailSender.send(message);
        return "发送成功!";
    }

}

4.图片和模板存放位置:

spring boot发送其他邮件