容易碎的除了梦想,还有蛋!我们做产品的,对程序的要求极其严格。比如小编最近就遇到客户要求我们的ERP、OA等产品支持他们的邮箱用户登录。
Web应用支持第三方认证时很常见的功能,除了邮箱认证,还有手机短信认证,AD域认证,Radius认证,微信认证等。同样的,这样的框架也有很多,比如:spring security,还有OAuth第三方授权等。
好了,不扯了,回到今天的话题。让我们的网站实现第三方邮箱登录功能。完整实现代码如下:
package test;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
public class MailTest {
public static void main(String[] args) {
// 初始化props
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.qq.com");
props.put("mail.smtp.auth", "true");
// 验证
try {
Authenticator authenticator = new Authenticator() {
protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
/**
* :www.xttblog.com
* 账号密码
*/
return new javax.mail.PasswordAuthentication("1234567@qq.com","123456");
}
};
Session session=Session.getInstance(props, authenticator);
Transport trans = null;
try {
trans = session.getTransport("smtp");
trans.connect();
// 连接服务器
} catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
System.out.println("验证成功");
} catch (Exception e) {
e.printStackTrace();
}
}
}
注意,要登录的邮箱必须开启对应的smtp,pop3等协议。
版权声明:本文为博主原创文章,未经博主允许不得转载。原文地址:http://www.xttblog.com/?p=454

: » 让网站集成第三方邮箱登录功能
原创文章,作者:506227337,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/251438.html