Spring Boot 发送邮件

使用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.发送
邮件发送成功.png

发送附件,发送html的邮件后续补充。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容