2013-05-19 51 views
2

我正嘗試在eclipse IDE中使用java servlets發送電子郵件。 這是我的代碼。無法將套接字轉換爲TLS

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("******[email protected]")); 
      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); 
} 

這是執行後發生的異常。

異常Caught:javax.mail.MessagingException:無法將套接字轉換爲TLS; 嵌套的例外是: 產生java.io.IOException:服務器不被信任:smtp.gmail.com

+0

這將是很好得到更清楚一點異常消息 - 你可以嘗試添加'props.put( 「mail.smtp.debug」,「true」);'給你的道具編輯任何新東西? – ddmps

回答

5

通過設置「mail.smtp.ssl.trust」到「SMTPSERVER」你說你只相信服務器命名爲「smtpserver」。 「smtp.gmail.com」未被命名爲「smtpserver」。

0

如果你的環境是Android應用程序,那麼請確保您的Android設備的時間設置爲當前日期和時間

相關問題