Spring Boot 发送邮件和使用模板渲染邮件内容
2017-03-07 00:00
513 查看
首先确认自己有邮件服务器。简单的话,需要知道发信协议、服务器地址、端口号、是否为加密通信。
然后确认自己有发信账号。需要知道用户名、密码。很多邮件服务的用户名就是你的邮箱,但这也不是一定的。
一般端口号与加密之间的关系是这样的:
| 协议 | 服务器地址 | 端口号(常规) | 端口号(加密) |
|: - :|: - :|: - :|: - :|
| POP3 | pop3.XXX.com | 110 | 995 |
| SMTP | smtp.XXX.com | 25 | 465 |
| IMAP | imap.XXX.com | 143 | 993 |
知道这些就可以开始开发了。先在项目里引入
本文以 Maven 为例。
然后配置
使用 SMTP 常规端口,非加密通信,不需要认证:
使用 SMTP 常规端口,非加密通信,需要认证:
使用 SMTPS (SMTP-over-SSL),SSL 加密通信,需要认证:
如果是 TLS 需要设置 properties
然后引入 JavaMailSender 对象:
这就实现了最简单的发信,要注意有些服务器只允许用户使用指定的发信邮箱地址,所以各方面都要遵守约定。
如果我们想发送 HTML 给对方,可以:
这样我们的邮件发送教程就结束了。
如果希望引入 thymeleaf / freemarker 等渲染引擎,提供邮件内容,请往下看。
以 thymeleaf 为例,引入依赖:
配置:
使用:
接下来,就可以两者配合一起使用了。
在
在 Java 中调用:
大功告成。
然后确认自己有发信账号。需要知道用户名、密码。很多邮件服务的用户名就是你的邮箱,但这也不是一定的。
一般端口号与加密之间的关系是这样的:
| 协议 | 服务器地址 | 端口号(常规) | 端口号(加密) |
|: - :|: - :|: - :|: - :|
| POP3 | pop3.XXX.com | 110 | 995 |
| SMTP | smtp.XXX.com | 25 | 465 |
| IMAP | imap.XXX.com | 143 | 993 |
知道这些就可以开始开发了。先在项目里引入
spring-boot-starter-mail的依赖:
本文以 Maven 为例。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
然后配置
application.yaml或你的配置文件:
使用 SMTP 常规端口,非加密通信,不需要认证:
spring: mail: protocol: smtp host: smtp.XXX.com port: 25 default-encoding: UTF-8 properties: mail: smtp: auth: false # 不需要认证
使用 SMTP 常规端口,非加密通信,需要认证:
spring: mail: protocol: smtp host: smtp.XXX.com port: 25 username: xxx password: xxx default-encoding: UTF-8 properties: mail: smtp: auth: true # 需要认证
使用 SMTPS (SMTP-over-SSL),SSL 加密通信,需要认证:
spring: mail: protocol: smtp host: smtp.xxx.com port: 465 username: xxx password: xxx default-encoding: UTF-8 properties: mail: smtp: auth: true # 需要认证 ssl.enable: true # 使用 SSL 加密通信 socketFactory: port: 465 # 加密通信端口号,同通信端口 class: javax.net.ssl.SSLSocketFactory # 指定加密连接的工厂类 fallback: false # 不验证服务器端证书
如果是 TLS 需要设置 properties
mail.smtp.starttls.enable = true。
然后引入 JavaMailSender 对象:
@Service public class MailService { @Autowired private JavaMailSender mailSender; public void simpleMail(String fromEmail, String toEmail, String subject, String body) { SimpleMailMessage mail = new SimpleMailMessage(); mail.setFrom(fromEmail); mail.setTo(receiverEmail); mail.setSubject(subject); mail.setText(body); mailSender.send(mail); } }
这就实现了最简单的发信,要注意有些服务器只允许用户使用指定的发信邮箱地址,所以各方面都要遵守约定。
如果我们想发送 HTML 给对方,可以:
@Service public class MailService { @Autowired private JavaMailSender mailSender; public void mimeMail(String fromEmail, List<String> toList, String subject, String html) { try { MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper messageHelper = new MimeMessageHelper(message); messageHelper.setFrom(fromEmail); for (String to : toList) { messageHelper.addTo(to); } messageHelper.setSubject(subject); messageHelper.setText(html, true); mailSender.send(message); } catch (MessagingException e) { e.printStackTrace(); } } }
这样我们的邮件发送教程就结束了。
如果希望引入 thymeleaf / freemarker 等渲染引擎,提供邮件内容,请往下看。
以 thymeleaf 为例,引入依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
配置:
spring: thymeleaf: enabled: true mode: HTML5 encoding: UTF-8 content-type: text/html prefix: classpath:/templates/ # 模板存放在资源目录的 templates/ 文件下 suffix: .html # 模板后缀 check-template-location: true check-template: false cache: false # 调试时关闭缓存
使用:
@Service public class TemplateService { @Autowired private SpringTemplateEngine templateEngine; private String render(String template, Map<String, ?> params) { Context context = new Context(LocaleContextHolder.getLocale()); context.setVariables(params); return templateEngine.process(template, context); } }
接下来,就可以两者配合一起使用了。
在
resources/templates下新建文件
sub/HelloWorld.html:
<!DOCTYPE html> <html> <body> <p>Hello World, <span th:text="${name}"></span></p> </body> </html>
在 Java 中调用:
Map<String, String> params = new HashMap<>(); params.put("name", "傅易君"); String html = templateService.render("sub/HelloWorld", params); mailService.mimeMail(fromEmail, toEmail, subject, html);
大功告成。
相关文章推荐
- Magento 发送邮件代码(使用数据库中模板)
- Spring中使用FreeMaker或Vilocity模板发送邮件 (转)
- Linux系统下使用mail发送一封简单的Internet邮件【以及验证邮件是否发送成功sendmail -bp,必须是root用户才可以使用此命令查看邮件消息队列中的内容】
- jenkins 使用邮件模板插件发送邮件
- Spring中使用FreeMaker或Vilocity模板发送邮件
- Android使用UncaughtExceptionHandler捕获全局异常 android获取手机信息大全Java追加文件内容使用JavaMail发送邮件OkHttp获取系统时间
- [PHP]使用PHPMailer发送带附件并支持HTML内容的邮件
- 使用PHPMailer发送带附件并支持HTML内容的邮件
- 使用Spring发送基于freemarker模板的邮件
- Spring 发送邮件 (3) Spring使用模板Freemarker
- 使用XSLT制作HTML邮件模板并发送
- C# 发送邮件(内容中可以插入图片,邮件可以添加附件,163邮箱、QQ邮箱等都可以使用)
- java调用html模板发送html内容的邮件
- 使用ruby抓取页面内容后发送邮件
- Spring中使用FreeMaker或Vilocity模板发送邮件 (转)
- spring使用模板发送邮件及附件笔记
- .net邮件发送实例 邮件内容为网页模板
- 使用PHPMailer发送带附件并支持HTML内容的邮件
- spring使用模板发送邮件及附件笔记
- 使用PHPMailer发送带附件并支持HTML内容的邮件