【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);
    }
}
相关推荐
萧邀人7 分钟前
第一课、Cocos Creator 3.8 安装与配置
开发语言
前行的小黑炭16 分钟前
Android 协程的使用:结合一个环境噪音检查功能的例子来玩玩
android·java·kotlin
李少兄1 小时前
解决IntelliJ IDEA 提交代码时无复选框问题
java·ide·intellij-idea
Jayden_Ruan1 小时前
C++逆向输出一个字符串(三)
开发语言·c++·算法
不吃鱼的羊1 小时前
启动文件Startup_vle.c
c语言·开发语言
cyforkk1 小时前
Spring Boot @RestController 注解详解
java·spring boot·后端
VBA63371 小时前
VBA之Word应用第四章第二节:段落集合Paragraphs对象(二)
开发语言
叫我阿柒啊2 小时前
从Java全栈到前端框架:一次真实面试的深度复盘
java·spring boot·typescript·vue·database·testing·microservices
点云SLAM2 小时前
C++ 常见面试题汇总
java·开发语言·c++·算法·面试·内存管理
sniper_fandc2 小时前
IDEA修改系统缓存路径,防止C盘爆满
java·ide·intellij-idea