zoukankan      html  css  js  c++  java
  • JavaMailSender怎么发送163和qq邮件

    https://blog.csdn.net/Tracycater/article/details/73441010

     

    引入Maven依赖包

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>
    1
    2
    3
    4
    163邮箱

    application.properties

    #####163邮箱########
    spring.mail.host=smtp.163.com
    spring.mail.username=*****@163.com
    #163邮箱密码
    spring.mail.password=!@#$%^&*
    spring.mail.properties.mail.smtp.auth=true
    spring.mail.properties.mail.smtp.starttls.enable=true
    spring.mail.properties.mail.smtp.starttls.required=true
    1
    2
    3
    4
    5
    6
    7
    8
    运行类:

    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.mail.SimpleMailMessage;
    import org.springframework.mail.javamail.JavaMailSender;
    import org.springframework.test.context.junit4.SpringRunner;

    @RunWith(SpringRunner.class)
    @SpringBootTest(classes=Application.class)
    public class My163MailTest {

    @Autowired
    private JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    private String username;

    @Test
    public void testSendSimple() {
    SimpleMailMessage message = new SimpleMailMessage();
    message.setFrom(username);
    message.setTo("*******@qq.com");
    message.setSubject("标题:测试标题");
    message.setText("测试内容部份");
    javaMailSender.send(message);
    }
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    QQ邮箱和163邮箱的区别是需要设置授权码而不是密码,具体操作参考:
    http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256

    application.properties

    ######qq邮箱########
    spring.mail.host=smtp.qq.com
    spring.mail.username=******@qq.com
    #QQ邮箱授权码
    spring.mail.password=xuojxtkdojvzbhjj
    spring.mail.properties.mail.smtp.auth=true
    spring.mail.properties.mail.smtp.starttls.enable=true
    spring.mail.properties.mail.smtp.starttls.required=true
    1
    2
    3
    4
    5
    6
    7
    8
    运行类:

    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.mail.SimpleMailMessage;
    import org.springframework.mail.javamail.JavaMailSender;
    import org.springframework.test.context.junit4.SpringRunner;

    @RunWith(SpringRunner.class)
    @SpringBootTest(classes=Application.class)
    public class MyQQMailTest {

    @Autowired
    private JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    private String username;

    @Test
    public void testSendSimple() {
    SimpleMailMessage message = new SimpleMailMessage();
    message.setFrom(username);
    message.setTo("******@qq.com");
    message.setSubject("标题:测试标题");
    message.setText("测试内容部份");
    javaMailSender.send(message);
    }
    }
    ---------------------
    作者:罗罗诺亚-小鱼
    来源:CSDN
    原文:https://blog.csdn.net/Tracycater/article/details/73441010
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    党史回顾+十九大习思想+中国历史事件+地理+航天史及古代天文历法
    国际组织
    生活-常识-物理
    生物
    数量题目总结-利润排列组合
    数量题目总结-工程最值集合
    考公错题记录表
    个人向简历介绍
    数据库操作大全
    团队博客作业-技术规格说明书
  • 原文地址:https://www.cnblogs.com/a1304908180/p/10224066.html
Copyright © 2011-2022 走看看