【Java发送邮箱】spring boot 发送邮箱

导入依赖

bash 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

2.在properties配置邮箱

bash 复制代码
# 发件人QQ号
spring.mail.username=2508575653@qq.com
# QQ邮箱授权码
spring.mail.password=xxxxxxxxxxxxxxx
# 主机
spring.mail.host=smtp.qq.com
# qq邮箱需要开启安全连接
spring.mail.properties.mail.stmp.ssl.enable=true

3.获取QQ邮箱授权码

打开网页版的QQ邮箱,登录邮箱,进入设置-》帐户

然后,在"帐户"设置中,找到服务设置项,进行设置,如下:

开启POP3/SMTP服务器,验证密保

用正确的手机好发送正确的验证内容到指定的号码,成功获取授权码

测试代码

java 复制代码
package com.peng;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
@SpringBootTest
class SpringbootSecurityApplicationTests {
    @Autowired
    private JavaMailSenderImpl javaMailSender;
    @Test
    void contextLoads() {
        //简单邮件
        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject("测试");//主题
        message.setText("邮箱内容");
      、//接受者邮箱(任何邮箱都可以)
        message.setTo("2508575653@qq.com");
      //发送者邮箱
        message.setFrom("2508575653@qq.com");
        javaMailSender.send(message);
    }
    @Test
    void test() throws MessagingException {
        //复杂邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);//组装,开启之后可以添加附件
        helper.setSubject("ok");//主题
        helper.setText("内容");
        helper.addAttachment("按什么名称发送.后缀",new File("文件路径"));
        helper.setTo("收件人");
        helper.setFrom("发件人");
        javaMailSender.send(mimeMessage);
    }
}
相关推荐
星空露珠几秒前
时间罗盘小界面模组
开发语言·数据结构·算法·游戏·lua
DeniuHe1 分钟前
C++实现在数组中找到重复元素及其出现的次数。
开发语言·c++·哈希算法
Byron Loong2 分钟前
【Python】Pytorch是个什么包
开发语言·pytorch·python
qq_5470261794 分钟前
多版本 JDK 安装与配置
java·开发语言
shbelec16 分钟前
实邦电子如何确保电子产品开发质量与可靠性?
开发语言
we have a whole life18 分钟前
Golang(Handler入门)
开发语言·http·golang
韩立学长21 分钟前
基于Springboot就业岗位推荐系统a6nq8o76(程序、源码、数据库、调试部署方案及开发环境)系统界面展示及获取方式置于文档末尾,可供参考。
数据库·spring boot·后端
lkbhua莱克瓦2422 分钟前
进阶-索引5-设计原则
开发语言·数据库·笔记·mysql·索引·存储引擎
木木木一30 分钟前
Rust学习记录--C3 Rust通用编程概念
开发语言·学习·rust
2501_9417987331 分钟前
面向微服务动态限流与服务熔断的互联网系统高可用设计与多语言工程实践分享
java·大数据·开发语言