用springboot发送一个简单的邮件

本文详细介绍了如何在Spring Boot项目中配置和使用JavaMailSender发送邮件。通过pom.xml引入spring-boot-starter-mail依赖,设置application.properties中的邮件服务器参数,并在测试类中使用@SpringBootTest进行邮件发送测试。

pom.xml

<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>

在测试代码中


@SpringBootTest
class SpringbootTestApplicationTests {
    @Autowired
    JavaMailSenderImpl mailSender;


    @Test
    void contextLoads() {
        //一个简单的邮件
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setSubject("nihao");
        mailMessage.setText("xiexieni");
        mailMessage.setTo("1426386618@qq.com");
        mailMessage.setFrom("1426386618@qq.com");

        mailSender.send(mailMessage);
    }

}

application.properites

spring.mail.username=1426386618@qq.com
spring.mail.password=wnxlwhdpttwkfhhh
spring.mail.host=smtp.qq.com
#开启加密验证
spring.mail.properties.mail.smtp.ssl.enable=true

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值