JavaMail-3:Oreilly欧莱理上传组件的使用
发送带附件的邮件:
1.前端输入页面加入file
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>JavaMail3.htmltitle>
head>
<body>
<form action="SEND3" method="post" enctype="multipart/form-data">
From:<input type="text" name="from"><br>
TO:<input type="text" name="to"><br>
Subject:<input type="text" name="subject"><br>
type:<select name=type size="1">
<option value="text/plain">Textoption>
<option value="text/html">Htmloption>
select><br>
file:<input type="file" name="filename"><br>
Context:<textarea rows="4" cols="40" name="context">textarea><br>
<input type="submit" value="send">
form>
body>
html>
2.编写servlet : SEND3.java
package com.servlet;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.oreilly.servlet.MultipartRequest;
public class SEND3 extends HttpServlet ...{
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException ...{
response.setContentType("text/html;charset=GB18080");
PrintWriter out = response.getWriter();
request.setCharacterEncoding("GB18030");
//重新封装请求对象--oreilly
MultipartRequest req = new MultipartRequest(request,".",5*1024*1024,"GB18030");
String from = req.getParameter("from");
String to = req.getParameter("to");
String subject = req.getParameter("subject");
String context = req.getParameter("context");
String type=req.getParameter("type");
//获取文件名
String filename=req.getFilesystemName("filename");
// 确定要发送的邮件服务器的地址
String mailserver = "codedestiny-pc";
try ...{
// 设置邮件的传输协议
Properties prop = System.getProperties();
prop.put("mail.smtp.host", mailserver);
// 建立邮件发送的连接
Session session = Session.getDefaultInstance(prop, null);
// 创建发送的信息的载体
Message msg = new MimeMessage(session);
// 设置相关的邮件属性
msg.setFrom(new InternetAddress(from));
// 点到点的发送
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
msg.setSubject(subject);
msg.setSentDate(new Date());
//处理文件的上传
if(filename != null)...{
//File file=new File(filename);//存留备份
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setContent(context, type+";charset=GB18030");//对于邮件的普通信息的处理
//附件的处理
MimeBodyPart mbp2 = new MimeBodyPart();
FileDataSource fds = new FileDataSource(filename);
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(MimeUtility.encodeText(fds.getName(),"GB18030","B"));
/**//*
* file-----inputStream--|--FileDataSource---DataHandler(fds)接口的封装---mbp2
* */
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);
msg.setContent(mp);
}
else...{
msg.setContent(context, type+";charset=GB18080");
//信封的书写格式
}
// 发送
Transport.send(msg);
} catch (Exception e) ...{
e.printStackTrace();
}
out.print("send ok"+"
");
out.print("查看信件");
out.flush();
out.close();
}
}
阅读(898) | 评论(0) | 转发(0) |