控制台打印出的内容,我们无法阅读,其实,让我们自己来解析一封复杂的邮件是很不容易的,邮件里面格式、规范复杂得很。不过,我们所用的浏览器内置了解析各种数据类型的数据处理模块,我们只需要在把数据流传输给浏览器之前明确地指定该数据流属于哪种数据类型即可,之后一切的解析操作由浏览器自动帮我们完成。下面这张图可以很好的说明解析邮件的步骤:
1、调用Message对象的getFrom、getSubject等方法,可以得到邮件的发件人和主题等信息,调用getContentType方法得到邮件的类型;
2、通过Message.getContentType方法的返回值判断邮件类型,并调用Message.getContent方法得到邮件内容。如果邮件类型为"text/plain"或者"text/html",表示邮件内容为纯文本,此时调用Message对象的getContent方法得到邮件内容,然后将返回对象的类型转换成String输出给显示软件即可。如果邮件类型为"multipart/*",表示邮件内容是一个复合类型,此时需将Message.getContent方法复合的对象转换成Multipart。
3、调用Multipart对象的getCount方法检测Multipart对象中封装了多少个BodyPart对象,并通过for循环逐一取出Multipart对象中的每个BodyPart对象进行处理。
4、在处理每个BodyPart对象时,首先调用BodyPart对象的getContentType方法得到它的MIME类型,然后根据MIME类型作出如下三种情况的处理:当MIME类型为"text/*"时,表示BodyPart对象中保存的是纯文本数据,如上图中的"text/plain",此时第一BodyPart对象的getContent方法并将返回的对象转换成String输出给显示软件显示即可。
当MIME类型表示的是图片、声音或者是附件等二进制数据时,如上图中的"image/gif"时,此时应调用BodyPart对象的getDataHandler方法得到了封装了数据的DataHanlder对象,然后调用DataHandler对象的getInputStream方法获得与数据相关联的InputStream对象,通过这个InputStream对象中即可获得原始的二进制数据内容。
当MIME类型为"multipart/mixed"时,表示BodyPart对象中保存的是一个复合MIME消息,此时应调用BodyPart对象的getContent方法得到封装复合MIME消息的对象并将它转换成Multipart类型,接着重复第3和第4个步骤操作对Multipart对象进行递归调用。
编写一个接收并解析带有附件的程序
packagemail;importjava.util.Properties;importjavax.mail.Folder;importjavax.mail.Session;importjavax.mail.Store;public classPOP3Help {public staticFolder getFolder(String host, String username, String password) {
Properties prop= newProperties();
prop.setProperty("mail.store.protocol", "pop3");
prop.setProperty("mail.pop3.host", host);
Session mailSession= Session.getDefaultInstance(prop, null);
mailSession.setDebug(false);try{
Store store= mailSession.getStore("pop3");
store.connect(host, username, password);
Folder folder= store.getFolder("inbox");
folder.open(Folder.READ_WRITE);returnfolder;
}catch(Exception e) {
e.printStackTrace();
}return null;
}
}
POP3Help.java
该类用来连接和登录POP3服务器,并返回代表邮件夹的Folder对象
login.html主机名:
用户名:
密码:
index.html
登录页面,需要用户填写邮件服务器的主机名,用户名和密码
String username= request.getParameter("username");
String password= request.getParameter("password");
String from= "";
String subject= "";
Folder folder=POP3Help.getFolder(host,username,password);
session.setAttribute("folder",folder);
Message [] messages=folder.getMessages();for(int i=0;i
{try{
from= messages[i].getFrom()[0].toString();
subject=messages[i].getSubject();
out.print(i+ 1);%>发件人地址: 邮件主题:
}%>
login.jsp
获取邮件夹中的所有邮件
" scrolling="no">
" scrolling="no">
displayMsg.jsp
用于展示邮件的信息
packagemail;importjava.io.IOException;importjava.io.PrintWriter;importjava.text.DateFormat;importjavax.mail.BodyPart;importjavax.mail.Folder;importjavax.mail.Message;importjavax.mail.Multipart;importjavax.mail.internet.MimeUtility;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjavax.servlet.http.HttpSession;
@SuppressWarnings("serial")public class DisplayHead extendsHttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {
response.setContentType("text/html;charset=gb2312");
PrintWriter out=response.getWriter();
HttpSession session=request.getSession();int msgnum = Integer.parseInt(request.getParameter("msgnum"));
Folder folder= (Folder) session.getAttribute("folder");try{
Message msg=folder.getMessage(msgnum);
String from= msg.getFrom()[0].toString();
String subject=msg.getSubject();
String sendDate=DateFormat.getInstance().format(msg.getSentDate());
out.println("邮件主题:" + subject + "
");
out.println("发件人:" + from + "
");
out.println("发送日期:" + sendDate + "
");
System.out.println("contentType:" +msg.getContentType());//如果该邮件是组合型"multipart/*"则可能包含附件等
if (msg.isMimeType("multipart/*")) {
Multipart mp=(Multipart) msg.getContent();for (int i = 0; i < mp.getCount(); i++) {
BodyPart bp=mp.getBodyPart(i);//如果该BodyPart对象包含附件,则应该解析出来
if (bp.getDisposition() != null) {
String filename=bp.getFileName();
System.out.println("filename:" +filename);if (filename.startsWith("=?")) {//把文件名编码成符合RFC822规范
filename =MimeUtility.decodeText(filename);
}//生成打开附件的超链接
out.print("附件:");
out.print("" + filename + "
");
}
}
}
}catch(Exception e) {
e.printStackTrace();
}
}
}
DisplayHead.java
用于显示邮件头内容
packagemail;importjava.io.IOException;importjavax.mail.BodyPart;importjavax.mail.Folder;importjavax.mail.Message;importjavax.mail.Multipart;importjavax.servlet.ServletException;importjavax.servlet.ServletOutputStream;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjavax.servlet.http.HttpSession;
@SuppressWarnings("serial")public class DisplayContent extendsHttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {
ServletOutputStream sos=response.getOutputStream();
HttpSession session=request.getSession();int msgnum = Integer.parseInt(request.getParameter("msgnum"));
Folder folder= (Folder) session.getAttribute("folder");try{
Message msg=folder.getMessage(msgnum);//邮件类型不是mixed时,表示邮件中不包含附件,直接输出邮件内容
if (!msg.isMimeType("multipart/mixed")) {
response.setContentType("message/rfc822");
msg.writeTo(sos);
}else{//查找并输出邮件中的邮件正文
Multipart mp =(Multipart) msg.getContent();int bodynum =mp.getCount();for (int i = 0; i < bodynum; i++) {
BodyPart bp=mp.getBodyPart(i);/** MIME消息头中不包含disposition字段, 并且MIME消息类型不为mixed时,
* 表示当前获得的MIME消息为邮件正文*/
if (!bp.isMimeType("multipart/mixed") && bp.getDisposition() == null) {
response.setContentType("message/rfc822");
bp.writeTo(sos);
}
}
}
}catch(Exception e) {
e.printStackTrace();
}
}
}
DisplayContent.java
用于显示邮件正文
packagemail;importjava.io.IOException;importjava.io.InputStream;importjavax.mail.BodyPart;importjavax.mail.Folder;importjavax.mail.Message;importjavax.mail.Multipart;importjavax.servlet.ServletException;importjavax.servlet.ServletOutputStream;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjavax.servlet.http.HttpSession;
@SuppressWarnings("serial")public class HandleAttach extendsHttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {
response.setContentType("text/html");
HttpSession session=request.getSession();
ServletOutputStream out=response.getOutputStream();int msgnum = Integer.parseInt(request.getParameter("msgnum"));int bodynum = Integer.parseInt(request.getParameter("bodynum"));
String filename= request.getParameter("filename");
Folder folder= (Folder) session.getAttribute("folder");try{
Message msg=folder.getMessage(msgnum);//将消息头类型设置为附件类型
response.setHeader("Content-Disposition", "attachment;filename=" +filename);
Multipart multi=(Multipart) msg.getContent();
BodyPart bodyPart=multi.getBodyPart(bodynum);
InputStream is=bodyPart.getInputStream();int c = 0;while ((c = is.read()) != -1) {
out.write(c);
}
}catch(Exception e) {
e.printStackTrace();
}
}
}
HandleAttact.java
用于处理附件
mailDemo
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
DisplayHead
mail.DisplayHead
DisplayHead
/DisplayHead
DisplayContent
mail.DisplayContent
DisplayContent
/DisplayContent
HandleAttach
mail.HandleAttach
HandleAttach
/HandleAttach
web.xml