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();
        }
       }

java mail Received fatal alert: handshake_failure java 无法发送邮件问题 java 发送qq邮件(含源码)的更多相关文章

  1. 解决idea gradle构建Received fatal alert: handshake_failure问题

    Gradle是一款强大的构建工具,但是搭建项目运行环境总是非常头痛,各种网络原因会导致项目不能成功的导入. 说一下这个问题的解决办法,折腾了很久终于解决了. javax.net.ssl.SSLHand ...

  2. jdk1.7访问https报javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure问题解决

    本地jdk版本java version "1.8.0_31",代码中已对https做了相应处理:信任所有来源证书,运行正常:上包到服务器(服务器jdk版本java version ...

  3. 转:javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure 解决方案

    转:javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure 解决方案javax.net.ssl.SSL ...

  4. Eclipse4.6安装Tomcat插件时报错:Unable to read repository at http://tomcatplugin.sf.net/update/content.xml. Received fatal alert: handshake_failure

    错误如下: Unable to read repository at http://tomcatplugin.sf.net/update/content.xml.Received fatal aler ...

  5. 处理Https 异常记录 javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure

    http://blog.csdn.net/baidu_18607183/article/details/51595330 https://blogs.oracle.com/java-platform- ...

  6. javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure

    SSL握手失败:用JDK1.8做发邮件的功能遇到这种问题处理方式是:将目录 %JAVA_HOME%\...\jre\lib\security里的local_policy.jar,US_export_p ...

  7. javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure 解决方案

    这个是jdk导致的,jdk里面有一个jce的包,安全性机制导致的访问https会报错,官网上有替代的jar包,换掉就好了 目录 %JAVA_HOME%\jre\lib\security里的local_ ...

  8. Centos7编译hadoop异常:Received fatal alert: handshake_failure

    保持网络畅通 或者 配置代理 能够访问cdh的仓库 https://repository.cloudera.com/artifactory/cloudera-repos/ 编译hadoop版本 had ...

  9. SSL异常javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure

    jdk 7 http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html jdk 8 http: ...

随机推荐

  1. Java--equals和 == 的比较和equals()、HashCode()的重写

    一. equals和 == 的比较 1.== 运算符 ① == 如果比较的是基本数据类型,则比较的是值. ② == 如果比较的是引用数据类型,则比较的是地址值. 2.equals ①它属于java.l ...

  2. Python中类的声明,使用,属性,实例属性,计算属性及继承,重写

    Python中的类的定义以及使用: 类的定义: 定义类 在Python中,类的定义使用class关键字来实现 语法如下: class className: "类的注释" 类的实体 ...

  3. GoF23种设计模式之行为型模式之策略模式

    传送门 ☞ 轮子的专栏 ☞ 转载请注明 ☞ http://blog.csdn.net/leverage_1229 1概述           定义一系列算法,把它们一个个都封装起来,并且让它们可以相互 ...

  4. poj 3280 回文字符串问题 dp算法

    题意:给一个字符串,构成回文(空也是回文) 其中增删都需要代价.问:代价最少? 思路:把字符串s变空  dp[i][j]表示变成回文的最小代价 for(i=m-1;i>=0;--i)       ...

  5. category常量及对应字符串

  6. luogu2754 星际转移问题

    源向地球连 月球向汇连 每一天往下一天连 飞船上一天与这一天连 枚举答案 #include <iostream> #include <cstring> #include < ...

  7. Python全栈开发第二期课表

     day01-python 全栈开发-基础篇                 01 开课介绍 01:55:13 ★  02 开课介绍02 01:28:31 ★  03 开课介绍03 00:22:55 ...

  8. C语言变量长度在32位和64位处理器上的关系

    C语言变量长度在32位和64位处理器上的关系       理论上来讲 我觉得数据类型的字节数应该是由CPU决定的,但是实际上主要由编译器决定(占多少位由编译器在编译期间说了算).常用数据类型对应字节数 ...

  9. python递归深度报错--RuntimeError: maximum recursion depth exceeded

    当你的程序递归的次数超过999次的时候,就会引发RuntimeError: maximum recursion depth exceeded. 解决方法两个: 1.增加系统的递归调用的次数: impo ...

  10. 【java基础 13】两种方法判断hashmap中是否形成环形链表

    导读:额,我介绍的这两种方法,有点蠢啊,小打小闹的那种,后来我查了查资料,别人都起了好高大上的名字,不过,本篇博客,我还是用何下下的风格来写.两种方法,一种是丢手绢法,另外一种,是迷路法. 这两种方法 ...