2011-09-05 118 views
4

我的程序如下:發送郵件不能正常工作,並顯示異常

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 

/** 
* 
* @author Cygnet 
*/ 
    import java.util.Properties; 

    import javax.mail.Message; 
    import javax.mail.MessagingException; 
    import javax.mail.Session; 
    import javax.mail.Transport; 
    import javax.mail.Message.RecipientType; 
    import javax.mail.internet.AddressException; 
    import javax.mail.internet.InternetAddress; 
    import javax.mail.internet.MimeMessage; 
    public class SendMail { 
     private String from; 
     private String to; 
     private String subject; 
     private String text; 

     public SendMail(String from, String to, String subject, String text){ 
      this.from = from; 
      this.to = to; 
      this.subject = subject; 
      this.text = text; 
        System.out.println("your massege running here"); 
     } 

     public void send(){ 

      Properties props = new Properties(); 
      props.put("mail.smtp.host", "smtp.cygnet3.com"); 
      props.put("mail.smtp.port", "8383"); 

      Session mailSession = Session.getDefaultInstance(props); 
      Message simpleMessage = new MimeMessage(mailSession); 

      InternetAddress fromAddress = null; 
      InternetAddress toAddress = null; 
      try { 
       fromAddress = new InternetAddress(from); 
       toAddress = new InternetAddress(to); 
      } catch (AddressException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      try { 
       simpleMessage.setFrom(fromAddress); 
       simpleMessage.setRecipient(RecipientType.TO, toAddress); 
       simpleMessage.setSubject(subject); 
       simpleMessage.setText(text); 
       Transport.send(simpleMessage); 
      } catch (MessagingException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      }  
       } 
      public static void main(String[] args) {  
      String from = "[email protected]"; 
      String to = "[email protected]"; 
      String subject = "Hi server problem"; 
      String message = "I could not find anything in the coding"; 
      SendMail sendMail = new SendMail(from, to, subject, message); 
      sendMail.send(); 
        System.out.println("Your massege is successfully sent"); 
     }   

    } 

我得到以下異常:

run-main: 
your massege running here 
javax.mail.MessagingException: Exception reading response; 
    nested exception is: 
     java.net.SocketException: Connection reset 
     at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:1462) 
     at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1260) 
     at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370) 
     at javax.mail.Service.connect(Service.java:275) 
     at javax.mail.Service.connect(Service.java:156) 
     at javax.mail.Service.connect(Service.java:105) 
     at javax.mail.Transport.send0(Transport.java:168) 
     at javax.mail.Transport.send(Transport.java:98) 
     at SendMail.send(SendMail.java:58) 
Your massege is successfully sent 
     at SendMail.main(SendMail.java:71) 
Caused by: java.net.SocketException: Connection reset 
     at java.net.SocketInputStream.read(SocketInputStream.java:168) 
     at com.sun.mail.util.TraceInputStream.read(TraceInputStream.java:97) 
     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:75) 
     at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:1440) 
     ... 9 more 
BUILD SUCCESSFUL (total time: 2 minutes 15 seconds) 

請幫助我,我在做什麼錯誤?我不想做用戶名和密碼認證,我的端口號和用戶ID工作正常。

+0

*「你massege發送成功」 *你的消息拼寫錯誤。 –

+0

對於該錯誤感到抱歉 – user898489

+0

發佈電子郵件地址是獲取更多垃圾郵件的好方法,您可能不希望發生這種情況。 – Arjan

回答

1

「連接重置」意味着意外關閉了您嘗試建立的連接。也許你的SMTP服務器正在等待一個安全的連接(SSL或TLS)。你可以嘗試用telnet或netcat連接到smtp.cygnet3.com:8383,看看你得到了什麼。如果你有openssl,你可以檢查它是否是一個類似於openssl s_client -connect smtp.cygnet3.com:8383之類的安全端口。

編輯:正如Brian指出的那樣,端口8383是一個HTTP服務器。它看起來像郵件服務器的Web界面。嘗試使用標準端口25(不安全)或587(安全)。

+0

試過了,沒有快樂。 ::聳肩::儘管OP並沒有真正掌握核心概念,但我不知道它會有所幫助。 –

+0

@Brian:25,465和587都是開放的(對我來說)。 25上的netcat和465上的openssl都會返回一個看起來像SMTP的橫幅,但是對HELO或EHLO沒有反應...... –

+0

Heh ... *現在*他們是。當我評論時,他們不是。我責怪互聯網。 –

1

該端口上的主機上沒有運行SMTP服務器。您需要一個有效的SMTP服務器。

它似乎有在該端口上運行的Web服務器:

[email protected]:~$ telnet smtp.cygnet3.com 8383 
Trying 66.7.149.27... 
Connected to cygnet3.com. 
Escape character is '^]'. 
HELO? 
HTTP/1.1 400 Bad Request 
Content-Type: text/html; charset=us-ascii 
Server: Microsoft-HTTPAPI/2.0 
Date: Mon, 05 Sep 2011 05:35:35 GMT 
Connection: close 
Content-Length: 326 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd"> 
<HTML><HEAD><TITLE>Bad Request</TITLE> 
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD> 
<BODY><h2>Bad Request - Invalid Verb</h2> 
<hr><p>HTTP Error 400. The request verb is invalid.</p> 
</BODY></HTML> 
Connection closed by foreign host. 
+0

只是解釋我的事情。我無法理解 – user898489

+0

@ user898489:您正在使用錯誤的服務器/端口。您正試圖讓Web服務器發送電子郵件,這是行不通的。嘗試端口25,這是發送電子郵件的標準端口。 – Arjan

+0

什麼將是我的smtp域? – user898489