用springboot+thymeleaf來簡單的實現郵件的發送任務。(example:QQ郵箱)
1.登陸QQ郵箱,而且拿到受權碼。(設置–>帳戶–>開啓服務–>POP3/SMTP–>登陸,獲得受權碼。
2.建立springboot (web)項目 ,在破pom.xml中引入thymeleaf依賴。html
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
複製代碼
3.配置application.properties文件java
spring.mail.host=smtp.qq.com
spring.mail.protocol=smtp
spring.mail.username=915166712@qq.com
spring.mail.default-encoding=UTF-8
spring.mail.port=465
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.debug=true
spring.mail.password=htnyjnlwjqfnbefi
複製代碼
4.建立HTMLweb
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p>歡迎 <span style="color: #ff1a0e" th:text="${username}"></span>加入 XXX 你們庭!</p>
<div>您的入職信息以下:</div>
<img src="https://user-gold-cdn.xitu.io/2019/4/16/16a266fa8acaaf90?w=454&h=340&f=jpeg&s=14854"/>
<table border="1">
<tr>
<td>職位</td>
<td th:text="${position}"></td>
</tr>
<tr>
<td>薪水</td>
<td th:text="${salary}"></td>
</tr>
</table>
</body>
</html>
複製代碼
5.而後在MailApplicationTests裏面建立測試類spring
package org.javaboy.mail;
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.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMailMessage;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.test.context.junit4.SpringRunner;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Date;
@RunWith(SpringRunner.class)
@SpringBootTest
public class MailApplicationTests {
//註冊郵件發送器到springboot中
@Autowired
JavaMailSender mailSender;
//注入模板引擎
@Autowired
TemplateEngine templateEngine;
@Test
public void test1() throws MessagingException {
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
helper.setTo("915166712@qq.com");
helper.setCc("2579605371@qq.com");
helper.setSubject("TEST");
helper.setFrom("915166712@qq.com");
helper.setSentDate(new Date());
Context context = new Context();
context.setVariable("username","張牛娃");
context.setVariable("position","Java");
context.setVariable("salary", "10000");
String mail = templateEngine.process("mail", context);
helper.setText(mail, true);
mailSender.send(mimeMessage);
}
}
複製代碼
6.查看運行結果springboot
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "SIZE", arg "73400320"
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN"
DEBUG SMTP: Found extension "AUTH=LOGIN", arg ""
DEBUG SMTP: Found extension "MAILCOMPRESS", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: protocolConnect login, host=smtp.qq.com, user=915166712@qq.com, password=<non-null>
DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM XOAUTH2
DEBUG SMTP: Using mechanism LOGIN
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN succeeded
DEBUG SMTP: use8bit false
MAIL FROM:<915166712@qq.com>
250 Ok
RCPT TO:<915166712@qq.com>
250 Ok
RCPT TO:<2579605371@qq.com>
250 Ok
DEBUG SMTP: Verified Addresses
DEBUG SMTP: 915166712@qq.com
DEBUG SMTP: 2579605371@qq.com
DATA
354 End data with <CR><LF>.<CR><LF>
Date: Tue, 16 Apr 2019 21:08:11 +0800 (GMT+08:00)
From: 915166712@qq.com
To: 915166712@qq.com
Cc: 2579605371@qq.com
Message-ID: <697240075.0.1555420098586@LAPTOP-3FE6ILOH>
Subject: TEST
MIME-Version: 1.0
Content-Type: text/html;charset=UTF-8
Content-Transfer-Encoding: quoted-printable
複製代碼
OK,此時郵件發送已經完成~。app