2012-03-19 201 views
-1

我需要使用servlet的附件編碼電子郵件。請幫幫我。我是Servlets & JSP的新手。我需要JSP和Servlet編碼。如何使用servlet發送帶附件的電子郵件?

這裏是我到目前爲止已經試過 -

<%@page contentType="text/html" pageEncoding="UTF-8"%>   
<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <title>Sending email</title> 
    <link rel="stylesheet" href="style.css" type="text/css"> 
</head> 
<body> 
    <center>   
     <form action="EmailServlet" method="post" name="ss"> 
      <table> 
       <tr> 
        <td>To</td> 
        <td><input type="text" name="to"></td> 
       </tr> 
       <tr> 
        <td>cc</td> 
        <td><input type="text" name="cc"></td> 
       </tr> 
       <tr> 
        <td>bcc</td> 
        <td><input type="text" name="bcc"></td> 
       </tr> 
       <tr> 
        <td>From</td> 
        <td><input type="text" name="from"></td> 
       </tr> 
       <tr> 
        <td>Subject</td> 
        <td><input type="text" name="subject"></td> 
       </tr> 
       <tr> 
        <td>attach</td> 
        <td><input type="file" name="file1"></td> 
       </tr> 
       <tr> 
        <td>attach2</td> 
        <td><input type="file" name="file2"></td> 
       </tr>   
       <tr> 
        <td>Message</td> 
        <td><textarea cols="25" rows="8" name="message"></textarea></td> 
       </tr> 
       <tr> 
        <td>password</td> 
        <td><input type="password" name="password"></td> 
       </tr> 
      </table> 
      <br> 
      <input type="submit" value="submit" onclick=" return soora();"> 
     </form> 
    </center> 
</body> 
</html> 

我的servlet頁面:

import java.io.*; 
import java.net.*; 

import java.util.Properties; 
import javax.mail.AuthenticationFailedException; 
import javax.mail.Authenticator; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.AddressException; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 
import javax.servlet.*; 
import javax.servlet.http.*; 

public class EmailServlet extends HttpServlet { 

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

     final String err = "/error.jsp"; 
     final String succ = "/success.jsp"; 

     String from = request.getParameter("from"); 
     String to = request.getParameter("to"); 
     String subject = request.getParameter("subject"); 
     String message = request.getParameter("message"); 
     String login = request.getParameter("login"); 
     String password = request.getParameter("password"); 

     try { 
      Properties props = new Properties(); 
      props.setProperty("mail.host", "smtp.gmail.com"); 
      props.setProperty("mail.smtp.port", "587"); 
      props.setProperty("mail.smtp.auth", "true"); 
      props.setProperty("mail.smtp.starttls.enable", "true"); 

      Authenticator auth = new SMTPAuthenticator(login, password); 

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

      MimeMessage msg = new MimeMessage(session); 
      msg.setText(message); 
      msg.setSubject(subject); 
      msg.setFrom(new InternetAddress(from)); 
      msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); 
      Transport.send(msg); 

     } catch (AuthenticationFailedException ex) { 
      request.setAttribute("ErrorMessage", "Authentication failed"); 

      RequestDispatcher dispatcher = request.getRequestDispatcher(err); 
      dispatcher.forward(request, response); 

     } catch (AddressException ex) { 
      request.setAttribute("ErrorMessage", "Wrong email address"); 

      RequestDispatcher dispatcher = request.getRequestDispatcher(err); 
      dispatcher.forward(request, response); 

     } catch (MessagingException ex) { 
      request.setAttribute("ErrorMessage", ex.getMessage()); 

      RequestDispatcher dispatcher = request.getRequestDispatcher(err); 
      dispatcher.forward(request, response); 
     } 
      RequestDispatcher dispatcher = request.getRequestDispatcher(succ); 
      dispatcher.forward(request, response); 

    } 

    private class SMTPAuthenticator extends Authenticator { 

     private PasswordAuthentication authentication; 

     public SMTPAuthenticator(String login, String password) { 
      authentication = new PasswordAuthentication(login, password); 
     } 

     protected PasswordAuthentication getPasswordAuthentication() { 
      return authentication; 
     } 
    } 

    protected void doGet(HttpServletRequest request, 
         HttpServletResponse response) 
        throws ServletException, IOException { 
     processRequest(request, response); 
    } 

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

我得到了下面這樣的錯誤:

java.lang.IllegalStateException: Cannot forward after response has been committed 
EmailServlet.processRequest(EmailServlet.java:71) 
EmailServlet.doPost(EmailServlet.java:97) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:710) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:803) 
+0

先生請幫我找出答案。我發佈了我試過的 – 2012-03-19 11:01:23

回答

0

您可以使用Java Mail進行此操作,它可以免費下載。它在servlet或JSP中與在沒有的情況下完全相同,只不過您可以使用context.xml和JNDI來獲取必須對所有內容進行硬編碼的連接。但是我不確定如果你不知道servlet或者JSP,那麼你已經準備好了。

0

你發送一個期待的err頁面中catch塊將提交回應,但你不能從那裏回來,所以代碼繼續在那裏你發送另一個期待的succ底部頁。實際上,如果發生異常,您將發送兩次前鋒,第二次前鋒不能發生並導致您面臨的特定異常。

您需要將return;添加到catch塊的末尾,以便該方法返回並且不執行剩餘的代碼。另請參閱java.lang.IllegalStateException: Cannot (forward | sendRedirect | create session) after response has been committed

對於JavaMail無法正常工作的情況,如果您打印得到的實際異常的堆棧跟蹤信息會更有幫助,以便您更準確地知道失敗的原因。現在,您只是將消息設置爲請求屬性(顯然需要在轉發的錯誤頁面中顯示),並且您將注意力集中在「錯誤」異常上。請注意,發送郵件在servlet類中與在普通的Java類中沒有區別。我建議把servlet代碼放在一邊,首先讓它在普通的Java類中工作。

相關問題