为了防止垃圾邮件泛滥,阿里云ECS 基于安全考虑,目前已禁用 25 端口,不再开放smtp邮件的25端口,现在需要使用465/587端口进行smtp邮件发送。具体操作步骤如下:
1、QQ企业邮箱开启开启IMAP/SMTP服务
2、springboot引入mail包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>3、application配置
spring: mail: default-encoding: UTF-8 host: smtp.exmail.qq.com port: 587 username: ***@qq.com password: ******** properties: mail: smtp: auth: true timeout: 250004、代码示例
-
@RunWith(SpringJUnit4ClassRunner.class) -
@SpringApplicationConfiguration(classes = Application.class) -
public class ApplicationTests { -
-
@Autowired -
private JavaMailSender mailSender; -
-
@Test -
public void sendSimpleMail() throws Exception { -
SimpleMailMessage message = new SimpleMailMessage(); -
message.setFrom("[email protected]"); -
message.setTo("[email protected]"); -
message.setSubject("主题:简单邮件"); -
message.setText("测试邮件内容"); -
-
mailSender.send(message); -
} -
-
}