SpringBoot发送邮件

0 导入发送邮件的依赖包

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-mail</artifactId>
</dependency>

1 获取邮件授权码

image

image

2 配置yml文件

spring:
  mail:
    #smtp服务主机  qq邮箱则为smtp.qq.com
    host: smtp.qq.com
    #服务协议
    protocol: smtp
    # 编码集
    default-encoding: UTF-8
    #发送邮件的账户
    username: xxxxxxx@163.com
    #授权码
    password: xxxxxx
    test-connection: true
    properties:
      mail:
        smtp:
          auth: true
          starttls:
            enable: true
            required: true

3 service代码

@SpringBootTest
class MsmServiceImplTest {
    @Autowired
    private JavaMailSender mailSender;

    @Test
    void mailTest() {
        // 创建简单邮件消息
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from); // 谁发的
        message.setTo(to); // 谁要接收
        message.setSubject(subject); // 邮件标题
        message.setText(test); // 邮件内容
        mailSender.send(message);
    }
}

image

posted @   xyzZero3  阅读(23)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示