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