Package test;
Import java. text. SimpleDateFormat;
Import java. util. Date;
Import java. util. Properties;
Import javax. mail. Folder;
Import javax. mail. Message;
Import javax. mail. MessagingException;
Import javax. mail. Session;
Import javax. mail. Store;
Import javax. mail. URLName;
Import javax. mail. internet. InternetAddress;
Import javax. mail. internet. MimeMessage;
Import com. sun. mail. pop3.POP3Folder;
/**
* A ReciveMail object needs to be created for an email.
*/
Public class ReciveOneMail {
Private MimeMessage mimeMessage = null;
Private String dateformat = "yyyy-MM-dd HH: mm"; // default day-to-day display format
Public ReciveOneMail (MimeMessage mimeMessage ){
This. mimeMessage = mimeMessage;
}
Public void setMimeMessage (MimeMessage mimeMessage ){
This. mimeMessage = mimeMessage;
}
/**
* Obtain the sender's address and name.
*/
Public String getFrom () throws Exception {
InternetAddress address [] = (InternetAddress []) mimeMessage. getFrom ();
String from = address [0]. getAddress ();
If (from = null)
From = "";
String personal = Address [0]. getpersonal (); // sender's name
If (Personal = NULL)
Personal = "";
String fromaddr = personal + "<" + from + ">"; // sender's address
Return fromaddr;
}
/**
* Obtain the subject of the email.
*/
Public String getsubject () throws messagingexception {
String subject = "";
Try {
Subject = mimemessage. getsubject ();
String header = (MimeMessage) mimeMessage). getHeader ("SUBJECT") [0];
If (header. toLowerCase (). indexOf ("=? ")> 0 ){
Subject = new String (subject. getBytes ("ISO-8859-1"), "gb2312 ");
}
If (subject = null)
Subject = "";
} Catch (Exception exce ){
}
Return subject;
}
/**
* Get the mail sending date
*/
Public String getSentDate () throws Exception {
Date sentdate = mimeMessage. getSentDate ();
SimpleDateFormat format = new SimpleDateFormat (dateformat );
Return format. format (sentdate );
}
/**
* Obtain the Message-ID of the email.
*/
Public String getMessageId () throws MessagingException {
Return mimeMessage. getMessageID ();
}
/**
* [Set the date display format]
*/
Public void setDateFormat (String format) throws Exception {
This. dateformat = format;
}
/**
* PraseMimeMessage class test
*/
Public static void main (String args []) throws Exception {
Properties props = System. getProperties ();
Props. put ("mail. smtp. host", "mail.socix.com ");
Props. put ("mail. smtp. auth", "true ");
URLName url = new URLName ("pop3", "mail.socix.com", 110, null,
"Xcf@socix.com", "111111 ");
System. Out. println ("Log on! ");
Session session = session. getinstance (system. getproperties (), null );
Store store = session. getstore (URL );
Pop3folder inbox = NULL;
Try {
Store. Connect ();
System. Out. println ("store connected! ");
Inbox = (pop3folder) store. getfolder ("inbox ");
Inbox. Open (Folder. read_only );
Message [] message = inbox. getMessages ();
For (int I = 0; I <message. length; I ++ ){
System. out. println ("Message" + I + "UID :"
+ Inbox. getUID (message [I]);
}
System. out. println ("fetched! ");
For (int I = 0; I <message. length; I ++ ){
ReciveOneMail pmm = new ReciveOneMail (MimeMessage) message [I]);
System. out. println ("th" + (I + 1) + "");
System. out. println ("===================== ");
System. out. println ("Message" + (I + 1) + "topic :"
+ Pmm. getSubject (); // obtain the topic
System. out. println ("Message" + (I + 1) + "Date :"
+ Pmm. getSentDate (); // obtain the mail sending date
System. out. println ("Message" + (I + 1) + "Sender :"
+ Pmm. getFrom (); // get the sender and address
System. out. println ("Message" + (I + 1) + "UID :"
+ Inbox. getUID (message [I]);
}
} Finally {
Try {
Inbox. close (false );
} Catch (Exception e ){
}
Try {
Store. close ();
} Catch (Exception e ){
}
}
}
}