【问题标题】:Could Not convert socket to TLS无法将套接字转换为 TLS
【发布时间】:2015-08-12 13:49:09
【问题描述】:

我正在尝试在 Eclipse IDE 中使用 java servlet 发送电子邮件。 这是我的代码。

    final String username = "******@gmail.com";
    final String password = "******";

    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "587");
    props.put("mail.smtp.ssl.trust", "smtpserver");

    Session session1 = Session.getInstance(props, new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
      });

    try 
    {
        if(result)
        {

            Message message = new MimeMessage(session1);
            message.setFrom(new InternetAddress("******60@gmail.com"));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email));

            message.setSubject("Welcome To Our Bank");
            message.setText("Dear "+custname+","
                    +"\n\n Your Account has been Created Successfully."
                    +"\n\n Your Account Details Are:"
                    +"\n   User Id : "+userid+""
                    +"\n   Account Number : "+accno+""
                    +"\n   Login Password : "+passwd+""
                    +"\n   Transaction Password : "+t_passwd+"");

            Transport.send(message);
            out.println("mail sent");
        }
    } 
    catch(MessagingException e) 
    {
        out.println("Exception Caught : "+e);
}

这是执行后发生的异常。

异常捕获:javax.mail.MessagingException:无法将套接字转换为 TLS; 嵌套异常是: java.io.IOException:服务器不受信任:smtp.gmail.com

【问题讨论】:

  • 如果能得到更清晰的异常消息会很好 - 您可以尝试将props.put("mail.smtp.debug", "true"); 添加到您的道具并编辑任何新内容吗?

标签: java ssl jakarta-mail


【解决方案1】:

通过将“mail.smtp.ssl.trust”设置为“smtpserver”,您已经说过您只信任名为“smtpserver”的服务器。 “smtp.gmail.com”未命名为“smtpserver”。

【讨论】:

    【解决方案2】:

    如果您的上下文是 android 应用程序,请确保您的 android 设备时间设置为当前日期和时间

    【讨论】:

      猜你喜欢
      • 2021-10-06
      • 2023-03-08
      • 2013-04-13
      • 1970-01-01
      • 2013-06-22
      • 2016-11-13
      • 1970-01-01
      • 1970-01-01
      • 2021-10-26
      相关资源
      最近更新 更多