admin管理员组文章数量:1559073
借鉴:http://wwwblogs/lua123/p/5575134.html
腾讯企业邮箱需要采用基于ssl的mail登陆方式进行发送和接收电子邮件,并且需要设置企业邮箱的授权码。
在邮箱设置=》安全设置中,绑定微信获取微信的动态码,然后就可以添加授权码,授权码起到密码作用来登录邮箱。
application.properties配置文件:
#####企业邮箱#####
spring.mail.account=***@*****
spring.mail.pass=ewaolnihyxpklfqf
spring.mail.from=***@*****
spring.mail.host=smtp.exmail.qq
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"));
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
九游会下载的版权声明:本文标题:springboot 腾讯企业邮箱 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/xitong/1727422253a1113711.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论