Java Mail API Show by mail
Calling read in the jsp javamail found only the e-mail, no e-mail watching, and watching the mail is a very troublesome code conversion, so writing this article out to take a look at. If mistakes please write to me.
Use Java Mail API method, the main operations:
â— access javax.mail.Session examples, and thus be javax.mail.Store examples.
â— use examples were javax.mail.Store default Write me (INBOX) javax.mail.Folder examples.
â— use javax.mail.Folder Object Extraction new e-mail, storage array for javax.mail.Message object.
â— create javax.mail.FetchProfile to (potentially) optimization of specific components of the e-mail extraction.
Here javax.mail.FetchProfile category agreement to provide e-mail providers optional parameters, to be more effective in achieving the composition of the pre-mail extraction.
One example:
<% @ Page language = "java" contentType = "text / html; gb2312 charset ="%>
<% @ Page import = "java.util .*, java.io. *, java.text .*"%>
<% @ Page import = "javax.mail .*, javax.mail.internet .*, javax.activation .*"%>
<%!
Private String getDisplayAddress (Address a) / / conversion to Unicode RFC822
(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;
)
%>
<%
Session mailsession = Session.getInstance (System.getProperties (), null);
Mailsession.setDebug (false);
Store store = mailsession.getStore (protocol); / / protocol for connectivity protocols, IMAP or POP
Store.connect (mailhost, -1, user, passwd); / / mailhost mainframe user for the user name, password for the passwd
Folder folder = store.getFolder ( "INBOX");
Try
(Folder.open (Folder.READ_WRITE);)
Catch (MessagingException ex)
(Folder.open (Folder.READ_ONLY);)
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;
For (int i = 0; i <request.getParameter ( "id"). Length (); i ++)// assumptions to read a paragraph e-mail ID.
Id = id * 10 + (request.getParameter ( "id"). CharAt (i) -48);
For (j = 0; j <id; j + +);
Message [j]. SetFlag (Flags.Flag.SEEN, true);
Out.println ( "<table width = \" 550 \ "border = \" 1 \ "cellpadding = \" 0 \ "cellspacing = \" 0 \ "borderColorDark = \" # eaf0ff \ "borderColorLight = \" # 000000 \ "align = \" center \ ">");
Out.println ( "<tr>");
Out.println ( "<td width=60> theme: </ td> <td width=490>" [j] + message. GetSubject () + "</ td> </ tr>");
String from = new String ();
Address fr [] [j] = message. GetFrom ();
If (fr! = Null)
(Boolean tf = true;
For (int i = 0; i <fr.length; i + +)
From = from + getDisplayAddress (fr);
)
Out.println ( "<tr> <td width=60> from: </ td> <td width=490>" + from + "</ td> </ tr>");
Out.println ( "<tr> <td colspan=\"2\"> <div align=\"center\"> <b> content </ b> </ div> <br>");
Object o = message [j]. GetContent ();
If (message [j]. IsMimeType ( "text / plain")) / / if the Type for tex / plain can be directly read out.
Out.println ((String) o + "</ td>");
Else if (message [j]. IsMimeType ( "multipart /*"))
Multipart mp = ((Multipart) o;
Part part mp.getBodyPart = (0);
String msg = (String) part.getContent ();
StringBuffer buf = new StringBuffer (msg.length () +6);
Char ch = '';
For (int i = 0; i <msg.length (); ++)// if i encountered on the trip to exchange <br>
(Ch = msg.charAt (i);
If (ch == '\ n') buf.append ( "<br>");
Else buf.append (ch);
)
Out.println (buf.toString ());
)
Else
Out.println ( "It's not show:" [j] + message. GetContentType ());
Out.println ( "</ tr>");
Out.println ( "<table>");
Folder.close (true);
Store.close ();
%>
In Folder object can be used getMessageCount () and getUnreadMessageCount () method to obtain a total of the number of letters and the total number of letters read.






