SpringBoot实现QQ邮件发送
-
建项目
- 创建一个SpringBoot项目
-
改pom,导入相关依赖
org.springframework.boot
spring-boot-starter-parent
2.2.2.RELEASE
<dependencies> <!--web依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--邮件发送依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
-
写Yml,配置application.yml
server:
port: 端口号spring:
mail:
#邮件发送配置
default-encoding: UTF-8
host: smtp.qq.com
# 授权码
password: 你的授权码
# 邮件发送安全配置
properties:
mail:
smtp:
auth: true
starttls:
enable: true
required: true
# 发件人信息
username: 发件人邮箱 -
主启动类EmailSignupApplication
/**
- @author QiuQiu&LL
- @create 2021-08-09 2:18
- @Description:
*/
@SpringBootApplication
@ComponentScan("com.qbb")
public class EmailSignupApplication {
public static void main(String[] args) {
SpringApplication.run(EmailSignupApplication.class, args);
}
}
-
业务
- 创建Service
/**
- @author QiuQiu&LL
- @create 2021-08-09 2:18
- @Description:
/
public interface MailService {
/*- 发送邮件
- @param to 邮件收件人
- @param subject 邮件主题
- @param verifyCode 邮件验证码
*/
public void sendVertifyCode(String to, String subject, String verifyCode);
}
- 实现类ServiceImpl
package com.qbb.email_signup.service.impl;
import com.qbb.email_signup.service.MailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.stereotype.Service;/**
-
@author QiuQiu&LL
-
@create 2021-08-09 2:20
-
@Description:
*/
@Service
public class MailServiceImpl implements MailService {@Value("${spring.mail.username}")
private String from;@Autowired
private JavaMailSender mailSender;Logger logger = LoggerFactory.getLogger(this.getClass());
/**
- 发送邮件
- @param to 邮件收件人
- @param subject 邮件主题
- @param verifyCode 邮件验证码
*/
@Override
public void sendVertifyCode(String to, String subject, String verifyCode) {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(from); //发送人
message.setTo(to); //收件人
message.setSubject(subject); //邮件名
message.setText(verifyCode); //邮件内容(验证码)
mailSender.send(message);
logger.info("已经发送");
}
}
-
测试
package com.qbb.email_signup;
import com.qbb.email_signup.service.MailService;
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;/**
-
@author QiuQiu&LL
-
@create 2021-08-09 2:28
-
@Description:
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = EmailSignupApplication.class)
public class MailServiceTest {@Autowired
private MailService mailService;@Test
public void Test1() {
/填你的测试信息/
String to = "收件人邮箱";
String title = "测试邮件";
String context = "测试验证码";
mailService.sendVertifyCode(to, title, context);
}
}
-
-
结果
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构