2016-01-08 23 views
0

您好我有web應用程序java使用球衣,我部署我的程序與tomcat和連接到MySQL數據庫...我有一個類負責發送郵件給用戶。 localhost上一流的做工精細和郵件發送,但AFER我部署的方案,並上傳到服務器犯規向用戶發送郵件...我怎麼能從web應用程序發送郵件

這個IHS的CLAS:

public static boolean SendMail(String from, String password, String message, String to[]) { 
    String host = "smtp.gmail.com"; 
    Properties properties = System.getProperties(); 
    properties.put("mail.smtp.starttls.enable", "true"); 
    properties.put("mail.smtp.host", host); 
    properties.put("mail.smtp.user", from); 
    properties.put("mail.smtp.password", password); 
    properties.put("mail.smtp.port", 587); 
    properties.put("mail.smtp.auth", "true"); 
    Session session = Session.getDefaultInstance(properties, null); 
    MimeMessage mimeMessage = new MimeMessage(session); 
    try { 
     mimeMessage.setFrom(new InternetAddress(from)); 
     InternetAddress[] toAddress = new InternetAddress[to.length]; 
     for (int index = 0; index < to.length; index++) { 
      toAddress[index] = new InternetAddress(to[index]); 
     } 
     for (int index = 0; index < toAddress.length; index++) { 
      mimeMessage.addRecipient(Message.RecipientType.TO, toAddress[index]); 
     } 
     mimeMessage.setSubject("email From linkeride"); 
     mimeMessage.setText(message); 
     Transport transport = session.getTransport("smtp"); 
     transport.connect(host, from, password); 
     transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients()); 
     transport.close(); 
     return true; 

    } catch (MessagingException me) { 
     me.printStackTrace(); 
    } 
    return false; 
} 

我也許有人可以告訴我問題是什麼?

+1

您的主機是否運行郵件服務器?如果您想進行一些測試,我知道有一種方法可以通過您的Gmail帳戶管理電子郵件。至於製作推出,你需要一個郵件服務器來連接。 –

+0

可能是您的服務器無法訪問SMTP服務器或某個端口被阻止的簡單情況。 –

+0

是否有錯誤?您的網絡是否需要網關進行此類連接? – Perdomoff

回答

0

如您所述,此代碼在一臺機器上運行,而不在另一臺機器上運行,似乎更像服務器配置問題而不是編程。

properties.put("mail.smtp.debug", "true"); 

嘗試爲SMTP啓用調試模式,因此在日誌中,您將完全瞭解準確地發送郵件的步驟失敗。

+0

添加到我的代碼這是我從browesr得到: –

+0

HTTP狀態500 - com.sun.mail.util .MailConnectException:無法連接到主機,端口:smtp.gmail.com,587;超時-1; –

+0

java.lang.RuntimeException:com.sun.mail.util.MailConnectException:無法連接到主機,端口:smtp.gmail.com,587;超時-1; 嵌套的異常是: –