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" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title> View Mailing Lists </title>
<body>
<div align= "center" >
<%
Session mailsession = session.getinstance (system.getproperties (), NULL);
Mailsession.setdebug (FALSE);
Protocol for Connection protocol, IMAP or POP
Store store = Mailsession.getstore ("POP3");
Pop.126.com is the host
Store.connect ("pop.126.com", "scau2008", "1351035598");
Get file information from the Inbox folder on the mail server
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);
Get message information from your Inbox and save them to a stack in message form
Message message[] = Folder.getmessages ();
Out.println ("+message.length+" Mail </p> "in the inbox of <p align=center> email [email protected]);
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> number </th>");
Out.println ("<th width=15% align=center> title </th>");
Out.println ("<th width=15% align=center> sender </th>");
Out.println ("</tr>");
Cycle through message information
for (int i = 0; i < message.length; i++) {
Get sender Information
if (!message[i].getfolder (). IsOpen ())//Determine if Open
Message[i].getfolder (). open (Folder.read_write); If close, re-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 ();
}
Display message Information
Out.println ("<tr>");
Out.println ("<td width=15% align=center>" + (i+1) + "</td>");
Out.println ("<td width=60% align=left> <a href= ' mail_detail.jsp?id=" +i+ ">"
+message[i].getsubject () + "</a></td>");
Out.println ("<td width=25% align=left> " +from+ "</td>");
Out.println ("</tr>");
Close the Folder object and store object
Folder.close (TRUE);
Store.close ();
}
%>
</div>
</body>
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) {
Convert RFC822 to 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" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title> View message Content </title>
<body>
<div align= "center" >
<%
Session mailsession = session.getinstance (system.getproperties (), NULL);
Mailsession.setdebug (FALSE);
Protocol for Connection protocol, TMap or pop
Store store = Mailsession.getstore ("POP3");
//
Store.connect ("pop3.126.com", "scau2008", "1351035598");
Get file information from the Inbox folder on the mail server
Folder folder = Store.getfolder ("INBOX");
try{
Folder.open (Folder.read_write);
}
catch (Messagingexception ex) {
Folder.open (folder.read_only);
}
Get mail messages from your Inbox and save them to a stack in message form
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;
Suppose you want to read an ID message
for (int i = 0; i < Request.getparameter ("id"). length (); i++) {
id = ID * + + (Request.getparameter ("id"). charAt (i)-48);
}
for (j=0;j<id;j++) {
;
}
Message[j].setflag (Flags.Flag.SEEN, true);
Out.println ("
Out.println ("<table width=700 border=1 cellspacing=0 cellpadding=0>");
Out.println ("<tr>");
Out.println ("<td width=100 height=25> title:</td>");
Out.println ("<td width=600 height=25 align=left>" +message[j].getsubject () + "</td>");
Out.println ("</tr>");
Get sender Information
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> sender:</td>");
Out.println ("<td height=25 align=left>" +from+ "</td>");
Out.println ("</tr>");
Out.println ("<tr>");
Out.println ("<td> e-mail content:</td>");
Out.println ("<td align=left>");
Get the message content to display
Object o = message[j].getcontent ();
If the type is Text/plain, you can read it directly.
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 = ';
Switch to <br> if you encounter a line break
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 ("Temporarily unable to display:" +message[j].getcontenttype ());
}
Out.println ("</tr>");
Out.println ("<table>");
Close the Folder object and store object
Folder.close (TRUE);
Store.close ();
%>
</div>
</body>