2015-07-12 49 views
-2

我試圖在Eclipse中使用Java發送簡單郵件。當我試圖運行該程序時,打開一個名爲ast jtree示例的小窗口。它的根目錄是c1 c2 c3,郵件沒有發送。在eclipse中使用Java Mail發送郵件時出錯

import java.util.*; 
import javax.mail.*; 
import javax.mail.internet.*; 

public class SingleEmail { 

    public static void main(String[] args) { 
     String to = "[email protected]"; 
      String from = "[email protected]"; 
      String host = "localhost";//or IP address 

     //Get the session object 
      Properties properties = System.getProperties(); 
      properties.setProperty("mail.user", "[email protected]"); 
      properties.setProperty("mail.password", "tecknodel"); 
      properties.setProperty("mail.smtp.host", host); 
      Session session = Session.getDefaultInstance(properties); 
     //compose the message 
      try{ 
      MimeMessage message = new MimeMessage(session); 
      message.setFrom(new InternetAddress(from)); 
      message.addRecipient(Message.RecipientType.TO,new InternetAddress(to)); 
      message.setSubject("Ping"); 
      message.setText("Hello, this is example of sending email "); 

      // Send message 
      Transport.send(message); 
      System.out.println("message sent successfully...."); 

      }catch (MessagingException mex) {mex.printStackTrace();} 
    } 

} 
+1

你有本地主機上運行的郵件服務器嗎?因爲這是你發送郵件的地方。 – Glorfindel

+0

其實我並不熟悉java,所以請你介紹一下郵件服務器。 – kani

+0

檢查@馬努的答案下面,那些似乎是正確的配置。 – Glorfindel

回答

1

由於您使用Gmail ID作爲從&解決,您可以使用下面的Gmail服務器的詳細信息來發送電子郵件。

props.put("mail.smtp.host", "smtp.gmail.com"); 
props.put("mail.smtp.socketFactory.port", "465"); 
props.put("mail.smtp.socketFactory.class", 
     "javax.net.ssl.SSLSocketFactory"); 
props.put("mail.smtp.auth", "true"); 
props.put("mail.smtp.port", "465"); 
+0

我改變了代碼如下。還存在同樣的問題。 – kani

+0

嘗試更改端口爲587. – connorp

+0

我試過了,仍然存在問題。 – kani