多附件邮件的发送实例,附件上传这部分我用的是cos。关于javaMail基础配置请参考我的上一篇文章
<form action="SendEnclosure" method="post" enctype="multipart/form-data">
<table border="0" align="center" width="80%">
<tr>
<td align="right">From:</td>
<td>
<input type="text" name="from">
</td>
</tr>
<tr>
<td align="right">To:</td>
<td>
<input type="text" name="to">
</td>
</tr>
<tr>
<td align="right">Subject:</td>
<td>
<input type="text" name="subject">
</td>
</tr>
<tr>
<td align="right">Context:</td>
<td>
<textarea rows="5" cols="30" name="context"></textarea>
</td>
</tr>
<tr>
<td align="right">附件:</td>
<td>
<input type="file" name="fileName">
</td>
</tr>
<tr>
<td align="right">附件:</td>
<td>
<input type="file" name="fileName2">
</td>
</tr>
<tr>
<td align="center" colspan="2">
<input type="submit" value="发送">
</td>
</tr>
</table>
</form>
servlet如下:
public class SendEnclosure extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 3264368537634726072L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("GBK");
/**
* from的enctype设置为"multipart/form-data"在servlet中request.getParameter("")无法获得
* 使用oreilly的MultipartRequest封装可以获取
*/
int enclosureSize = 5 * 1024 * 1024;
// 把文件名带有.的作为附件信息
MultipartRequest req = new MultipartRequest(request, ".", enclosureSize, "GBK");
String from = req.getParameter("from");
String to = req.getParameter("to");
String subject = req.getParameter("subject");
String context = req.getParameter("context");
// 获得文件名
/**
String fileName = req.getFilesystemName("fileName");
**/
List<String> fileNames = new ArrayList<String>();
// 多附件上传例子:获得所有的文件名,返回的是Enumeration
Enumeration enumFiles = req.getFileNames();
String fname = "";
while(enumFiles.hasMoreElements()){
fname = (String) enumFiles.nextElement();
File f = req.getFile(fname);
System.out.println(f.getName());
fileNames.add(f.getName());
}
// 邮件服务器的地址
String mailServer = "PC200904071715";
// 设置邮件的传输协议信息
Properties transProp = System.getProperties();
// 邮件服务器地址
transProp.put("mail.smtp.host", mailServer);
// 邮件传输协议中的接收协议:smtp
transProp.put("mail.transport.protocol", "smtp");
// 是否通过验证
transProp.put("mail.smtp.auth", "true");
// 服务器端口
transProp.put("mail.smtp.port", "25");// 默认端口25
javax.mail.Session mailSession = javax.mail.Session.getDefaultInstance(transProp,new Authenticator(){
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("admin","");
}
});
javax.mail.Message mailMessage = new javax.mail.internet.MimeMessage(mailSession);
try {
mailMessage.setFrom(new InternetAddress(from));
mailMessage.setRecipient(RecipientType.TO, new InternetAddress(to));
mailMessage.setSubject(subject);
mailMessage.setSentDate(new Date());
Multipart mp = new MimeMultipart();
// 文本信息部分
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setContent(context, "text/plain;charset=GBK");
mp.addBodyPart(mbp1);
/**
if (fileName != null) {
// 附件部分
MimeBodyPart mbp2 = new MimeBodyPart();
FileDataSource fds = new FileDataSource(fileName);
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(MimeUtility.encodeText(fds.getName(), "GBK", "B"));
mp.addBodyPart(mbp2);
}
**/
MimeBodyPart mbp3 = null;
if (fileNames != null && fileNames.size() > 0) {
for (int i = 0; i < fileNames.size(); i++) {
mbp3 = new MimeBodyPart();
FileDataSource fds = new FileDataSource(fileNames.get(i));
mbp3.setDataHandler(new DataHandler(fds));
mbp3.setFileName(MimeUtility.encodeText(fds.getName(),"GBK", "B"));
mp.addBodyPart(mbp3);
}
}
mailMessage.setContent(mp);
javax.mail.Transport.send(mailMessage);
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
request.getRequestDispatcher("ok.jsp").forward(request, response);
}
}