Spring中使用 网易邮箱 或 qq邮箱 发送信息
Posted _DiMinisH
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring中使用 网易邮箱 或 qq邮箱 发送信息相关的知识,希望对你有一定的参考价值。
Spring中使用 网易邮箱 或 qq邮箱 发送信息
1. 所需依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
其他需要的Spring依赖可以自行添加
2. 代码
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
@Service
public class EmailServiceSmtpImpl
@Autowired
private JavaMailSender emailMailSender;
public void send()
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setFrom(你的邮箱,发送方邮箱,qq邮箱或者网易邮箱);
simpleMailMessage.setTo(接收方的邮箱);
simpleMailMessage.setSubject(邮件的主题(邮件的名称));
simpleMailMessage.setText(发送的内容);
emailMailSender.send(simpleMailMessage);
3. qq邮箱配置
在application.properties文件中添加如下配置
# 服务器的主机, 是固定的
spring.mail.host=smtp.qq.com
# 授权码, 下面说如何获取
spring.mail.password=xxxx
# 配置
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
# 放送方的qq邮箱
spring.mail.username=
获取授权码
鼠标滚轮向下滑动, 找到POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务
我这里需要一些验证
获得授权码
授权码要牢记, 如果忘了, 关了再重新开启一次
4. 网易邮箱配置
在application.properties文件中添加如下配置
# 服务器的主机, 是固定的
spring.mail.host=smtp.163.com
# 授权码, 下面说如何获取
spring.mail.password=xxxx
# 配置
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
# 放送方的网易邮箱
spring.mail.username=
获取授权码
开启后获取授权码, 我的已经开启啦
测试类
可能要引入一些依赖, 大家自行引入就行, 能发送成功邮件就行
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
@ActiveProfiles("test")
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class BaseIntegrationTest
import main.service.validation.EmailServiceSmtpImpl;
import main.service.validation.SmsServiceTencentSmsImpl;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit.jupiter.SpringExtension;
@ExtendWith(SpringExtension.class)
public class SmtpSendMailUnitTests extends BaseIntegrationTest
@Autowired
private EmailServiceSmtpImpl emailServiceSmtpImpl;
@Test
public void sendSmsWangYi_thenAcceptSuccessfully ()
emailServiceSmtpImpl.send("1550571781@qq.com", "1234");
发送Email服务
import lombok.RequiredArgsConstructor;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
@Service
@RequiredArgsConstructor
public class EmailServiceSmtpImpl implements EmailService
private final JavaMailSender emailMailSender;
public void send(String email, String message)
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setFrom("xxx@163.com");
simpleMailMessage.setTo(email);
simpleMailMessage.setSubject("Spring Security 登录验证码");
simpleMailMessage.setText("验证码为:" + message);
emailMailSender.send(simpleMailMessage);
以上是关于Spring中使用 网易邮箱 或 qq邮箱 发送信息的主要内容,如果未能解决你的问题,请参考以下文章