[JSP] 發送MAIL之二(可加入附件)

  • 7231
  • 0

摘要:[JSP] 發送MAIL之二(可加入附件)

之前已經發表過發送MAIL了

而剛好發送MAIL也常需要附加檔案

因此小弟在此補上CODE參考

也需要mail.jar與activation.jar這兩個檔案

因此在底下附上連結供網友下載使用

 

lib.zip

 

 

#sendEmailAttachement.htm


<%@ page contentType="text/html; charset=UTF-8" language="java" import="java.sql.*" errorPage="" %> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>带附件的邮件发送程序</title> 
</head> 

<body> 
<form name="form1" method="post" action="sendEmailAttachement.jsp"> 
  <table width="482" height="396" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="C4BED8"> 
    <tr bgcolor="#F2EDFF"> 
      <th height="34" colspan="2" scope="col">带附件的邮件发送程序</th> 
    </tr> 
    <tr bgcolor="#F2EDFF"> 
      <td width="70" height="32"><div align="right">收件人:</div></td> 
      <td width="406"><div align="center"> 
        <input type="text" name="rname" size="51"> 
</div></td> 
    </tr> 
    <tr bgcolor="#F2EDFF"> 
      <td height="26"><div align="right">发件人:</div></td> 
      <td><div align="center"> 
        <input type="text" name="sname" size="51"> 
      </div></td> 
    </tr> 
    <tr bgcolor="#F2EDFF"> 
      <td height="28"><div align="right">密码:</div></td> 
      <td><div align="center"> 
        <input type="password" name="password" size="57"> 
      </div></td> 
    </tr> 
    <tr bgcolor="#F2EDFF"> 
      <td height="27"><div align="right">主题:</div></td> 
      <td><div align="center"> 
        <input type="text" name="title" size="51"> 
      </div></td> 
    </tr> 
    <tr bgcolor="#F2EDFF"> 
      <td height="183"><div align="right">内容:</div></td> 
      <td><div align="center"> 
        <textarea name="content" cols="50" rows="12"></textarea> 
      </div></td> 
    </tr> 
    <tr  bgcolor="#F2EDFF"> 
      <td height="34"><div align="right">附件:</div></td> 
      <td><div align="center">  
          <input type="file" name="attachement" size="43"> 
</div></td> 
    </tr> 
    <tr align="center" valign="middle" bgcolor="#F2EDFF"> 
      <td height="30" colspan="2">
	  <input type="submit" name="Submit2" value="提交"> 
        
      <input type="reset" name="Submit3" value="重置">
	  </td> 
    </tr> 
  </table> 
</form> 
</body> 
</html> 

 

#sendEmailAttachement.jsp


<%@ page contentType="text/html; charset=UTF-8" language="java" import="java.sql.*" errorPage="" %> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="java.io.*"%> 
<%@ page import="java.util.*"%> 
<%@ page import="javax.mail.*"%> 
<%@ page import="javax.mail.internet.*"%> 
<%@ page import="javax.activation.*"%> 
<%@ page import="java.net.*"%> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>带附件的邮件发送程序</title> 
</head> 

<body> 
<% 
try{ 
    request.setCharacterEncoding("UTF-8"); 
    String from_mail=request.getParameter("sname"); 
    String to_mail=request.getParameter("rname"); 
    String from_psd=request.getParameter("password"); 
    String title=request.getParameter("title"); 
    String content=request.getParameter("content"); 
    String path =request.getParameter("attachement"); 

    String S =request.getParameter("sname"); 
    int n =S.indexOf('@'); 
    int m=S.length() ; 
    String server = "XXX"; //MAIL HOST 

    Properties prop =new Properties(); 
    prop.put("mail.smtp.host", server); 
    prop.put("mail.smtp.auth", "true"); 
    Session sess =Session.getInstance(prop); 
    sess.setDebug(true); 

    MimeMessage message=new MimeMessage(sess); 

    //给消息对象设置收件人、发件人、主题 
    InternetAddress from =new InternetAddress(from_mail); 
    message.setFrom(from); 
    InternetAddress to =new InternetAddress(to_mail); 
    message.setRecipient(Message.RecipientType.TO,to); 
    message.setSubject(title); 

    Multipart mul=new MimeMultipart(); //新建一个MimeMultipart对象来存放多个BodyPart 对象 
    BodyPart mdp=new MimeBodyPart(); //新建一个存放信件内容的BodyPart对象 
    mdp.setContent(content,"text/html;charset=UTF-8"); 
    mul.addBodyPart(mdp); //将含有信件内容的BodyPart加入到MimeMulitipart对象中 

    //设置信件的附件 
    mdp=new MimeBodyPart(); //新建一个存放附件的BodyPart 
    DataHandler handler=new DataHandler("JAVAMAIL 附件测试","text/plain; charset=UTF-8");  //新建一个DataHandler对象,并设置其内容、格式和编码方式 
    mdp.setFileName("mrsoft.txt"); //为“内容”文本框中的内容设置标题,并以附件的形式发送 
    mdp.setDataHandler(handler); //给BodyPart设置内容为handler 
    mul.addBodyPart(mdp); //将含有附件的BodyPart加入到MimeMulitipart对象中 
	
    //设置信件的附件(用本机上的文件作为附件) 
    mdp=new MimeBodyPart(); 
    FileDataSource fds=new FileDataSource(path);// 
    DataHandler handler=new DataHandler(fds); 
    mdp.setFileName(fds.getName());//和原文件名一致 
    mdp.setDataHandler(handler); 
    mul.addBodyPart(mdp); 

    message.setContent(mul);  //把mul作为消息对象的内容 

    message.saveChanges(); 
    Transport transport = sess.getTransport("smtp"); 

    //以smtp方式登陆邮箱,第一个参数是发送邮件用的邮件服务器SMTP地址,第二个参数为用户名,第三格参数为密码 
    transport.connect(server,from_mail,from_psd); 
    transport.sendMessage(message,message.getAllRecipients()); 
    transport.close(); 

    out.println("邮件发送成功!"); 

}catch(Exception Ex){ 
    out.print("邮件发送失败!,原因可能是:<ul>"); 
    out.print(Ex.getMessage()); 
    } 
%> 
</body> 
</html> 

 

 






Y2J's Life:http://kimenyeh.blogspot.tw/