首页 > 代码库 > JSP中的邮件收发(2)

JSP中的邮件收发(2)

mail_list.jsp

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%@ page import="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=UTF-8">
<title>查看邮件列表</title>
</head>
<body>
<div align="center">
<h1>查看邮件列表</h1>

<%
Session mailsession = Session.getInstance(System.getProperties(),null);
mailsession.setDebug(false);
//protocol为连接协议,IMAP或是POP
Store store = mailsession.getStore("pop3");
//pop.126.com是主机
store.connect("pop.126.com", "scau2008", "1351035598");
//获得邮件服务器上INBOX文件夹中的文件信息
Folder folder = store.getFolder("INBOX");
try{
folder.open(Folder.READ_WRITE);
System.out.println(0);
}
catch(MessagingException ex){
folder.open(Folder.READ_ONLY);
System.out.println(1);
}
System.out.println(2);
//获得收件箱中的邮件信息,将他们保存到Message形式的栈中
Message message[] = folder.getMessages();
out.println("<p align=center>邮箱scau2008@126.com的收件箱中共有"+message.length+"件邮件</p>");
out.println("<div align=center>");
out.println("<table width=95% border=1 cellspacing=1 cellpadding=4>");
out.println("<tr>");
out.println("<th width=15% align=center>编号</th>");
out.println("<th width=15% align=center>标题</th>");
out.println("<th width=15% align=center>发信人</th>");
out.println("</tr>");

//循环显示邮件信息
for(int i = 0; i < message.length; i++){
//获得发信人信息
if(!message[i].getFolder().isOpen()) //判断是否open
message[i].getFolder().open(Folder.READ_WRITE); //如果close,就重新open
//String from = ((InternetAddress)message[i].getFrom()[0]).getPersonal();
String from = ((InternetAddress)message[i].getFrom()[0]).getPersonal();

if(from == null){
from = ((InternetAddress)message[i].getFrom()[0]).getAddress();
}
//显示邮件信息
out.println("<tr>");
out.println("<td width=15% align=center>"+(i+1)+"</td>");
out.println("<td width=60% align=left>&nbsp;<a href=http://www.mamicode.com/‘mail_detail.jsp?id="+i+"‘>"
+message[i].getSubject()+"</a></td>");
out.println("<td width=25% align=left>&nbsp;"+from+"</td>");
out.println("</tr>");

//关闭Folder对象和Store对象
folder.close(true);
store.close();
}
%>
</div>
</body>
</html>

 

mail_detail.jsp

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%@ page import="javax.mail.*" %>
<%@ page import="javax.mail.internet.*" %>
<%@ page import="javax.activation.*" %>
<%@ page import="java.net.*" %>
<%!
private String getDisplayAddress(Address a){
//转换RFC822为Unicode
String pers = null;
String addr = null;
if(a instanceof InternetAddress && ((pers=((InternetAddress)a).getPersonal())!=null))
addr = pers+" "+"<"+((InternetAddress)a).getAddress()+">";
else
addr = a.toString();
return addr;
}
%>
<!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=UTF-8">
<title>查看邮件内容</title>
</head>
<body>
<div align="center">
<h1>查看邮件内容</h1>
<%
Session mailsession = Session.getInstance(System.getProperties(),null);
mailsession.setDebug(false);
//protocol为连接协议,TMAP或是POP
Store store = mailsession.getStore("pop3");
//
store.connect("pop3.126.com", "scau2008", "1351035598");
//获得邮件服务器上INBOX文件夹中的文件信息
Folder folder = store.getFolder("INBOX");
try{
folder.open(Folder.READ_WRITE);
}
catch(MessagingException ex){
folder.open(Folder.READ_ONLY);
}


//获得收件箱中邮件信息,将他们保存到Message形式的栈中
Message message[] = folder.getMessages();
FetchProfile fp = new FetchProfile();
fp.add(FetchProfile.Item.ENVELOPE);
fp.add(FetchProfile.Item.FLAGS);
fp.add("X-Mailer");
folder.fetch(message, fp);
int id=0,j=0;

//假设要阅读第ID个邮件
for(int i = 0; i < request.getParameter("id").length(); i++){
id = id * 10+(request.getParameter("id").charAt(i)-48);
}

for(j=0;j<id;j++){
;
}
message[j].setFlag(Flags.Flag.SEEN, true);
out.println("<h1 align=center>查看邮件内容</h1>");
out.println("<table width=700 border=1 cellspacing=0 cellpadding=0>");
out.println("<tr>");
out.println("<td width=100 height=25>标题:</td>");
out.println("<td width=600 height=25 align=left>"+message[j].getSubject()+"</td>");
out.println("</tr>");

//获取发信人信息
String from = new String();
Address[] fr = message[j].getFrom();
if(fr != null){
for(int i = 0; i < fr.length; i++)
from = from+getDisplayAddress(fr[i]);
}
out.println("<tr>");
out.println("<td height=25>发信人:</td>");
out.println("<td height=25 align=left>"+from+"</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td>邮件内容:</td>");
out.println("<td align=left>");

//获取要显示的邮件内容
Object o = message[j].getContent();
//若其Type为text/plain就可直接读出了
if(message[j].isMimeType("text/plain")){
out.println((String)o+"</td>");
}else if(message[j].isMimeType("multipart/*")){
Multipart mp = (Multipart)o;
BodyPart part = mp.getBodyPart(0);
String msg = (String)part.getContent();
StringBuffer buf = new StringBuffer(msg.length()+6);
char ch = ‘ ‘;
//若遇到换行就转为<br>
for(int i = 0; i < msg.length(); i++){
ch = msg.charAt(i);
if(ch==‘\n‘){
buf.append("<br>");
}else{
buf.append(ch);
}
}
out.println(buf.toString());
}else{
out.println("暂时无法显示:"+message[j].getContentType());
}

out.println("</tr>");
out.println("<table>");
//关闭Folder对象和Store对象
folder.close(true);
store.close();
%>
</div>
</body>
</html>