2013-03-20 52 views
0

我寫一個簡單的Java程序將郵件發送到特定的郵箱ID使用java mail using SSL無法通過SSL發送Java郵件中的Servlet

以下是我的代碼

 public static String sendMail(String to) 
     { 
      String status_message = null; 

      try 
      { 
        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() 
        { 
         @Override 
         protected PasswordAuthentication getPasswordAuthentication() 
         { 
          return new PasswordAuthentication("[email protected]","password");//change accordingly 
         } 
        }); 

       //compose message 
       MimeMessage message = new MimeMessage(session); 
       message.setFrom(new InternetAddress("[email protected]"));//change accordingly 
       message.addRecipient(Message.RecipientType.TO,new InternetAddress(to)); 
       message.setSubject("Email Subject"); 
       message.setText("Text Content included in inside Email"); 

       //send message 
       Transport.send(message); 

       status_message = "success"; 
    } 
    catch (MessagingException ex) 
    { 
       status_message = ex.getMessage(); 
    } 


} 

當我衝了進去驗證碼a pariticular main線程。它成功地

當我把它inside a function called within a servlet

當我看到STATUS_MESSAGE值這同樣沒有工作。它顯示smtp

任何人都可以幫我找出問題所在。我錯過了什麼? 我想通過一個jsp servlet發送郵件。

+0

發佈像「沒有工作」這樣的無用語句基本上是浪費時間。發佈異常,堆棧跟蹤等 – EJP 2013-03-20 23:36:22

+0

@EJP status_message包含錯誤消息,它顯示smtp ... – 2013-03-21 05:02:59

回答