您的位置:首页 > 编程语言 > Java开发

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

2016-10-19 14:13 771 查看
一、邮件发送

1、在pom文件中添加依赖:

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


2、在application.properties中添加配置

#spring boot mail configure
spring.mail.host=smtp.qq.com
spring.mail.username=2973821510@qq.com #此用户名不是qq号,而是邮箱地址
spring.mail.password=iipjvyzafytndfga
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  邮件 java springboot