博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringBoot系列——mail
阅读量:4668 次
发布时间:2019-06-09

本文共 5120 字,大约阅读时间需要 17 分钟。

  前言

  邮件是许多项目里都需要用到的功能,之前一直都是用JavaMail来发,现在Spring框架为使用JavaMailSender接口发送电子邮件提供了一个简单的抽象,Spring Boot为它提供了自动配置以及启动模块。springboot参考手册介绍:

  作为发送方,首先需要开启POP3/SMTP服务,登录邮箱后前往设置进行开启,开启后取得授权码。

  POP3 :

  POP3是Post Office Protocol 3的简称,即的第3个版本,规定怎样将个人计算机连接到Internet的邮件服务器和下载电子邮件的电子协议。是因特网电子邮件的第一个离线协议标准,POP3允许用户从服务器上把邮件存储到本地主机(即自己的计算机)上,同时删除保存在邮件服务器上的邮件,而POP3服务器则是遵循POP3协议的接收邮件服务器,用来接收电子邮件的。
  

  SMTP:

  SMTP 的全称是“Simple Mail Transfer Protocol”,即简单邮件传输协议。是一组用于从源地址到目的地址传输邮件的规范,通过来控制邮件的中转方式。SMTP 协议属于 TCP/IP 协议簇,帮助每台计算机在发送或中转信件时找到下一个目的地。SMTP 服务器就是遵循 SMTP 协议的发送邮件服务器。

 

  代码编写

  maven引包,其中,邮件模板需要用到thymeleaf

org.springframework.boot
spring-boot-starter-mail
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test

 

  appliaction.propertise配置文件

#设置服务端口server.port=10010# Email (MailProperties)spring.mail.default-encoding=UTF-8spring.mail.host=smtp.qq.comspring.mail.username=huanzi.qch@qq.com #发送方邮件名 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

 

  SpringBootMailServiceImpl.java

@Serviceclass SpringBootMailServiceImpl implements SpringBootMailService {    @Autowired    private JavaMailSender mailSender;    /**     * 发送方     */    @Value("${spring.mail.username}")    private String from;    /**     * 发送简单邮件     *     * @param to      接收方     * @param subject 邮件主题     * @param text    邮件内容     */    @Override    public void sendSimpleMail(String to, String subject, String text) {        SimpleMailMessage message = new SimpleMailMessage();        message.setFrom(from);        message.setTo(to);        message.setSubject(subject);        message.setText(text);        mailSender.send(message);    }    /**     * 发送HTML格式的邮件     *     * @param to      接收方     * @param subject 邮件主题     * @param content HTML格式的邮件内容     * @throws MessagingException     */    @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);    }    /**     * 发送HTML格式的邮件,并可以添加附件     * @param to      接收方     * @param subject 邮件主题     * @param content HTML格式的邮件内容     * @param files   附件     * @throws MessagingException     */    @Override    public void sendAttachmentsMail(String to, String subject, String content, List
files) 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); //添加附件 for(File file : files){ helper.addAttachment(file.getName(), new FileSystemResource(file)); } mailSender.send(message); }}

 

  测试controller

@Autowired    private SpringBootMailService springBootMailService;    @Autowired    private TemplateEngine templateEngine;    @GetMapping("/index")    public String index() throws MessagingException {        //简单邮件        springBootMailService.sendSimpleMail("1726639183@qq.com","Simple Mail","第一封简单邮件");        //HTML格式邮件        Context context = new Context();        context.setVariable("username","我的小号");        springBootMailService.sendHtmlMail("1726639183@qq.com","HTML Mail",templateEngine.process("mail/mail",context));        //HTML格式邮件,带附件        Context context2 = new Context();        context2.setVariable("username","我的小号(带附件)");        ArrayList
files = new ArrayList<>(); files.add(new File("C:\\Users\\Administrator\\Desktop\\上传测试.txt")); files.add(new File("C:\\Users\\Administrator\\Desktop\\上传测试2.txt")); springBootMailService.sendAttachmentsMail("1726639183@qq.com","Attachments Mail",templateEngine.process("mail/attachment",context2),files); return "hello springboot!"; }

 

  两个html模板,路径:myspringboot\src\main\resources\templates\mail\

  mail.html

    
Mail Templates

,你好!

这是一封HTML格式的邮件。

  attachment.html

    
Mail Templates Accessory

,你好!

这是一封HTML格式的邮件。请收下附件!

 

  效果

  Simple Mail

  

  HTML Mail

 

  Attachments Mail 

 

 

  后记

  本文章部分参考:https://www.cnblogs.com/yangtianle/p/8811732.html

 

  代码开源

  代码已经开源、托管到我的GitHub、码云:

  GitHub:

  码云:

转载于:https://www.cnblogs.com/huanzi-qch/p/9957987.html

你可能感兴趣的文章
oracle 11g r2安装
查看>>
关于自关联1
查看>>
存储控制器、MMU、flash控制器介绍
查看>>
hdu-1814(2-sat)
查看>>
自我反省
查看>>
反射,得到Type引用的三种方式
查看>>
pl sql练习(2)
查看>>
Problem B: 判断回文字符串
查看>>
谷歌浏览器,添加默认搜索引擎的搜索地址
查看>>
数据结构化与保存
查看>>
C# .net 获取程序运行的路径的几种方法
查看>>
为什么需要Docker?
查看>>
国内5家云服务厂商 HTTPS 安全性测试横向对比
查看>>
how to control project
查看>>
转 python新手容易犯的6个错误
查看>>
第四节 -- 列表
查看>>
Python入门学习笔记4:他人的博客及他人的学习思路
查看>>
webstorm里直接调用命令行
查看>>
关联规则算法之FP growth算法
查看>>
对数组序列进行洗牌
查看>>