2012-05-02 174 views
2

這段代碼有什麼問題?我試圖用hMailServer在本地主機上發送電子郵件,但它不起作用。儘管此代碼正在爲Gmail的SMTP服務器。我可能會認爲錯誤是在我的hMailServer,但我不能找到它..Java連接到本地SMTP服務器

try{ 
    String host = "127.0.0.1"; 
    String from = "[email protected]"; 
    String pass = "1q2w#E$R"; 
    Properties props = System.getProperties(); 
    props.put("mail.smtp.starttls.enable", "true"); // added this line 
    props.put("mail.smtp.host", host); 
    props.put("mail.smtp.user", from); 
    props.put("mail.smtp.password", pass); 
    props.put("mail.smtp.port", "25"); 
    props.put("mail.smtp.auth", "true"); 

    String[] to = {"[email protected]"}; // added this line 

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

    InternetAddress[] toAddress = new InternetAddress[to.length]; 

    // To get the array of addresses 
    for(int i=0; i < to.length; i++) { // changed from a while loop 
     toAddress[i] = new InternetAddress(to[i]); 
    } 
    for(int i=0; i < toAddress.length; i++) { // changed from a while loop 
     message.addRecipient(Message.RecipientType.TO, toAddress[i]); 
    } 
    message.setSubject("sending in a group"); 
    message.setText("Welcome to JavaMail"); 
    Transport transport = session.getTransport("smtp"); 
    transport.connect(host, from, pass); 
    transport.sendMessage(message, message.getAllRecipients()); 
    transport.close(); 
    } 
    catch (Exception e) { 
     e.printStackTrace(); 
    } 

這是我得到的錯誤:

javax.mail.MessagingException: Could not connect to SMTP host: 127.0.0.1, port: 25; 
nested exception is: 
    java.net.SocketException: Permission denied: connect 
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1213) 
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:311) 
    at javax.mail.Service.connect(Service.java:233) 
    at javax.mail.Service.connect(Service.java:134) 
    at nl.company.cms.login.emailservice.TestMail.main(TestMail.java:71) 

我使用hMailServer ..

+0

您是否嘗試過使用標準客戶端連接到本地SMTP服務器,例如Thunderbird? –

回答

3

如果您使用的是Java 7,那麼我會嘗試添加這是一個JVM的參數,當你啓動應用程序:

-Djava.net.preferIPv4Stack=true

這可能是必要的,因爲Java現在試圖使用IPv6,所以我們需要告訴它改用IPv4堆棧。

+0

我正在使用Java 6 .. – Gynnad

+0

我剛剛注意到了關於starttls.enable的一行,這對Gmail很好,因爲它們有安全的服務器,您可以將它設置爲false嗎? –

1

如果您使用Xampp,請確保Mercury正在您的機器上運行,因爲如果汞未啓動,端口25將無法與javaMail一起使用。

1

我確認只有在Tomcat的catalina.bat中編寫的這個參數「-Djava.net.preferIPv4Stack = true」幫助我從我的Spring web應用程序發送一封電子郵件到我的本地hMailServer實例中。環境:Windows 8,Java 7u60。

0

我可以確認只通過實施@Quantas提供的在ECLIPSE的「運行配置」中添加參數「-Djava.net.preferIPv4Stack = true」的建議,我的問題是獲取異常mail.Messaging Exception現在解決。

從過去的2天開始,我在使用「Fake SMTP mail server」捕獲從我在eclipse中運行的應用程序發送的郵件時遇到了以下異常。

javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25; 

現在的問題已經通過只是把這個小小的一段代碼爲「VM參數」,在「運行配置」的ECLIPSE LUNA

-Djava.net.preferIPv4Stack=true 

得到徹底解決如圖快照下面

enter image description here

這表明java傾向於使用IPV4堆棧而不是IPV6(它先選擇它)