【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);
    }
}
相关推荐
RuoyiOffice几秒前
SpringBoot+Vue3+Uniapp实现PC+APP双端考勤打卡设计:GPS围栏/内网双模打卡、节假日方案、定时预生成——附数据结构和核心源码讲解
java·spring·小程序·uni-app·vue·产品运营·ruoyi
StackNoOverflow4 分钟前
Spring Boot 核心知识点总结
java·spring boot·后端
世界哪有真情5 分钟前
使用 Arthas 精准排查 SpringBoot 多模块项目中未使用的类(安全清理无用代码)
java·后端
softbangong12 分钟前
816-批量将图片分别转为pdf,文件夹下所有图片转为一个pdf
java·服务器·pdf·图片处理·图片转pdf·pdf工具·批量转换
玛卡巴卡ldf12 分钟前
【LeetCode 手撕算法】(矩阵)73-矩阵置零、54-螺旋矩阵(贪吃蛇)、48-旋转图像
java·数据结构·算法·leetcode·力扣
程序员小寒14 分钟前
JavaScript设计模式(四):发布-订阅模式实现与应用
开发语言·前端·javascript·设计模式
不吃香菜学java14 分钟前
苍穹外卖-新增套餐
java·spring boot·spring·tomcat·maven·mybatis
csbysj202014 分钟前
JSON 语法
开发语言
wangchunting15 分钟前
Spring Boot 概述
java·spring boot·后端
郝学胜-神的一滴17 分钟前
深入解析:生成器在UserList中的应用与Python可迭代对象实现原理
开发语言·python·程序人生·算法