springboot邮件发送功能和两个常见问题

来源:互联网 发布:济宁网络问政平台app 编辑:程序博客网 时间:2024/06/05 14:00

一、邮件发送
1、在pom文件中添加依赖:

        <!--spring boot mail-->        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-mail</artifactId>        </dependency>

2、在application.properties中添加配置

#spring boot mail configurespring.mail.host=smtp.qq.comspring.mail.username=2973821510@qq.com #此用户名不是qq号,而是邮箱地址spring.mail.password=iipjvyzafytndfgaspring.mail.properties.mail.smtp.auth=truespring.mail.properties.mail.smtp.starttls.enable=truespring.mail.properties.mail.smtp.starttls.required=true

3、打开qq邮箱,配置(你可能会出现的问题一)
这里写图片描述
如果你是用的是163邮箱,也可根据提示保存授权码.

4、即可编写发送邮件代码

/** * Created by pengkai * @date 2016/10/19 0019. * @mail pengxiankaikai@163.com */@RunWith(SpringJUnit4ClassRunner.class)@SpringApplicationConfiguration(Application.class)public class mailTest {    @Resource    private JavaMailSender javaMailSender;    @Test    public void sendmail(){        SimpleMailMessage message = new SimpleMailMessage();        message.setFrom("xx@qq.com");        message.setTo("xx@163.com");        message.setSubject("您好,我们可以做朋友吗");        message.setText("哈哈,是不是傻");        javaMailSender.send(message);    }}
测试,如果你是用的是163或者sina邮箱,应该直接就能成功;如果你是用的是qq邮箱,那么你可能就会出现另一个问题就是握手失败将出现如下异常信息:
nested exception is: javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
找了些资料发现,问题出自jdk。在jdk的jre中local_policy.jar和US_export_policy.jar包,由于安全机制导致的访问出错。解决方法:oracle中下载这两个包替换掉下载地址:[这里写链接内容](http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html)

这里写图片描述

替换jdk安装目录下的jdk安装路径 \jre\lib\security下的两个文件即可,替换后再次运行。即可成功。。。good luck。

这里写图片描述

ending

0 0
原创粉丝点击