我想使用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
答案 0 :(得分: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");
}};
}
答案 1 :(得分:0)
您将Properties对象中的SMTP服务器设置为您自己的计算机(localhost),这显然没有在端口25上运行SMTP服务器。我认为您想要使用现有的SMTP服务器(就像您从互联网服务提供商,或GMail为您提供的服务,等等......)。