2012-10-25 146 views
1

我想通過servlet觸發我的電子郵件,但它不會觸發。當我作爲獨立的Java應用程序運行代碼時,它工作正常。以下是我的代碼。
Servlet是給我的問題,從服務器,我無法觸發代碼電子郵件不觸發

package model; 

import java.util.Date; 
import java.util.Properties; 

import javax.mail.*; 
import javax.mail.internet.AddressException; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 
import javax.naming.InitialContext; 
importjavax.naming.NamingException; 

import com.sun.xml.internal.bind.CycleRecoverable.Context; 

public class TestJavaMail 
{ 

    String userid ; 
    String password; 
    public TestJavaMail(String userid , String password) 
    { 
     this.userid = userid; 
     this.password =password; 
    } 

     public void process() 
     { 

     String[] to = {"[email protected]","[email protected]   technologies.com", 
       "[email protected]"}; 
     Properties props = new Properties(); 
     props.put("mail.transport.protocol", "smtp"); 
     props.put("mail.smtp.host", "smtp.exchangemails.com"); 
     props.put("mail.smtp.port", 25); 

     props.put("mail.smtp.user", userid); 
     props.put("mail.smtp.pass", password); 
     props.put("mail.smtp.starttls.enable", "true"); 
     props.put("mail.smtp.auth ", "true"); 

           Authenticator auth = new javax.mail.Authenticator() 
     { 
      protected PasswordAuthentication getPasswordAuthentication() 
      { 
       return new PasswordAuthentication(userid, password); 
      } 
     }; 

        Session session = Session.getDefaultInstance(props,auth); 

     try 
     { 
           MimeMessage msg = new MimeMessage(session); 
      System.out.println("Mimemessage ceated"); 
      InternetAddress[] iaFrom = { new InternetAddress( 
        "[email protected]") }; 
      msg.setFrom(iaFrom[0]); 
      InternetAddress[] iaTo = new InternetAddress[to.length] ; 
      for(int i=0;i<to.length;i++) 
      { 
       iaTo[i] = new InternetAddress(to[i]); 
       msg.addRecipient(Message.RecipientType.TO, iaTo[i]); 
      } 

      msg.setSubject("Test Java Mail"); 
      msg.setSentDate(new Date()); 
      msg.setText("Hello, Congrats - It is working\n pls send acknowledgement mail to senderof u get this" + 
        "\n as it is part of project"); 
      Transport tran = session.getTransport("smtp"); 
      System.out.println("Transport object created......"); 
      //tran.setStarttls(true); 
      tran.connect("smtp.exchangemails.com", 25, "[email protected]   technologies.com", "welcome"); 
      //tran.connect(); 
      msg.saveChanges(); 
      System.out.println("Connect succeeded"); 
      tran.sendMessage(msg, msg.getAllRecipients()); 
      tran.close(); 
      System.out.println("Mail Sent Successfully"); 
     } 
     catch (MessagingException mex) 
     { 
      System.out.println("send failed, exception: " + mex); 
     } 
         } 
} 

下面是servlet代碼:

package com.controller; 

import java.io.*; 
import java.io.PrintWriter; 

import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import model.TestJavaMail; 
/** 
* Servlet implementation class emailservice 
*/ 
    public class emailservice extends HttpServlet { 


    protected void doGet(HttpServletRequest req, HttpServletResponse res) throws  ServletException, IOException { 
     res.setContentType("text/html"); 
     System.out.println("here"); 

      PrintWriter writer = res.getWriter(); 
      writer.println("HELLO WORLD"); 
      String h = "hello"; 

      writer.println("" +h); 

      writer.println("welcome "); 

      writer.println(req.getRemoteHost()); 




     String userid = getServletConfig().getInitParameter("userid"); 
     String password = getServletConfig().getInitParameter("password"); 
     writer.println(userid);  
     TestJavaMail t1 = new TestJavaMail(userid, password); 

     t1.process(); 
     writer.println("\n bhej di "); 


    } 


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 

    } 

} 
+1

大部分代碼似乎被註釋掉了。請改善您的代碼的格式,使其更具可讀性。並且請將其縮減爲了解您的問題所需的內容。 – 2012-10-25 10:30:22

+0

可以告訴我,如果我的servlet代碼可以調用process()method.only,我已經評論了我的主要方法 – Monis

+0

編輯了代碼 – Monis

回答

0

我試圖運行你的代碼爲我自己,看看會發生什麼。首先,我確定我在類路徑中有mail.jar文件。其次,我做了一些必要的更改,以便能夠在沒有獲取認證錯誤的情況下運行代碼。即的,而不是:

props.put("mail.smtp.host", "smtp.exchangemails.com"); 

我用:

props.put("mail.smtp.host", "smtp.gmail.com"); 

,顯然,我必須爲這個不同的主機提供我自己的用戶名和密碼。

你是對的;該應用程序在單獨執行時運行良好。但是,當我將它移動到JBoss AS 7.0.0時,我遇到了兩個不同的問題。

起初,我面對這個錯誤:

at javax.mail.Session.getDefaultInstance(Session.java:320) [mail-1.4.4.jar:1.4.4] 
at test.TestJavaMail.process(TestJavaMail.java:42) [classes:] 

其中一些谷歌上搜索,並找到解決方案給出here後,我能擺脫這種錯誤的。即,我換成這一行:

Session session = Session.getDefaultInstance(props, auth); 

有:

Session session = Session.getInstance(props, auth); 

於是,我試圖再次運行該應用程序。這一次,我收到以下錯誤:

java.lang.NoClassDefFoundError: javax/net/ssl/SSLPeerUnverifiedException 
    com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1900) 
    com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:638) 
    javax.mail.Service.connect(Service.java:295) 
    test.TestJavaMail.process(TestJavaMail.java:63) 

我用谷歌搜索上面的錯誤,發現there was a bug with my used JBoss version。所以,我將應用程序移動到了JBoss版本,該版本是JBoss AS 7.1.1,並且我成功了。這個servlet成功地發送了一封郵件到我爲它提供的目的地。

我希望這項調查會對你和任何正在爲類似問題苦苦掙扎的訪問者有所幫助。