2012-01-12 172 views
3

我想通過使用SSL連接的Gmail SMTP服務器發送電子郵件通知。但沒有電子郵件越來越。即使沒有錯誤報告那裏。我的方法send()給出below.Please help.I使用JSF2。使用SSL連接通過Gmail SMTP服務器發送電子郵件通知

public void send() 
{ 
    Properties props = new Properties(); 
    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"); 

    Session session = Session.getDefaultInstance(props, 
      new javax.mail.Authenticator() { 
     protected PasswordAuthentication getPasswordAuthentication() { 
      return new PasswordAuthentication("userName","password"); 
     } 
    }); 

    try { 

     Message message = new MimeMessage(session); 
     message.setFrom(new InternetAddress("[email protected]")); 
     message.setRecipients(Message.RecipientType.TO, 
       InternetAddress.parse("[email protected]")); 
     message.setSubject("Testing Subject"); 
     message.setText("Dear Mail Crawler," + 
       "\n\n No spam to my email, please!"); 

     Transport.send(message); 

     System.out.println("Done"); 

    } catch (MessagingException e) { 
     throw new RuntimeException(e); 
    } 
} 
+2

JSF2到底有多相關?你是否暗示在通過'main()'方法在普通的Java應用程序中執行時,它可以正常工作?如果它甚至失敗了,那麼它與JSF2完全無關,你需要刪除JSF2標籤。 – BalusC 2012-01-12 13:02:42

+0

好吧,但這不是'main()'。我在某處調用了這個'send()'。 – NaaN 2012-01-12 13:26:28

+2

我想你不理解我。將該代碼複製到'main()'方法內並作爲普通Java應用程序執行。如果你遇到完全相同的問題,那麼它根本與JSF2無關。畢竟,作爲一個普通的Java應用程序執行也允許更快和更容易的開發/測試。一旦你得到它的工作,所有你需要做的就是在你的JSF2操作方法中導入/調用獨立的Java類。 – BalusC 2012-01-12 13:27:20

回答

相關問題