SpringBoot 腾讯企业邮箱
2017-06-18 21:24
127 查看
借鉴:http://www.cnblogs.com/LUA123/p/5575134.html
腾讯企业邮箱需要采用基于SSL的Mail登陆方式进行发送和接收电子邮件,并且需要设置企业邮箱的授权码。
在邮箱设置=》安全设置中,绑定微信获取微信的动态码,然后就可以添加授权码,授权码起到密码作用来登录邮箱。
application.properties配置文件:
测试类:
腾讯企业邮箱需要采用基于SSL的Mail登陆方式进行发送和接收电子邮件,并且需要设置企业邮箱的授权码。
在邮箱设置=》安全设置中,绑定微信获取微信的动态码,然后就可以添加授权码,授权码起到密码作用来登录邮箱。
application.properties配置文件:
#####企业邮箱##### spring.mail.account=***@*****.com spring.mail.pass=ewaOLNIhYXPklfqf spring.mail.from=***@*****.com spring.mail.host=smtp.exmail.qq.com spring.mail.port=465 spring.mail.protocol=smtp
测试类:
package com.qianbao.baihua.oms; import com.sun.mail.util.MailSSLSocketFactory; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import javax.mail.*; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.io.UnsupportedEncodingException; import java.security.GeneralSecurityException; import java.util.Date; import java.util.Properties; @RunWith(SpringRunner.class) @SpringBootTest(classes = Application.class) public class CompanyEmailUtils { public CompanyEmailUtils() { } @Value("${spring.mail.account}") private String account;//登录用户名 @Value("${spring.mail.pass}") private String pass; //登录密码 @Value("${spring.mail.from}") private String from; //发件地址 @Value("${spring.mail.host}") private String host; //服务器地址 @Value("${spring.mail.port}") private String port; //端口 @Value("${spring.mail.protocol}") private String protocol; //协议 //用户名密码验证,需要实现抽象类Authenticator的抽象方法PasswordAuthentication static class MyAuthenricator extends Authenticator { String u = null; String p = null; public MyAuthenricator(String u, String p) { this.u = u; this.p = p; } @Override public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(u, p); } } @Test public void send() { Properties prop = new Properties(); //协议 prop.setProperty("mail.transport.protocol", protocol); //服务器 prop.setProperty("mail.smtp.host", host); //端口 prop.setProperty("mail.smtp.port", port); //使用smtp身份验证 prop.setProperty("mail.smtp.auth", "true"); //使用SSL,企业邮箱必需! //开启安全协议 MailSSLSocketFactory sf = null; try { sf = new MailSSLSocketFactory(); sf.setTrustAllHosts(true); } catch (GeneralSecurityException e) { e.printStackTrace(); } prop.put("mail.smtp.ssl.enable", "true"); prop.put("mail.smtp.ssl.socketFactory", sf); Session session = Session.getDefaultInstance(prop, new MyAuthenricator(account, pass)); session.setDebug(true); MimeMessage mimeMessage = new MimeMessage(session); try { mimeMessage.setFrom(new InternetAddress(from, "XXX")); mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress("*******@qq.com")); mimeMessage.setSubject("XXX账户密码重置"); mimeMessage.setSentDate(new Date()); mimeMessage.setText("您在XXX使用了密码重置功能,请点击下面链接重置密码:\n" + "http://localhost:/XXX/ResetPassword?id=123456789"); mimeMessage.saveChanges(); Transport.send(mimeMessage); } catch (MessagingException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } }
相关文章推荐
- 第十八章 springboot + thymeleaf
- SpringBoot Velocity toolbox配置
- SpringBoot 启动慢(坑)
- SpringBoot笔记之四:持久化数据之Mysql
- spring boot与kafka集成的简单实例
- eclipse Springboot,Spring整合lombook
- Springboot starter web service consume a soap message
- Spring Boot 406(type=Not Acceptable, status=406)异常解决办法
- Spring Boot 官方文档学习(一)入门及使用
- 自己现实mybatis分页插件 整合springboot 运行
- Springboot starter web service produce a soap message
- Spring Boot 官方文档学习(一)入门及使用
- SpringBoot实践之---处理前端表单提交
- eayui结合Spring Boot实现客户端分页
- 微服务开发的入门级框架Spring Boot详解(二)
- 给你一份Spring Boot核心知识清单 推荐
- Spring-boot-devtools依赖 极大程度上方便了开发者
- spring-boot全局变量的声明
- spring-boot启动注解@EnableAutoConfiguration
- 【CSDN下载】第九期:springboot微信点餐系统代码 、中国区块链产业发展白皮书