2012-12-30 35 views
3

我已經使用以下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

+0

你可以嘗試遠程登錄到所配置的SMTP端口的本地主機(默認值:25)。 telnet localhost 25'並檢查? – Srinivas

+0

那麼,你的機器上是否有SMTP服務器? –

+0

我試過telnet localhost 25 bt它與wr errored顯示 正在連接到本地...無法打開連接到主機,在端口25上:連接失敗 我已經在Windows功能中啓用了telnet客戶端和telnet服務器功能,我正在使用windows 7 –

回答

0

更改主機名:smtp.gmail.com

String host = "localhost"; 

更改爲:

String host = "smtp.gmail.com" 
+0

Downvoter。你有什麼問題? –

0

你現在你要嘗試這樣

String host = "smtp.gmail.com"; 
String from = "user name"; 
Properties props = System.getProperties(); 
props.put("mail.smtp.host", host); 
props.put("mail.smtp.user", from); 
props.put("mail.smtp.password", "asdfgh"); 
props.put("mail.smtp.port", "587"); // 587 is the port number of yahoo mail 
props.put("mail.smtp.auth", "true"); 

Session session = Session.getDefaultInstance(props, null); 
MimeMessage message = new MimeMessage(session); 
message.setFrom(new InternetAddress(from)); 

InternetAddress[] to_address = new InternetAddress[to.length]; 
int i = 0; 
// To get the array of addresses 
while (to[i] != null) { 
    to_address[i] = new InternetAddress(to[i]); 
    i++; 
} 
System.out.println(Message.RecipientType.TO); 
i = 0; 
while (to_address[i] != null) { 

    message.addRecipient(Message.RecipientType.TO, to_address[i]); 
    i++; 
} 
message.setSubject("sending in a group"); 
message.setText("Welcome to JavaMail"); 
// alternately, to send HTML mail: 
// message.setContent("<p>Welcome to JavaMail</p>", "text/html"); 
Transport transport = session.getTransport("smtp"); 
transport.connect("smtp.mail.yahoo.co.in", "user name", "asdfgh"); 
transport.sendMessage(message, message.getAllRecipients()); 
transport.close(); 

分辯試圖連接到本地主機

禮貌this answer

0

嘗試添加以下內容到性質:

private static final String SMTP_HOST = "smtp.gmail.com"; //Use Gmail's SMTP Host. 
private static final String SMTP_PORT = ""; //Use Gmail's Port Number for SMTP. 
properties.setProperty("mail.smtp.host", SMTP_HOST); 
properties.setProperty("mail.smtp.port", SMTP_PORT); 
5

我可以看到你要使用Gmail爲您的SMTP服務器。此行不正確:

properties.setProperty("smtp.gmail.com", host); 

您正在使用主機名作爲屬性名稱,這是不正確的。因爲您沒有設置mail.smtp.host屬性JavaMail嘗試連接到'localhost'。而不是設置這些下列特性:

properties.put("mail.smtp.starttls.enable", "true"); 
properties.put("mail.smtp.host", "smtp.gmail.com"); 
properties.put("mail.smtp.user", "username"); // User name 
properties.put("mail.smtp.password", "password"); // password 
properties.put("mail.smtp.port", "587"); 
properties.put("mail.smtp.auth", "true"); 
+0

什麼是用戶名?它是Gmail的身份證?或者是其他東西 ?? –