How to obtain the email information?

Source: Internet
Author: User
Tags dateformat

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 ){
}
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.