JavaMail组件实现邮件功能
实现邮件收发功能需要3个jar包:
1.JavaMail组件保内的mail.jar和smtp.jar包
2.JAF组件包里的activition.jar。
复制到WebRoot/WEB-INF/lib目录下。
一、编写index.jsp页面,具体代码如下:
1 <%@ page language="java" contentType="text/html; charset=gb2312" 2 pageEncoding="gb2312"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 7 <title>welcome</title> 8 </head> 9 <body> 10 <form action="sendMail.jsp" method="post" name="form1"> 11 <table width="75" border="0" align="center" cellspacing="1" 12 bgcolor="#006600" class="black"> 13 <tr bgcolor="#ffffff"> 14 <td width="24%">收信人地址:</td> 15 <td width="76%"><input name="to" type="text" id="to"></td> 16 </tr> 17 <tr bgcolor="#ffffff"> 18 <td >主题:</td> 19 <td ><input name="title" type="text" id="title"></td> 20 </tr> 21 <tr> 22 <td height="18" colspan="2" bgcolor="#ffffff"> 23 信件类型 24 <select name="emailtype" id="emailtype"> 25 <option value="text/plain" selected>Text</option> 26 <option value="text/html" selected>Html</option> 27 </select> 28 </td> 29 </tr> 30 <tr> 31 <td height="53" colspan="2" bgcolor="#ffffff"> 32 <textarea rows="5" cols="50" name="content" id="content"></textarea> 33 </td> 34 </tr> 35 <tr align="center"> 36 <td colspan="2" bgcolor="#ffffff"> 37 附件1(自定义): 38 <input name="fj1" type="text" id="fj1"> 39 (输入文本信息) 40 </td> 41 </tr> 42 <tr align="center" valign="bottom"> 43 <td colspan="2" bgcolor="#ffffff"> 44 附件2(本地): 45 <input name="fj2" type="file" id="fj2" size="10"> 46 </td> 47 </tr> 48 <tr align="center" > 49 <td colspan="2" bgcolor="#ffffff"> 50 附件3(远程): 51 <input name="fj3" type="text" id="fj3" value="http://"> 52 (输入URL) 53 </td> 54 </tr> 55 <tr align="center" > 56 <td colspan="2" bgcolor="#ffffff"> 57 58 <input name="submit" type="submit" value="发送"> 59 <input name="submit2" type="reset" value="重置"> 60 </td> 61 </tr> 62 </table> 63 </form> 64 </body> 65 </html>
二、创建sendMail.jsp页面,具体代码如下:
<%@ page language="java" contentType="text/html; charset=gb2312" pageEncoding="gb2312"%> <%request.setCharacterEncoding("gb2312"); %> <%@ page import="java.util.*,javax.mail.*" %> <%@ page import="javax.mail.internet.*"%> <%@ page import="javax.activation.*" %> <%@ page import="java.net.*" %> <!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=gb2312"> <title>Insert title here</title> </head> <body> <% try{ String tto=request.getParameter("to"); String ttitle=request.getParameter("title"); String emailtype=request.getParameter("emailtype"); String tcontent=request.getParameter("content"); String tfj1=request.getParameter("fj1"); String tfj2=request.getParameter("fj2"); String tfj3=request.getParameter("fj3"); //声明properities存储发件服务器信息 Properties props=new Properties(); props.put("mail.smtp.host", "smtp.qq.com"); props.put("mail.smtp.auth", "true"); //创建邮件回话 Session s=Session.getInstance(props); s.setDebug(true); //创建一个消息对象 MimeMessage message=new MimeMessage(s); InternetAddress from=new InternetAddress("393743083@qq.com"); message.setFrom(from); InternetAddress to=new InternetAddress(tto); message.setRecipient(Message.RecipientType.TO, to); message.setSubject(ttitle); message.setSentDate(new Date()); Multipart mm = new MimeMultipart(); BodyPart mdp=new MimeBodyPart(); mdp.setContent(tcontent, emailtype+";charset=gb2312"); mm.addBodyPart(mdp); //附件1 mdp=new MimeBodyPart(); DataHandler dh=new DataHandler(tfj1,"text/plain;charset=gb2312"); mdp.setFileName("text.txt"); mdp.setDataHandler(dh); mm.addBodyPart(mdp); //附件2 mdp=new MimeBodyPart(); FileDataSource fds=new FileDataSource(tfj2); dh=new DataHandler(fds); int ddd=tfj2.lastIndexOf("\\"); String fname=tfj2.substring(ddd); String ffname=new String(fname.getBytes("gb2312"),"ISO8859-1"); mdp.setFileName(ffname); mdp.setDataHandler(dh); mm.addBodyPart(mdp); //附件3 mdp=new MimeBodyPart(); URL urlfj=new URL(tfj3); URLDataSource ur=new URLDataSource(urlfj); dh=new DataHandler(ur); int ttt=tfj3.lastIndexOf("/"); String urlname=tfj3.substring(ttt); mdp.setFileName(urlname); mdp.setDataHandler(dh); mm.addBodyPart(mdp); message.setContent(mm); message.saveChanges(); //发送邮件 Transport transport=s.getTransport("smtp"); transport.connect("smtp.qq.com",用户名,密码); transport.sendMessage(message, message.getAllRecipients()); transport.close(); %> <div align="center"> <p><font color="#ff6600">发送成功</font></p> <br> <a href="sendMail.jsp">再来一封</a> </div> <% }catch(MessagingException e){ out.println(e.toString()); } %> </body> </html>
注:腾讯邮箱需要开通smtp功能。