使用Spring Boot 发送邮件
1.在pom.xml中引入邮件模块
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2.在application.properties中增加如下配置
#MAIL (MailProperties)
spring.mail.default-encoding=UTF-8
#邮箱服务器地址(https://qiye.aliyun.com)
spring.mail.host=smtp.mxhichina.com
#端口 25, SSL 加密端口465
spring.mail.port=465
#用户名
spring.mail.username=yourname@cqxxs.cn
#密码
spring.mail.password=yourpassword
spring.mail.properties.mail.smtp.ssl.enable=true
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=false
spring.mail.properties.mail.smtp.starttls.required=false
3.编写发送邮件测试类MailTest
@RunWith(SpringRunner.class)
@SpringBootTest
public class MailTest {
@Autowired
private JavaMailSender mailSender;
@Test
public void sendMail(){
SimpleMailMessage mail = new SimpleMailMessage();
mail.setFrom("springboot@cqxxs.cn");
mail.setTo("cqxxs@qq.com");
mail.setSubject("Spring Boot Send Mail");
mail.setText("你好,你正在发送邮件。");
mailSender.send(mail);
}
}
4.发送
发送附件,发送html的邮件后续补充。