2014-09-28 126 views
1

我目前正在研究一個涉及創建一個.jar文件的項目。下面是要實現的,我應該出口/閱讀朋友的註冊表的路徑,一旦完成,我應該通過電子郵件得到結果。整個概念是在創建jar文件,一旦它被點擊,我通過電子郵件得到結果,因爲我實際上是通過電子郵件發送的。通過Java發送電子郵件 - javax.mail.MessagingException:無法連接到SMTP主機:localhost,port:587;

(我希望這是有道理的)

因此,首先,我結合下面的代碼實際上讀取註冊表並顯示鍵(我從受歡迎的崗位上進行讀/寫註冊表棧溢出了它)所以閱讀過程工作正常,現在我的問題是與電子郵件代碼,

(我不太確定誰是這個代碼的原始所有者,但充分的功勞去了他) 我試圖讓這個電子郵件代碼的基本概念工作,以便我可以繼續工作發送我的jar文件作爲附件,並以某種方式將其鏈接到我的代碼,當用戶單擊jar文件時,註冊表結果將是電子郵件回到我身邊。

import java.util.Properties; 
import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.AddressException; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 

public class MailCode { 

    public static void main(String[] args) throws Exception { 


    final String smtp_host = "smtp.gmail.com"; 
    final String smtp_username = "[email protected]"; 
    final String smtp_password = "password"; 
    final String smtp_connection = "TLS"; // Use 'TLS' or 'SSL' connection 

    final String toEmail="[email protected]"; 
    final String fromEmail="**@gmail.com"; 

    Properties props = new Properties(); 
    props.put("mail.smtp.auth", "true"); 

    if (smtp_connection.equals("TLS")) { 
      props.put("mail.smtp.starttls.enable", "true"); 
      props.put("mail.smtp.port", "587"); 
    } else{ 
      props.put("mail.smtp.socketFactory.port", "465"); 
      props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
      props.put("mail.smtp.port", "465"); 
    } 

    Session session = Session.getInstance(props, 
     new javax.mail.Authenticator() { 
      @Override 
      protected PasswordAuthentication getPasswordAuthentication() { 
        return new PasswordAuthentication(smtp_username, smtp_password); 
      } 
     }); 

    try { 
     Message msg = new MimeMessage(session); 
     msg.setFrom(new InternetAddress(fromEmail, "NoReply")); 
     msg.addRecipient(Message.RecipientType.TO, 
         new InternetAddress(toEmail, "Mr. Recipient")); 
     msg.setSubject("Welcome To JavaMail API"); 
     msg.setText("JavaMail API Test - Sending email example through remote smtp server"); 
     Transport.send(msg); 
     System.out.println("Email sent successfully..."); 
    } catch (AddressException e) { 
     throw new RuntimeException(e); 
    } catch (MessagingException e) { 
     throw new RuntimeException(e); 
    } 
    } 
} 

這是錯誤消息我得到:

Exception in thread "main" java.lang.RuntimeException: javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 587; 
nested exception is: 
java.net.SocketException: Permission denied: connect 
+1

你是否字面上試圖執行該代碼?你知道它在做什麼嗎?爲了記錄,它嘗試以「[email protected]」作爲用戶訪問Gmail的郵件服務器,並以「password」作爲密碼。我懷疑[email protected]是你真正的Gmail地址。即使是這樣,我懷疑你的密碼是「密碼」。如果是這樣,你最好現在改變它。 – 2014-09-28 16:59:27

+0

@JBNizet是我知道這段代碼試圖做什麼,現在既不是我的真實密碼也不是電子郵件,我把它放在證明用戶名是電子郵件地址應該插入的地方,「密碼」是真正的密碼應插入 – Scarl 2014-09-29 03:24:26

+0

你可以嘗試將SMTP主機更改爲smtp.googlemail.com嗎? – Rorschach 2014-09-29 03:54:58

回答

2

下面的代碼可以幫助你解決你的問題,其工作........

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

public class Email { 

private static String USER_NAME = "username"; // GMail user name (just the part before "@gmail.com") 
private static String PASSWORD = "password"; // GMail password 

private static String RECIPIENT = "[email protected]"; 

public static void main(String[] args) { 
    String from = USER_NAME; 
    String pass = PASSWORD; 
    String[] to = { RECIPIENT }; // list of recipient email addresses 
    String subject = "Java send mail example"; 
    String body = "hi ....,!"; 

    sendFromGMail(from, pass, to, subject, body); 
} 

private static void sendFromGMail(String from, String pass, String[] to, String subject, String body) { 
    Properties props = System.getProperties(); 
    String host = "smtp.gmail.com"; 

    props.put("mail.smtp.starttls.enable", "true"); 

    props.put("mail.smtp.ssl.trust", host); 
    props.put("mail.smtp.user", from); 
    props.put("mail.smtp.password", pass); 
    props.put("mail.smtp.port", "587"); 
    props.put("mail.smtp.auth", "true"); 


    Session session = Session.getDefaultInstance(props); 
    MimeMessage message = new MimeMessage(session); 

    try { 


     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++) { 
      toAddress[i] = new InternetAddress(to[i]); 
     } 

     for(int i = 0; i < toAddress.length; i++) { 
      message.addRecipient(Message.RecipientType.TO, toAddress[i]); 
     } 



     message.setSubject(subject); 
     message.setText(body); 


     Transport transport = session.getTransport("smtp"); 


     transport.connect(host, from, pass); 
     transport.sendMessage(message, message.getAllRecipients()); 
     transport.close(); 

    } 
    catch (AddressException ae) { 
     ae.printStackTrace(); 
    } 
    catch (MessagingException me) { 
     me.printStackTrace(); 
    } 
    } 
    } 
+0

其成功爲我工作,如果此代碼不適合你,請檢查你的jar文件及其版本。 – Anptk 2014-09-29 04:45:40

+0

感謝它,我也能夠得到一個附件的工作,我留下來的唯一的東西是接收到我的jar文件執行後的結果,你可能知道如果jar文件有某種java方法爲了那個原因? – Scarl 2014-09-29 04:53:03

+0

Actualylly,我沒有一個適當的解決方案,我正在試圖.. – Anptk 2014-09-29 05:02:09

2

你設置smtp_host但你永遠不會使用它。

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

您正嘗試連接到'localhost'而不是gmail.com。這是因爲你沒有在任何地方設置mail.smtp.host

你爲什麼要在-TLS情況下設置SSL插座工廠?

1

請檢查您的防病毒/惡意軟件。

如果您使用的是企業開發人員計算機,請與您的管理員聯繫以刪除該訪問保護規則。

我有同樣的問題,我的邁克菲阻止了所有的請求。

被端口阻止規則C阻止:\ PROGRAM FILES \ JAVA \ JDK1.8.0_74 \ BIN \ JAVAW.EXE防病毒標準保護:防止羣發郵件蠕蟲發送郵件0:0:0:0:0: ffff:4a7d:7e6d:587

在訪問保護日誌中發現了這一點,並要求我的管理員刪除該規則。

試試這可能會幫助你。

相關問題