2012-01-12 171 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

回答

相关问题