admin管理员组

文章数量:1559388

SpringBoot+JavaMailSender+ 腾讯企业邮箱配置

1. 引入spring-boot-starter-mail 依赖包

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

2. 在application.yml配置邮箱基本信息 

spring:
  mail:
    host: smtp.exmail.qq
    port: 465
    username: xxx@xx
    password: xxxx
    protocol: smtp
    properties:
      mail:
        smtp:
         auth: true
         ssl:
          enable: true
          socketFactory:
           class: com.sun.mail.util.MailSSLSocketFactory
           fallback: false

 

 3. 实现代码

        @Autowired

	JavaMailSender javaMailSender; 

	public void testSend() {

		SimpleMailMessage message = new SimpleMailMessage();

		message.setFrom("XXX@xxx"); //发送者邮箱地址  此地址一定要和yml邮箱一致

		message.setTo("xxx@xxx"); //收件人邮箱地址

		message.setSubject("测试主题");

		message.setText("测试内容");

		jms.send(message);

	}

 注意:

如果代码报:501 mail from address must be same as authorization user 错误  ;引起原因是yml中配置的邮箱地址和代码中message.setFrom(xx@xx);不一致导致;

 

本文标签: 腾讯企业邮箱SpringBootJavaMailSender