Springboot发送邮件(2)——通过spring-boot-starter-mail

1.引入jar

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

2. application.yml配置

spring:
  mail:
    host: smtp.exmail.qq.com
    username: 用户名
    password: 密码
    properties:
      mail:
        smtp:
          auth: true  //一定要打开。不然会503错误,需要验证登录状态
          starttls:
            enable: true
            required: true

3. 发送邮件

public class MailTest {

	@Value("${spring.mail.username}")
	private String username;

	@Autowired
	private JavaMailSender mailSender;

	public void test() throws Exception {
		MimeMessage message = mailSender.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(message, true);
		helper.setFrom(new InternetAddress(username));
		helper.setTo("接收者邮箱");
		helper.setSubject("标题");
		helper.setText("邮件内容", true);
		mailSender.send(message);
	}
}

 

你可能感兴趣的:(邮件发送)