2013-03-13 105 views
15

在發送電子郵件我得到這個錯誤無法連接到SMTP主機:smtp.gmail.com端口:465,響應:-1

java.lang.RuntimeException: javax.mail.SendFailedException: Sending failed; nested exception is: class javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1

我的代碼是:

Properties props = new Properties(); 
props.put("mail.smtp.host", "smtp.gmail.com"); 
props.put("mail.smtp.starttls.enable","true"); 
props.put("mail.smtp.socketFactory.port", "465"); 
props.put("mail.smtp.auth", "true"); 
props.put("mail.smtp.port", "465"); 

Session session = Session.getDefaultInstance(props, 
     new javax.mail.Authenticator() { 
       protected PasswordAuthentication getPasswordAuthentication() { 
         return new PasswordAuthentication("email","password"); 
       } 
     }); 

try { 

     Message message = new MimeMessage(session); 
     message.setFrom(new InternetAddress("email")); 
     message.setRecipients(Message.RecipientType.TO, 
         InternetAddress.parse(this.to)); 
     message.setSubject("Testing"); 
     message.setText("Hey, this is the testing email."); 



     Transport.send(message); 

任何幫助,將不勝感激。

在此先感謝。

+0

檢查http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/ – Abi 2013-03-13 06:14:53

+0

我試過相同的代碼,但仍然得到錯誤:javax.mail.MessagingException :530 5.7.0必須首先發出STARTTLS命令。 4sm28216799pbn.23 - gsmtp \t在com.sun.mail.smtp.SMTPTransport.issueCommand(SMTPTransport.java:1020)在com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:716) \t在COM。 sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:388) \t at rapid.mail.main(mail.java:60)starttls的這一行是那裏,那麼爲什麼得到相同的錯誤:properties.put(「mail .smtp.starttls.enable「,」true「); – user1900376 2013-03-13 06:23:11

+0

嘗試使用最新的jar版本JavaMail 1.4.7 – Abi 2013-03-13 06:36:11

回答

11

你需要告訴它你使用SSL:

props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 

如果你錯過了什麼,這裏是工作代碼:

String d_email = "[email protected]", 
      d_uname = "Name", 
      d_password = "urpassword", 
      d_host = "smtp.gmail.com", 
      d_port = "465", 
      m_to = "[email protected]", 
      m_subject = "Indoors Readable File: " + params[0].getName(), 
      m_text = "This message is from Indoor Positioning App. Required file(s) are attached."; 
    Properties props = new Properties(); 
    props.put("mail.smtp.user", d_email); 
    props.put("mail.smtp.host", d_host); 
    props.put("mail.smtp.port", d_port); 
    props.put("mail.smtp.starttls.enable","true"); 
    props.put("mail.smtp.debug", "true"); 
    props.put("mail.smtp.auth", "true"); 
    props.put("mail.smtp.socketFactory.port", d_port); 
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
    props.put("mail.smtp.socketFactory.fallback", "false"); 

    SMTPAuthenticator auth = new SMTPAuthenticator(); 
    Session session = Session.getInstance(props, auth); 
    session.setDebug(true); 

    MimeMessage msg = new MimeMessage(session); 
    try { 
     msg.setSubject(m_subject); 
     msg.setFrom(new InternetAddress(d_email)); 
     msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to)); 

Transport transport = session.getTransport("smtps"); 
      transport.connect(d_host, Integer.valueOf(d_port), d_uname, d_password); 
      transport.sendMessage(msg, msg.getAllRecipients()); 
      transport.close(); 

     } catch (AddressException e) { 
      e.printStackTrace(); 
      return false; 
     } catch (MessagingException e) { 
      e.printStackTrace(); 
      return false; 
     } 
+0

我得到異常。你能告訴我它失敗的位置嗎 javax.mail.SendFailedException:發送失敗; 嵌套的異常是: \t class javax.mail.MessagingException:無法連接到SMTP主機:smtp.gmail.com,端口:587; 嵌套的異常是: \t java.net.ConnectException:連接超時:連接 \t at javax.mail.Transport.send0(Transport。的java:218) \t在javax.mail.Transport.send(Transport.java:80) \t在sendMail.SendMail4.execute(SendMail4.java:70) \t在sendMail.sel.main(sel.java:10 ) – 2016-02-23 10:58:39

+0

謝謝。我不知道'd_uname'字段實用程序是什麼。另一方面,在連接指令中,我們應該傳遞電子郵件而不是用戶名:'transport.connect(d_host,Integer.valueOf(d_port),d_email,d_password);'。另外,你最好從代碼中的'SMTPAuthenticator'類中顯示包,或者在答案中指定所需的API。 – Omar 2016-04-16 19:46:36

5

我有在使用NetBeans進行調試時遇到此問題,甚至執行實際的jar文件。防病毒會阻止發送電子郵件。您應該在調試期間臨時禁用您的防病毒軟件,或者排除NetBeans和實際的jar文件被掃描。就我而言,我正在使用Avast。

參見如何排除此鏈接:How to Add File/Website Exception into avast! Antivirus 2014

這對我的作品。

+0

您能否更詳細地描述SMTP響應和防病毒之間的連接? – carlodurso 2014-10-25 18:39:43

+0

謝謝 - Avast也造成了我的'問題'... – DaniEll 2015-06-18 17:20:53

+1

@carlodurso:防病毒,(在我的情況下,AVAST)具有阻止程序的功能,這些程序使用端口進出的懷疑是安全威脅。所以最好排除你的jar文件被阻止。 – ronIT 2015-10-21 20:36:40

1

我做什麼我註釋掉

props.put("mail.smtp.starttls.enable","true"); 

因爲顯然是G-郵件,你並不需要它。如果你還沒有這樣做,你需要在你的程序的G-mail中創建一個應用程序密碼。我做到了這一點,它的工作完美。這裏的這個鏈接會告訴你如何:https://support.google.com/accounts/answer/185833

相關問題