0

我想使用 javax.mail 从 example@uni.ac.uk 向receiver@uni.ac.uk 发送一封电子邮件。我已经尝试了下面的代码,但它给出了我也在下面给出的错误。欢迎任何帮助。

    Properties prop = new Properties();
    Session sess = Session.getDefaultInstance(prop,null);

    Message msg = new MimeMessage(sess);
    msg.setFrom(new InternetAddress("example@uni.ac.uk", "Sender"));
             msg.addRecipient(Message.RecipientType.TO,
                      new InternetAddress("receiver@uni.ac.uk", "Receiver"));
            msg.setSubject("Your Example.com account has been activated");
            msg.setText("Testing messgage");
           Transport.send(msg);


}

产生的错误是

Exception in thread "main" javax.mail.MessagingException: Could not connect to
SMTP host: localhost, port: 25; nested exception is: java.net.SocketException: Permission denied: connect 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 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 MAIL.sendmail.main(sendmail.java:41) Caused by: java.net.SocketException: Permission denied: connect at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.PlainSocketImpl.doConnect(Unknown Source) at java.net.PlainSocketImpl.connectToAddress(Unknown Source) at java.net.PlainSocketImpl.connect(Unknown Source) at java.net.SocksSocketImpl.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) 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) ... 7 more

4

2 回答 2

1

这是使用 gmail 发送电子邮件的示例属性:

private Properties createConfiguration() {
    return new Properties() {{
        put("mail.smtp.auth", "true");
        put("mail.smtp.host", "smtp.gmail.com");
        put("mail.smtp.port", "587");
        put("mail.smtp.starttls.enable", "true");
    }};
}
于 2013-07-22T00:48:30.440 回答
0

您将 Properties 对象中的 SMTP 服务器设置为您自己的机器(localhost),这显然没有在端口 25 上运行 SMTP 服务器。我认为您想使用现有的 SMTP 服务器(就像您从 Internet 提供商处获得的服务器一样,或 GMail 为您提供的那个,等等...)。

于 2013-07-22T00:05:35.430 回答