如何用Java实现邮件发送?
用springboot+thymeleaf来简单的实现邮件的发送任务。(example:QQ邮箱)
1.登录QQ邮箱,并且拿到授权码。(设置–>账户–>开启服务–>POP3/SMTP–>登录,得到授权码。
2.创建springboot (web)项目 ,在破pom.xml中引入thymeleaf依赖。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
3.配置application.properties文件
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
创建HTML
<!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://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1555409261754&di=7de789c2302e7810ce627117e57fa855&imgtype=0&src=http%3A%2F%2Fpic2.orsoon.com%2F2017%2F0426%2F20170426112238942.jpg"/>
<table border="1">
<tr>
<td>职位</td>
<td th:text="${position}"></td>
</tr>
<tr>
<td>薪水</td>
<td th:text="${salary}"></td>
</tr>
</table>
</body>
</html>
然后在MailApplicationTests里面创建测试类
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);
}
}
查看运行结果
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,此时邮件发送已经完成~。