美文网首页
Spring Boot 发送邮件

Spring Boot 发送邮件

作者: Xiong忄s | 来源:发表于2017-11-21 21:41 被阅读261次

    使用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的邮件后续补充。

    相关文章

      网友评论

          本文标题:Spring Boot 发送邮件

          本文链接:https://www.haomeiwen.com/subject/solgvxtx.html