Spring Boot笔记之邮件(spring-boot-starter-mail)
pom.xml引入spring-boot-starter-mail
Spring Boot2.x集成了mail模块,在dependencies
引入这个
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
application.yml配置
spring:
mail:
# 163
host: smtp.163.com
port:
username: yimcarson@163.com
password: ************
protocol: smtp
default-encoding: UTF-8
properties:
mail.smtp.auth: true
mail.smtp.starttls.enable: true
mail.smtp.starttls.required: true
mail.smtp.socketFactory.port: 465
mail.smtp.socketFactory.class: javax.net.ssl.SSLSocketFactory
mail.smtp.socketFactory.fallback: false
其中spring.mail.host
spring.mail.port
spring.mail.username
spring.mail.password
不同邮箱的配置方法也不同
163邮箱
spring:
mail:
host: smtp.163.com
port:
username: yimcarson@163.com
password: ************
其中spring.mail.port
不指定;spring.mail.password
不是邮箱密码,需要登录mail.163.com
,前往设置
客户端授权密码
中获取的一个16个字符的密码,同时允许POP3/SMTP服务。
QQ邮箱
spring:
mail:
host: smtp.qq.com
port: 587
username: yimcarson@qq.com
password: ************
spring.mail.password
不是QQ密码,登录mail.qq.com
,前往设置
账户
POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务
开启POP3/SMTP服务
获取一个16个字符的密码
Gmail邮箱
spring:
mail:
host: smtp.gmail.com
port: 465
username: yimcarson@gmail.com
password: ****************
spring.mail.password
是Gmail的密码,但是要前往Google账户的安全性较低的应用的访问权限中允许不安全应用。
发送邮件
这是一个验证码模版邮件
service实现类
import com.my.demo.project.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
import java.util.UUID;
@Service
public class MailServiceImpl implements MailService {
<span class="token annotation punctuation">@Autowired</span>
<span class="token keyword">private</span> JavaMailSender mailSender<span class="token punctuation">;</span>
<span class="token comment">/**
* 用来发送模版邮件
*/</span>
<span class="token annotation punctuation">@Autowired</span>
<span class="token keyword">private</span> TemplateEngine templateEngine<span class="token punctuation">;</span>
<span class="token annotation punctuation">@Value</span><span class="token punctuation">(</span><span class="token string">"${spring.mail.username}"</span><span class="token punctuation">)</span>
<span class="token keyword">private</span> String from<span class="token punctuation">;</span>
<span class="token annotation punctuation">@Override</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">send</span><span class="token punctuation">(</span>String to<span class="token punctuation">,</span> String subject<span class="token punctuation">,</span> String text<span class="token punctuation">)</span> <span class="token punctuation">{</span>
// SimpleMailMessage message = new SimpleMailMessage();
// message.setFrom(from);
// message.setTo(to);
// message.setSubject(subject);
// message.setText(text);
Context context <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Context</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
context<span class="token punctuation">.</span><span class="token function">setVariable</span><span class="token punctuation">(</span><span class="token string">"project"</span><span class="token punctuation">,</span> <span class="token string">"demo"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
context<span class="token punctuation">.</span><span class="token function">setVariable</span><span class="token punctuation">(</span><span class="token string">"author"</span><span class="token punctuation">,</span> <span class="token string">"yimcarson"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
context<span class="token punctuation">.</span><span class="token function">setVariable</span><span class="token punctuation">(</span><span class="token string">"code"</span><span class="token punctuation">,</span> text<span class="token punctuation">)</span><span class="token punctuation">;</span>
String emailContent <span class="token operator">=</span> templateEngine<span class="token punctuation">.</span><span class="token function">process</span><span class="token punctuation">(</span><span class="token string">"mail"</span><span class="token punctuation">,</span> context<span class="token punctuation">)</span><span class="token punctuation">;</span>
MimeMessage message <span class="token operator">=</span> mailSender<span class="token punctuation">.</span><span class="token function">createMimeMessage</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
MimeMessageHelper helper <span class="token operator">=</span> null<span class="token punctuation">;</span>
<span class="token keyword">try</span> <span class="token punctuation">{</span>
helper <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">MimeMessageHelper</span><span class="token punctuation">(</span>message<span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
helper<span class="token punctuation">.</span><span class="token function">setFrom</span><span class="token punctuation">(</span>from<span class="token punctuation">)</span><span class="token punctuation">;</span>
helper<span class="token punctuation">.</span><span class="token function">setTo</span><span class="token punctuation">(</span>to<span class="token punctuation">)</span><span class="token punctuation">;</span>
helper<span class="token punctuation">.</span><span class="token function">setSubject</span><span class="token punctuation">(</span>subject<span class="token punctuation">)</span><span class="token punctuation">;</span>
helper<span class="token punctuation">.</span><span class="token function">setText</span><span class="token punctuation">(</span>emailContent<span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">MessagingException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
mailSender<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span>message<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
}
templates模版
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>yimcarson</title>
<style>
body {
text-align: center;
margin-left: auto;
margin-right: auto;
}
#main {
text-align: center;
position: absolute;
}
</style>
</head>
<body>
<div id="main">
<h3>Welcome <span th:text="${project}"></span> -By <span th:text=" ${author}"></span></h3>
Your Verification Code is
<h2><span th:text="${code}"></span></h2>
</div>
</body>
</html>
测试
import com.my.demo.Application;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.thymeleaf.TemplateEngine;
import java.util.Date;
import java.util.UUID;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {Application.class})
public class MailServiceTest {
@Autowired
private MailService mailService;
<span class="token annotation punctuation">@Test</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testSend</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
String to <span class="token operator">=</span> <span class="token string">"yimcarson@qq.com"</span><span class="token punctuation">;</span>
mailService<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span>to<span class="token punctuation">,</span> <span class="token string">"模板邮件"</span><span class="token punctuation">,</span> UUID<span class="token punctuation">.</span><span class="token function">randomUUID</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toUpperCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
}
结语
163
QQ
这两个邮箱比较常用,至于Gmail
……,如果服务器是阿里云香港或者国外、亚马逊这些的话可以正常使用,否则……,Couldn't connect to host, port: smtp.gmail.com, 465; timeout -1
原文地址:https://blog.csdn.net/yimcarson/article/details/84936440