0

我可以向自己发送邮件(不是通过 intertubes 发送),但似乎无法使用 Java 进行如下复制。Postfix 通过 telnet 表现良好:

thufir@dur:~$ 
thufir@dur:~$ 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:<someone@somewhere.com>
250 2.1.0 Ok
rcpt to:<thufir@dur.bounceme.net>
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.
thufir@dur:~$ 

这是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);
        }
    }
}
4

1 回答 1

1

我不是 Java-Mail 方面的专家,但ConnectException: connection refused通常在指定端口上没有运行服务时发生。

于 2013-02-23T11:19:48.483 回答