java发送email

本文介绍了如何利用SpringBoot结合Thymeleaf模板引擎,通过QQ邮箱的授权码来实现邮件的发送。步骤包括获取邮箱授权码、创建SpringBoot Web项目、引入Thymeleaf依赖、配置邮件服务参数、编写HTML邮件模板以及测试发送邮件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如何用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,此时邮件发送已经完成~。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值