我写了一些小java程序来发送电子邮件。 我正在从eclipse运行这个程序。
public static void sendMail(){
Properties prop = new Properties();
prop.setProperty("mail.host", "****.com");
prop.setProperty("mail.smtp.port", "25");
prop.setProperty("mail.smtp.auth", "false");
Session sess = Session.getDefaultInstance(prop);
Message msg = new MimeMessage(sess);
try {
msg.setSubject("From me");
msg.setText("Hello");
msg.setFrom(new InternetAddress("****@***com","Me"));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress("****@****.com"));
Transport.send(msg);
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("mail Sent");
}
它会出现以下错误
com.sun.mail.util.MailConnectException: Couldn't connect to host, port: *****.com, 25; timeout -1;
nested exception is:
java.net.SocketException: Permission denied: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2054)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:697)
at javax.mail.Service.connect(Service.java:345)
at javax.mail.Service.connect(Service.java:226)
at javax.mail.Service.connect(Service.java:175)
at javax.mail.Transport.send0(Transport.java:253)
at javax.mail.Transport.send(Transport.java:124)
at json.mail.sendMail(mail.java:33)
到目前为止,我已经尝试了一些事情,但都没有奏效
1.禁用防火墙。
2.将以下行添加到eclipse.ini中
-Djava.net.preferIPv4Stack =真
它们都没有真正起作用。有没有人有任何想法?