springboot利用JavaMailSender发送邮件

之前用javamaill的方式发送邮件,比较繁琐,现在利用spring-mail来发送邮件,非常简单

1.引入pom依赖,分别是freemarker邮件模板和spring-maill依赖

<!-- Spring Boot Freemarker 依赖,发送HTML格式的邮件的方式 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>
<!-- spring-boot-mail 发送邮箱 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
           </dependency>

2.加入配置在application.properties

#模板加载路径
spring.freemarker.template-loader-path=classpath:/static/template/
#模板文件后缀
spring.freemarker.suffix=.flt
spring.freemarker.enabled=true
spring.freemarker.cache=false
spring.freemarker.charset=UTF-8
spring.freemarker.content-type=text/html
spring.freemarker.allow-request-override=false
#是否需要检查模板路径是否存在
spring.freemarker.check-template-location=true
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
spring.freemarker.expose-spring-macro-helpers=false
#支持qq邮箱 163邮箱等
spring.mail.host=smtp.qq.com
spring.mail.protocol=smtp
#发送者邮箱需要和实际发送者一致,否则报错501
spring.mail.username=M@qq.com
#发送者邮箱的授权码,不是密码,自己去qq邮箱设置
spring.mail.password=MM
spring.mail.properties.mail.smtp.auth=true  
spring.mail.properties.mail.smtp.starttls.enable=true  
spring.mail.properties.mail.smtp.starttls.required=true
3.发送邮件接口

public interface SendEmailService {
 
    /**
     * 发送简单邮件
     * @throws Exception
     */
    public void sendSimpleMail() throws Exception;
    
    /**
     * 发送HTML邮件--代码重复样式难看弃用
     * @throws Exception
     */
    public void sendHtmlSimpleMail() throws Exception;
    
    /**
     * 发送模板文件
     * @throws Exception
     */
    public void sendFreemarker() throws Exception;
}
4.发送邮件实现部分

@Service
public class SendEmailServiceImpl implements SendEmailService {
 
    private static final Logger LOGGER = LoggerFactory.getLogger(SendEmailServiceImpl.class);
    
    @Autowired
    private JavaMailSender mailSender;
    
    @Autowired
    private Configuration configuration;
    
    public void sendSimpleMail() throws Exception {
        LOGGER.info("准备发送简单邮件");
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("2413172711@qq.com");
        message.setTo("2413172711@qq.com");
        message.setSubject("主题:简单邮件");
        message.setText("测试邮件内容");
        mailSender.send(message);
    }
 
    @Override
    public void sendHtmlSimpleMail() throws Exception {
        LOGGER.info("准备发送HTML邮件");
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom("2413172711@qq.com");
        helper.setTo("2413172711@qq.com");
        helper.setSubject("HTML主题");
        helper.setText("<html><body><img src=\"cid:springcloud\" >"
                + "</br>"
                +"祝我们520快乐!"
                + "</body></html>,true);
        // 发送图片
        File file = ResourceUtils.getFile("classpath:static/image/WechatIMG1.jpeg");
        helper.addInline("springcloud", file);
        // 发送附件
        file = ResourceUtils.getFile("classpath:static/file/a.txt");
        helper.addAttachment("附件名称", file);
        mailSender.send(message);
        
    }
    
    public void sendFreemarker() throws Exception {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom("2413172711@qq.com");
        helper.setTo("2413172711@qq.com");
        helper.setSubject("FREEMARKER主题邮件");
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("content", "这是邮件内容");
        model.put("path", "");
        Template template = configuration.getTemplate("welcome.flt");
        String text = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
        helper.setText(text, true);
        mailSender.send(message);
    }
}
5指定的模板路径下的模板文件在src/main.resource/ 下的static/template下的 文件名.ftl

需要注意这里和上面的model的赋值保持一致,否则会有空指针

<td colspan="2" style="font-size: 18px; line-height: 1.6;padding-top: 30px">
                            <div style="text-indent: 36px; padding-right: 24px">
                                 ${content}
                            </div>
                        </td>
6.发送邮件测试类

@Test
    public void testEmail() throws Exception {
        //sendEmailService.sendSimpleMail();
        //sendEmailService.sendHtmlSimpleMail();
        sendEmailService.sendFreemarker();
    }
ok,邮件到达指定邮箱啦!

代码中写死了邮箱发送者和接受者,这些可配置的,还可以发送多人的,还剩下用mq异步处理多人发送。

嗯嗯,你看到这里也知道qq号了啊,欢迎加qq交流学习,请备注:CSDN博客
--------------------- 
原文:https://blog.csdn.net/zhangxiaomin1992/article/details/80420374 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值