Sample Code for receiving mails using Java mail

Source: Internet
Author: User

Javamail is very troublesome to receive emails. Make a lot of judgments, such as the Chinese characters of attachments and the storage of attachments. This code is not complete. For reference only.

/**
* Store the recipient in the system.
*
* @ Param sess
* @ Param PMM
* @ Return
*/
Private Boolean storereceivemail (Session sess, platmessagemailset PMM) throws exception {
Store store = NULL;
Pop3folder folder = NULL;
Long size = 0;
Try {
Urlname url = new urlname ("POP3", PMM. getpop3 (), 110, "", PMM. GetUserName (), PMM. GetPassword ());
Javax. Mail. Session S = javax. Mail. session. getdefainstance instance (system. getproperties (), null );
Store = S. getstore (URL );
// If (Folder = NULL) throw new exception ("no default folder ");
// If it is an inbox
Store. Connect ();
Folder = (pop3folder) store. getfolder ("inbox ");
If (Folder = NULL) throw new exception ("No POP3 inbox ");
// Open the inbox in read-only mode
Folder. Open (Folder. read_write );
// Get the Folder Information and obtain the mail list
Fetchprofile profile = new fetchprofile ();
Profile. Add (uidfolder. fetchprofileitem. UID );
Profile. Add (fetchprofile. item. envelope );
Message [] msgs = folder. getmessages ();
Folder. Fetch (msgs, profile );
For (INT msgnum = 0; msgnum <msgs. length; msgnum ++ ){
Message message = (MSGs [msgnum]);
If (! Message. getflags (). Contains (flags. Flag. Seen )){
Platmessage PM = new platmessage ();
PM. setreceiver (PMM. getowner ());
PM. setrec_email (PMM. getmailaddr ());
PM. setsend_date (message. getsentdate ());
PM. setfolder ("inbox ");
PM. setmsg_tag (2 );
PM. setcreate_date (new date ());
PM. setistag (0 );
PM. setacctag (0 );
/// // Save the email
// Get the email address
String text = "";
String from = (internetaddress) message. getfrom () [0]). getpersonal ();
// Internetaddress IA = new internetaddress (from );
If (from = NULL) from = (internetaddress) message. getfrom () [0]). getaddress ();
PM. setsender (from );
// Obtain the topic
String subject = message. getsubject ();
PM. setmsg_title (subject );
System. Out. println ("received email, from:" + from + ". Topic: "+ subject );
// Obtain the information object
Part messagepart = message;
Object content = messagepart. getcontent ();
String acc_id = "";
System. Out. println ("content" + content );
If (content instanceof string ){
// Attachment
TEXT = string. valueof (content );
System. Out. println ("here" + text );
} Else if (content instanceof multipart ){
Multipart MP = (multipart) content );
For (int K = 0; k <MP. getcount (); k ++ ){
Bodypart BP = mp. getbodypart (k );
Object T = bp. getcontent ();
If (T instanceof string ){
TEXT = (string. valueof (t ));
System. Out. println ("here 1" + text );
} Else if (T instanceof inputstream ){
String physical = uuidhex. getuuidhex ();
Filecontrol fc = new filecontrol ();
FC. setmyfile (new file (message_acc_path + "\" + Physical ));
FC. createfile ();
FC. writefile (BP. getinputstream ());
Platmsgaccessary PMA = new platmsgaccessary ();
PMA. setfile_name (stringutil. getstring (mailutil. decodetext (BP. getfilename (), "unknown file name "));
PMA. setphysical (physical );
PMA. setfile_size (BP. getsize ());
Sess. Save (PMA );
Acc_id + = physical + "| ";
System. Out. println ("received attachment, name:" + mailutil. decodetext (BP. getfilename () + ". Size: "+ bp. getsize ());
Size + = bp. getsize ();
PM. setacctag (1 );
}
}
}
PM. setacc_id (acc_id );
/**
// Obtain the content type
String contenttype = messagepart. getcontenttype ();
// If the email content is plain text or HTML, print the information
String context = "";
If (contenttype. startswith ("text/plain") |
Contenttype. startswith ("text/html ")){
Inputstream is = messagepart. getinputstream ();
Bufferedreader reader = new bufferedreader (New inputstreamreader (is ));
String thisline = reader. Readline ();
Context + = thisline;
While (thisline! = NULL ){
Thisline = reader. Readline ();
Context + = thisline;
}
Size + = context. Length ();
}
**/
PM. setmsg_size (message. getsize ());
PM. setmsg_sys ("sseip ");
// Pm. setmsg_text (context );
Sess. Save (PM );
Sess. Flush ();
System. Out. println ("email body:" + text );
Savemessagetext (sess, PM. getmsg_id (), text );//
If (PMM. getdeltag () = 1 ){
Message. setflag (flags. Flag. Deleted, true );
} Else {
Message. setflag (flags. Flag. Seen, true );
}
}
// Folder. Expunge ();

//////////////////////////////////////// End
}
Return true;
} Catch (exception ex ){
Throw ex;
} Finally {
If (folder! = NULL ){
Folder. Close (true );
}
Store. Close ();
}
}

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.