java mail Received fatal alert: handshake_failure java 无法发送邮件问题 java 发送qq邮件(含源码)
java 无法发送邮件问题 java 发送qq邮件 报错:java mail Received fatal alert: handshake_failure (使用ssl)
javax.mail.MessagingException: Could not connect to SMTP host: smtp.qq.com, port: 465;
nested exception is:
javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1934)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:638)
at javax.mail.Service.connect(Service.java:295)
at javax.mail.Service.connect(Service.java:176)
at com.gzbugu.common.tools.ext.StringUtil.main(StringUtil.java:524)
Caused by: javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
at sun.security.ssl.Alerts.getSSLException(Unknown Source)
at sun.security.ssl.Alerts.getSSLException(Unknown Source)
at sun.security.ssl.SSLSocketImpl.recvAlert(Unknown Source)
at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:507)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:238)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1900)
... 4 more
找了半天终于解决了:
换掉本地jdk(安装目录jre\lib\security里的local_policy.jar,US_export_policy.jar)里面有一个jce的包,安全性机制导致的访问https会报错。
如本人安装的jdk需要替换的地方:1. C:\Program Files\Java\jdk1.8.0_102\jre\lib\security 2. C:\Program Files\Java\jre1.8.0_102\lib\security
下载地址: http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html
目录 %JAVA_HOME%\jre\lib\security里的local_policy.jar,US_export_policy.jar
源码:
public static void main(String[] args) {
try {
//这里使用的是qq邮箱发送
String email="xxxxxxx@qq.com";
String pwd="cgXXXXwqfaakcjgf";//非QQ邮箱密码;是qq邮箱安全码
String toemail="xxxxxx@qq.com";//接收的邮箱
Properties props = new Properties();
// 开启debug调试
props.setProperty("mail.debug", "true");
// 发送服务器需要身份验证
props.setProperty("mail.smtp.auth", "true");
// 设置邮件服务器主机名
props.setProperty("mail.host", "smtp.qq.com");
// 发送邮件协议名称
props.setProperty("mail.transport.protocol", "smtp");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.ssl.socketFactory", sf);
Session session = Session.getInstance(props);
session.setDebug(true);
Message msg = new MimeMessage(session);
msg.setSubject("http://www.cnblogs.com/qgc88");
StringBuilder builder = new StringBuilder();
builder.append("http://www.cnblogs.com/qgc88 " );
builder.append("http://www.cnblogs.com/qgc88");
builder.append("\n时间 " + new Date());
msg.setText(builder.toString());
msg.setFrom(new InternetAddress(email));//"**发送人的邮箱地址**"
Transport transport = session.getTransport();
transport.connect("smtp.qq.com", email, pwd);
transport.sendMessage(msg, new Address[] { new InternetAddress(toemail) });//"**接收人的邮箱地址**"
transport.close();
} catch (Exception e) {
e.printStackTrace();
}
}