2013-02-23 93 views
0

我可以發送郵件給自己(不通過intertubes發送),但似乎無法使用Java複製如下。通過telnet Postfix的行爲罰款:爲什麼我得到java.net.ConnectException:連接被拒絕?

[email protected]:~$ 
[email protected]:~$ telnet localhost 25 
Trying 127.0.0.1... 
Connected to localhost. 
Escape character is '^]'. 
220 dur.bounceme.net ESMTP Postfix (Ubuntu) 
helo me 
250 dur.bounceme.net 
mail from:<[email protected]> 
250 2.1.0 Ok 
rcpt to:<[email protected]> 
250 2.1.5 Ok 
data 
354 End data with <CR><LF>.<CR><LF> 
subject: hmm, is this smtp? 
how do I do this from Java? 
there's no password... 
postfix-dovecot 
. 
250 2.0.0 Ok: queued as 81A9717FEB0 
quit 
221 2.0.0 Bye 
Connection closed by foreign host. 
[email protected]:~$ 

以下是一個Java堆棧跟蹤,所有我能看到的是,它的java.net.ConnectException: Connection refused但我不知道爲什麼:

Feb 23, 2013 3:03:29 AM net.bounceme.dur.nntp.MessageSender send 
SEVERE: {0} 
javax.mail.MessagingException: Could not connect to SMTP host: dur.bounceme.net, port: 25; 
    nested exception is: 
    java.net.ConnectException: Connection refused 
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1962) 
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654) 
    at javax.mail.Service.connect(Service.java:295) 
    at net.bounceme.dur.nntp.MessageSender.send(MessageSender.java:57) 
    at net.bounceme.dur.nntp.MessageSender.populate(MessageSender.java:46) 
    at net.bounceme.dur.nntp.MessageSender.<init>(MessageSender.java:30) 
    at net.bounceme.dur.nntp.ArticleReader.<init>(ArticleReader.java:28) 
    at net.bounceme.dur.nntp.Driver.<init>(Driver.java:13) 
    at net.bounceme.dur.nntp.Driver.main(Driver.java:17) 
Caused by: java.net.ConnectException: Connection refused 
    at java.net.PlainSocketImpl.socketConnect(Native Method) 
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339) 
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200) 
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182) 
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391) 
    at java.net.Socket.connect(Socket.java:579) 
    at java.net.Socket.connect(Socket.java:528) 
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:321) 
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:237) 
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1928) 
    ... 8 more 

BUILD SUCCESSFUL (total time: 3 seconds) 

相關代碼:

package net.bounceme.dur.nntp; 

import java.util.logging.Level; 
import java.util.logging.Logger; 
import java.util.Properties; 
import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.NoSuchProviderException; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 

public class MessageSender { 

    private final static Logger LOG = Logger.getLogger(MessageSender.class.getName()); 
    private String header; 
    private String body; 
    private Properties p; 
    private Session session; 
    private MimeMessage message; 

    private MessageSender() { 
    } 

    public MessageSender(Properties p, String... s) throws Exception { 
     header = s[0]; 
     body = s[1]; 
     this.p = p; 
     populate(); 
    } 

    private void populate() throws Exception { 
     String lines[] = header.split("\\n"); 
     session = Session.getDefaultInstance(p, null); 
     message = new MimeMessage(session); 
     LOG.fine("\n\n\n\nnew message************\n\n\n\n"); 

     for (String s : lines) { 
      message.addHeaderLine(s); 
     } 
     message.setContent(message, body); 
     String recipient = p.getProperty("recipient"); 
     message.setRecipients(Message.RecipientType.TO, 
       InternetAddress.parse(recipient)); 
     send(); 
    } 

    private void send() { 
     try { 
      String protocol = p.getProperty("protocol"); 
      String host = p.getProperty("host"); 
      int port = Integer.valueOf(p.getProperty("port")); 
      String username = p.getProperty("username"); 
      String password = p.getProperty("password"); 
      Transport transport = session.getTransport(protocol); 
      transport.connect(host, port, username, password); 
      transport.sendMessage(message, message.getAllRecipients()); 
     } catch (MessagingException ex) { 
      LOG.log(Level.SEVERE, "{0}", ex); 
     } 
    } 
} 

回答

1

我不是Java-Mail的專家,但ConnectException: connection refused通常在指定端口上沒有運行服務時發生。

+0

端口25是smtp,我相信。我可以通過telnet端口25發送消息,因此服務(postfix-dovecot)似乎正在運行。電子郵件顯示在Thunderbird中。 (FQDN dur.bounceme.net僅用於我的用途,它不是一個「真正的」主機。) – Thufir 2013-02-23 11:22:55

+0

我不確定,我完全理解你。您可以通過端口25上的telnet發送消息,這意味着有一個telnet服務在端口25上運行。因此,不能在端口25上運行smtp服務,這意味着您會收到拒絕,因爲telnet服務可能會「 t接受smtp消息。 – fachammer 2013-02-23 11:50:48

+0

我不這麼認爲。根據http://articles.slicehost.com/2008/8/6/postfix-using-telnet-to-test-postfix測試postfix和smtp的方法是使用telnet來做到這一點。他們指定'telnet mail.demoslice.com 25'作爲此目的的示例命令,但也許這是不同的,因爲它是slicehost。 – Thufir 2013-02-23 12:59:48

相關問題