【发布时间】:2015-09-09 16:54:02
【问题描述】:
我是套接字编程的初学者。所以我无法解决我项目的错误,我什至无法理解问题出在哪里!
这是我的代码:
public static void main(String[] args) {
String to = "parisa.roodheleh@gmail.com";
String from = "parisainfinity@yahoo.com";
String host = "localhost";
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host",host);
Session session = Session.getDefaultInstance(properties);
try{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("This is the subject Line!");
message.setText("This is autual message!");
Transport.send(message);
System.out.println("sent message successfully...");
}catch(MessagingException mex){
mex.printStackTrace();
}
}
这里有一些错误:
com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 25; timeout -1;
nested exception is:
java.net.ConnectException: Connection refused: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2054)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:697)
有什么想法吗?
【问题讨论】:
-
你的本地机器上安装了任何 SMTP 服务器吗?检查它是否启动并运行。似乎你试图通过 Gmail SMTP 服务器发送电子邮件,那么你的主机将是 smtp .gmail.com 和端口将是 587。
-
emmm,对不起,我该如何检查?然后,我应该如何安装它?我应该谷歌什么?
-
好吧,我猜这不仅仅是关于 SMTP 服务器,这里还有另一个错误:在 javax.mail.Service.connect(Service.java:364)