All about JAVA javamail POP3 and IMAP read unread messages, etc.

Source: Internet
Author: User
Tags imap ssl connection

/** * 可以在props下设置认证信息,协议和端口信息,超时等信息,这样在store.connect的时候可以根 * 据props里是否设置了认证信息来选择不带参数的connect()或者如下有参数的connect(...)方法 *  store.connect(user, password); store.connect(host, user, password); */  Properties props = System.getProperties();  props.setProperty("mail.imap.host""mail.**.**.cn");  props.setProperty("mail.imap.port""143"); 具体端口信息由邮件提供商确定  props.setProperty("mail.imap.connectiontimeout""5000");  props.setProperty("mail.imap.timeout""5000");

Code Body:

Properties props = System.getproperties (); Store store =NULL;//used to store the folder in the mail, you can simply use the Javax.mail.Folder type,//If only the number of unread messages is taken, the folder type will suffice.Imapfolder Inbox =NULL;Try{Session session = Session.getdefaultinstance (props,NULL);//If using POP3 protocol here IMAP is changed to POP3, if use SSL connection here should use Imapsstore = Session.getstore ("IMAP"); Store.connect ("mail.**.**.cn","User","PWD");    SYSTEM.OUT.PRINTLN (store); Inbox = (Imapfolder) Store.getfolder ("Inbox");//Get Inbox object    //If you need to set the message to read when the number of messages is obtained, you need to use read_write here, otherwise read_only canInbox.open (Folder.read_write);//message messages[] = Inbox.getmessages ();//Get All messages    //Establish search conditions Flagterm, here flagterm inherit from Searchterm, that is to say, in addition to get unread mail    //pieces of conditions there are many other conditions also inherit the Searchterm conditional class, like according to sender, subject search, etc.    //There are complex logical searches like:    //         //Searchterm orterm = new Orterm (    //New Fromstringterm (from),    //New SubjectTerm (subject)    //            );     //      //Can search the Internet searchterm get moreFlagterm ft =NewFlagterm (NewFlags (Flags.Flag.SEEN),false);//false represents unread, true represents read    /** * Flag types are listed below * Flags.Flag.ANSWERED mail reply flag to identify whether the message has been replied to.    * Flags.Flag.DELETED Message Delete tag to identify whether the message needs to be deleted.    * Flags.Flag.DRAFT Draft message flag to identify whether the message is a draft.    * Flags.Flag.FLAGGED Indicates whether the message is a message in the Recycle Bin.    * Flags.Flag.RECENT new message flag to indicate whether the message is a new message.    * Flags.Flag.SEEN message reading flag to identify whether the message has been read.    * Flags.Flag.USER whether the underlying system supports user-defined markup, read-only. */Message messages[] = inbox.search (ft);//Get message based on set conditions    //Traversal of the obtained message array for information     for(Message message:messages) {//Returns the message type object by default because I need to get messageid so I need to do a mandatory        //Convert to Imapmessage typeImapmessage imes = (imapmessage) message;//I need to get the messages are plain text, so in this simple to make a judgment, not plain text directly skipped.         //If you need to read a message that is not pinned, you need to use Message.getcontenttype () to get the message        //body type, then further processing according to type        //If "Text/plain" or "text/html" is returned as plain text, if "multipart/*" then the body can be        //can also include information such as pictures        if(! (Message.getcontent ()instanceofString))Continue; map<string, string> map =NewHashmap<string, string> (); Map.put ("Content", (String) message.getcontent ()); Map.put ("title", Message.getsubject ());The //imapmessage type object can get MessageID and UID, both of which are differentiated MessageID is        //Mail unique identification, not limited to the current mail system, UID is the current mail system unique identification,        //Get MessageID need to read the message, UID does not need to read the message so faster. Map.put ("MessageId", Imes.getmessageid ()); Mailinfolist.add (map);//Save the information I want to get map listMeslist.add (message);//Save the message list I will set as read}//Set the message I have just taken as read    if(Meslist.size () >0) {message[] Savedmailmessage =NewMessage[meslist.size ()];        Meslist.toarray (Savedmailmessage); Inbox.setflags (Savedmailmessage,NewFlags (Flags.Flag.SEEN),true); }}Catch(Nosuchproviderexception e) {E.printstacktrace ();}Catch(Messagingexception e) {E.printstacktrace ();}Catch(IOException e) {//TODO auto-generated catch blockE.printstacktrace ();}finally{Try{if(Inbox! =NULL) {Inbox.close (false); Inbox =NULL; }if(Store! =NULL) {store.close (); store =NULL; }    }Catch(Messagingexception e) {//TODO auto-generated catch blockE.printstacktrace (); }}

All about JAVA javamail POP3 and IMAP read unread messages, etc.

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.