SpringBoot整合javaMailSender

1. 在Spring Boot的工程中的pom.xml中引入spring-boot-starter-mail依赖:


org.springframework.boot
spring-boot-starter-mail

2. 在完成了依赖引入之后,只需要在application.properties中配置相应的属性内容。

下面我们以QQ邮箱为例,在application.properties中加入如下配置

用户名就是qq邮箱名、密码是POP3/SMTP服务授权码,QQ邮箱POP3/SMTP服务开启详见开启smtp服务

如果使用腾讯企业邮箱host=smtp.exmail.qq.com,用户名是企业邮箱名,密码是企业邮箱密码:

#spring.mail.host=smtp.qq.com
#[email protected]
#spring.mail.password=zgbfeb1111
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
#端口使用默认端口为最好

3. 编写测试代码

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@EnableAutoConfiguration
public class ApplicationTests {
    @Autowired
    private JavaMailSender mailSender;
    @Test
    public void sendSimpleMail() throws Exception {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("[email protected]");//properties中配置的username
        message.setTo("[email protected]");//向谁发送
        message.setSubject("主题:简单邮件");
        message.setText("简单邮件内容from ");
        mailSender.send(message);
    }

4. OK完成,记得加入spring-boot的配置哦!

你可能感兴趣的:(spring-boot,SpringBoot,javaMailSender)