我已經使用以下Java代碼發送電子郵件。發送郵件時出錯 - ConnectException
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendEmail
{
public static void main(String [] args)
{
String to = "[email protected]";
String from = "[email protected]";
String host = "localhost";
Properties properties = System.getProperties();
properties.setProperty("smtp.gmail.com", 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 actual message");
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
當我運行該文件,我得到以下錯誤:
javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
nested exception is:
java.net.ConnectException: Connection refused: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1282)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
真的很感激,如果有人可以幫助我在此。
如何解決ConnectException
?
你可以嘗試遠程登錄到所配置的SMTP端口的本地主機(默認值:25)。 telnet localhost 25'並檢查? – Srinivas
那麼,你的機器上是否有SMTP服務器? –
我試過telnet localhost 25 bt它與wr errored顯示 正在連接到本地...無法打開連接到主機,在端口25上:連接失敗 我已經在Windows功能中啓用了telnet客戶端和telnet服務器功能,我正在使用windows 7 –