iT邦幫忙

DAY 16
3

JSP 學習分享系列 第 16

JSP 和Java Mail part2

發信可以使用附件功能,需要cos.jar ,可到http://www.servlets.com/cos/index.html下載
1.可以夾附件的EMail
=== mailInput2.jsp ===

	<%@page contentType="text/html" pageEncoding="UTF-8"%>  
	  
		  
			<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
			<title>Mail Input</title>  
		  
		  
			<h1>Send Mail</h1>  
			<form method="post" action="sendMail.jsp">  
				From Email:<input type="text" name="from" value="" /><br>  
				To Email  :<input type="text" name="to" value=""><br>  
				Subject   :<input type="text" name="subject" value=""><br>
				Attachment:<input type="file" name="upfile" value=""><br>
				Content   :<textarea name="content" cols="80" rows="6"></textarea><br>  
				<input type="submit" value="Send" name="send" />  
				<input type="reset" value="Reset" name="reset" />  
			</form>  
		  
	   

2.先使用cos.jar把附件upload到server再夾到附件裡發出去
=== sendMail ===

	<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="javax.mail.*" %>
<%@ page import="javax.mail.internet.*" %>
<%@ page import="javax.activation.*" %>
<%@ page import="java.util.*,java.io.*" %>
<%@ page import="com.oreilly.servlet.MultipartRequest" %>


  
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Send Mail</title>
  
  
<%
  InternetAddress[] address = null;
  request.setCharacterEncoding("UTF-8");
  //max file upload size;
  int maxFilesize = 5*1024*1024;
  MultipartRequest multi = new MultipartRequest(request , "d:/kill" , maxFilesize , "big5");


  String mailhost   = "smtp.pchome.com.tw";
  String from         = multi.getParameter("from");
  String to           = multi.getParameter("to");
  String subject      = multi.getParameter("subject");
  String content  = multi.getParameter("content");
  String filename     = multi.getFilesystemName("upfile");

  boolean sessionDebug = false;
  String userName="xxxxxx";//your id
  String password = "xxxxx";//your password
  try {

    java.util.Properties props = System.getProperties();
    props.put("mail.smtp.host",mailhost);
    props.put("mail.smtp.auth", "true");

    Authenticator auth = new javax.mail.Authenticator() {
      String userName="xxxxx";//your id
      String password = "xxxxxx";//your password
      protected PasswordAuthentication getPasswordAuthentication(){
        return new PasswordAuthentication(userName, password);
      }
    };
    javax.mail.Session mailSession = javax.mail.Session.getInstance(props,auth);
    mailSession.setDebug(sessionDebug);
    Message msg = new MimeMessage(mailSession);
    // set mail from
    msg.setFrom(new InternetAddress(from));
    // set to email
    address = InternetAddress.parse(to,false);
    msg.setRecipients(Message.RecipientType.TO, address);
    // set mail subject
    msg.setSubject(subject);
    // set send date
    msg.setSentDate(new Date());
    // set content
    msg.setText(content);
    // Get Transport use userName and password
    if (filename != null)    {
      File file = new File("d:/kill/" + filename);

      MimeBodyPart mbp1 = new MimeBodyPart();

      mbp1.setContent(content, "text/plain");
      MimeBodyPart mbp2 = new MimeBodyPart();
      FileDataSource fds = new FileDataSource(file);
      mbp2.setDataHandler(new DataHandler(fds));
      mbp2.setFileName(fds.getName());

      // combine multipart
      Multipart mp = new MimeMultipart();

      mp.addBodyPart(mbp1);
      mp.addBodyPart(mbp2);
      msg.setContent(mp);
    }  else    {
      // 若沒有檔案時,就直接存郵件內容
      msg.setContent(content,"text/plain");
    }
    Transport trans = mailSession.getTransport("smtp");
    trans.connect(mailhost,userName, password);
    trans.send(msg);
    %>Send OK<%
  }
  catch (Exception ex) {
    out.println(ex);
  }
%>
  

上一篇
JSP and JavaMail part1
下一篇
JSP 和Apache Upload
系列文
JSP 學習分享30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
aquarius6913
iT邦新手 5 級 ‧ 2013-06-19 14:35:14

大大請教一下,我會出現錯誤訊息如下,煩請解惑!

org.apache.jasper.JasperException: An exception occurred processing JSP page /sendMail2.jsp at line 19

16: request.setCharacterEncoding("UTF-8");
17: //max file upload size = 5MB;
18: int maxFilesize = 5*1024*1024;
19: MultipartRequest multi = new MultipartRequest(request , "d:/kill" , maxFilesize , "big5");
20:
21:
22: String mailhost = "mail.hosp.ncku.edu.tw";

Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:505)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:404)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

root cause

java.io.IOException: Posted content type

我要留言

立即登入留言