springboot框架使用腾讯邮箱自动发送邮件

配置腾讯邮箱获取邮箱连接密码

腾讯邮箱–>设置–>账户–>打开POP3/SMTP服务–>获取授权码

SpringBoot配置

POM文件

<dependency>
	<groupId>org.springframework.bootgroupId>
	<artifactId>spring-boot-starter-mailartifactId>
dependency>

application.properties配置

#这里指明发送方的邮箱
[email protected]

#password填我们刚刚说的授权码
spring.mail.password=你自己获取的授权码
spring.mail.protocol=smtp
spring.mail.default-encoding=utf-8

#下面几句是必须
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true

spring.application.name=spirng-boot-mail
spring.mail.host=smtp.qq.com
默认的邮箱服务器端口
spring.mail.port=25

springboot测试程序

service层

@Service
public class MailService {
    @Autowired
    private JavaMailSender javaMailSender;

    public void sendMail(String to, String subject, String content) {
        SimpleMailMessage mailMessage=new SimpleMailMessage();
        mailMessage.setFrom("[email protected]");//发起者
        mailMessage.setTo(to);//接受者
        //多人mailMessage.setTo("1xx.com","2xx.com","3xx.com");
        mailMessage.setSubject(subject);
        mailMessage.setText(content);
        try {
            javaMailSender.send(mailMessage);
            System.out.println("发送邮件成功");
        }catch (Exception e){
            System.out.println("发送邮件失败");
        }
    }
}

测试

 @Test
        public  void sendMailTest(){
            mailService.sendMail("xxx.com","这里是邮件的主题","助理是邮件的内容");//第一个参数是邮件的接收方
    }

你可能感兴趣的:(smtp,spring,boot)