2011-04-14 63 views
0

我想用我的雅虎(smtp.mail.yahoo.com)帳戶的SMTP發送一封電子郵件到我的Gmail賬戶。獲取javax.mail.MessagingException和java.net.SocketException

但是我得到了異常。

javax.mail.MessagingException: Exception reading response; 
    nested exception is: 
     java.net.SocketException: Connection reset 
     at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2153) 
     at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1912) 
     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 javax.mail.Service.connect(Service.java:125) 
     at javax.mail.Transport.send0(Transport.java:194) 
     at javax.mail.Transport.send(Transport.java:124) 
     at Sendmail.postMail(Sendmail.java:40) 
     at Sendmail.main(Sendmail.java:49) 
Caused by: java.net.SocketException: Connection reset 
     at java.net.SocketInputStream.read(SocketInputStream.java:168) 
     at com.sun.mail.util.TraceInputStream.read(TraceInputStream.java:110) 
     at java.io.BufferedInputStream.fill(BufferedInputStream.java:218) 
     at java.io.BufferedInputStream.read(BufferedInputStream.java:237) 
     at com.sun.mail.util.LineInputStream.readLine(LineInputStream.java:89) 
     at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2131) 
     ... 9 more 
BUILD SUCCESSFUL (total time: 2 seconds) 


import javax.mail.*; 
import javax.mail.internet.*; 
import java.util.*; 

代碼:

public class Sendmail { 

    public void postMail(String recipients[ ], String subject, String message , String from) throws MessagingException { 

     boolean debug = false; 

     // Set the host smtp address 
     Properties props = new Properties(); 
     props.put("mail.smtp.host", "smtp.mail.yahoo.com"); 

     // create some properties and get the default Session 
     Session session = Session.getDefaultInstance(props, null); 
     session.setDebug(debug); 

     // create a message 
     Message msg = new MimeMessage(session); 

     // set the from and to address 
     InternetAddress addressFrom = new InternetAddress(from); 
     msg.setFrom(addressFrom); 

     InternetAddress[] addressTo = new InternetAddress[recipients.length]; 
     for (int i = 0; i < recipients.length; i++) { 
      addressTo[i] = new InternetAddress(recipients[i]); 
     } 
     msg.setRecipients(Message.RecipientType.TO, addressTo); 


     // Optional : You can also set your custom headers in the Email if you Want 
     msg.addHeader("MyHeaderName", "myHeaderValue"); 

     // Setting the Subject and Content Type 
     msg.setSubject(subject); 
     msg.setContent(message, "text/plain"); 
     Transport.send(msg); 
    } 

    public static void main(String[] args) { 

     try{ 

      Sendmail sende = new Sendmail(); 
      String senderemailid [] = {"[email protected]"}; 
      sende.postMail(senderemailid,"Hi","Come to room","[email protected]"); 

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

請提供導致此錯誤代碼(短)的例子。 – musiKk 2011-04-14 06:41:08

+0

最有可能您使用了錯誤的服務器:'mpandit-mbp:〜mpandit $ telnet smtp.mail.yahoo.com 25 嘗試74.6.228.71 ... telnet:連接到地址74.6.228.71:連接被拒絕 Trying 98.138.84.55 ... 連接到smtp.mail.us.am0.yahoodns.net。 轉義字符是'^]'。 220 smtp110.mail.ne1.yahoo.com ESMTP help 214 SMTP RFC:http:// tools.ietf.org/html/rfc2821' – lobster1234 2011-04-14 06:53:56

+0

對不起,格式混亂 - 沒有意識到我沒有把換行符 – lobster1234 2011-04-14 07:00:10

回答

0

嘗試設置mail.transport.protocol屬性

props.put("mail.transport.protocol", "smtp"); 
props.put("mail.smtp.port", "25"); 

你確實需要一些其他的一段代碼,雖然認證。雅虎要求您在使用他們的smtp服務之前進行身份驗證 - 他們不允許任何人使用他們的smtp服務器進行中繼

+0

如果我添加該語句..它顯示,javax.mail .MessagingException:無法連接到SMTP主機:smtp.mail.yahoo.com,端口:25; – Poornachandra 2011-04-14 07:11:27

+0

@Poornachandra,好吧,你然後錯過了這個props.put(「mail.smtp.port」,「25」); – Gilbeg 2011-04-15 03:49:04

1

smtp.mail.yahoo.net是一個CNAME,指向多個不同的郵件服務器。雅虎恰好在今天的郵件服務器上遇到了一些問題...

[email protected]:~$ telnet smtp.mail.yahoo.com 25 
Trying 98.136.185.95... 
Connected to smtp.mail.us.am0.yahoodns.net. 
Escape character is '^]'. 
Connection closed by foreign host. 

[email protected]:~$ telnet smtp.mail.yahoo.com 25 
Trying 98.138.84.55... 
Connected to smtp.mail.us.am0.yahoodns.net. 
Escape character is '^]'. 
220 smtp113.mail.ne1.yahoo.com ESMTP 

[email protected]:~$ telnet smtp.mail.yahoo.com 25 
Trying 98.139.212.139... 
Connected to smtp.mail.us.am0.yahoodns.net. 
Escape character is '^]'. 
Connection closed by foreign host. 

剛剛嘗試5次我得到3次失敗和2次連接。

+0

那你有什麼建議? – Poornachandra 2011-04-14 07:19:29

+0

抓住異常,然後重試......無論如何,你應該這樣做。 – 2011-04-14 07:20:07

+0

嗨,再次..我昨天試了整個解決這個異常..我發現了更多的東西..和更少的例外.. 1. javax.mail.MessagingException和java.net.SocketException可能是因爲窮人smtp服務器。 – Poornachandra 2011-04-15 06:06:17

0

這是我的代碼,以防萬一:

import java.util.*; 
    import javax.mail.*; 
    import javax.mail.internet.*; 
    import javax.activation.*; 

    public class SendEmail 
    { 
     public static void main(String [] args) 
     {  
      // Recipient's email ID needs to be mentioned. 
      String to = "[email protected]"; 

      // Sender's email ID needs to be mentioned 
      String from = "[email protected]"; 

      // Assuming you are sending email from localhost 
      String host = "localhost"; 

      // Get system properties 
      Properties properties = System.getProperties(); 

      // Setup mail server 
      properties.put("mail.transport.protocol", "smtp"); 
      properties.put("mail.smtp.port", "25"); 
      properties.put("mail.smtp.host", "smtp.mail.yahoo.com.net"); 

      // Get the default Session object. 
      Session session = Session.getDefaultInstance(properties); 

      try{ 
      // Create a default MimeMessage object. 
      MimeMessage message = new MimeMessage(session); 

      // Set From: header field of the header. 
      message.setFrom(new InternetAddress(from)); 

      // Set To: header field of the header. 
      message.addRecipient(Message.RecipientType.TO, 
             new InternetAddress(to)); 

      // Set Subject: header field 
      message.setSubject("This is the Subject Line!"); 

      // Now set the actual message 
      message.setText("This is actual message"); 

      // Send message 
      Transport.send(message); 
      System.out.println("Sent message successfully...."); 
      }catch (MessagingException mex) { 
      mex.printStackTrace(); 
      } //catch(Message) 
     } 
    } 
相關問題