JavaMail Simulated mailbox Function-------------------intermediate combat article "message tagging method" (JavaMail API Email Instance

Source: Internet
Author: User


  JavaMail jar Package:

  This chapter may be the last one to explain JavaMail, the last update after the review method, the plan to update the weekend, did not want to be busy today Friday, summary executions here forget ... I'll finish it before work.  Before reading this chapter, it is recommended to read the previous chapters , as this chapter is based on previous additions to the business method, not looking at the previous may be a bit foggy. point me to

Message flag: Refers to a property field in the flag inner class that is set to the message Message object, and the flag type is given in the code.

This section shows a delete flag for the message to delete the message.

Code Walk//////

Markemailfunction Marking Mail Business class;;Importjavax.mail.Flags;ImportJavax.mail.Flags.Flag;ImportJavax.mail.Folder;ImportJavax.mail.Message;Importjavax.mail.MessagingException;/*** Mark Mail Business class (inclusion delete) *@authorLiuyt * @date 2014-10-31 pm 3:04:39 * Bolgs */ Public classMarkemailfunctionextendslookemailservice{/*** Because I inherited the view business class, so here I can call the parent class method to get the message, reduce the amount of code *@throwsunsupportedencodingexception *@throwsmessagingexception*/     PublicMarkemailfunction ()throwsunsupportedencodingexception, messagingexception {}/*** Flag The message method (in fact, in the previous chapter has the contact, is folder.read_only opens the Mail folder with the read-only method) * Each message message has a corresponding real-time flag, server-defined, and user-defined * server predefined are internal Constant existence in class Flags.flag *@paramFlag Status ID *@throwsmessagingexception *@throwsunsupportedencodingexception*/     Public voidMarkemail (Flag flag)throwsunsupportedencodingexception, messagingexception {/**get an email, get the latest email in your inbox, delete it, and test it yourself*/Message Message= This. Getmessagebyindex ( This. Getallmessage (). length); Message.setflag (Flags.Flag.DELETED,true);//true to set, False to cancel        /*** The identification found in the source code for reference only * answered reply * DELETED Delete * DRAFT Draft *         FLAGGED read (Personal understanding) * Recent.         * SEEN. * USER: (Still not understand-.-)*/        //important step, now the Mail has not really deleted, just have a delete tag, you need to refresh the Mail folder like refresh Stream//the previous section said false does not refresh, true refresh, at this time the message deleted (Important message please be careful to operate)         This. GetFolder (). Close (true); }        /*** There is a need to rewrite the parent class's initialization folder method, because the previous section for security, we opened the Mail folder as read-only * here to change the properties of the parent class of the previous section to protected, which the subclass can access (no longer post code)*/@Override Public voidInitstoreandfolder ()throwsunsupportedencodingexception, messagingexception { This. Emailservice =NewEmailserviceenity ("", "" ", EMAILSERVICEENITY.MAIL_PROTOCOL_POP3);  This. store =emailservice.getsession (). GetStore (Emailservice.getmailprotocol ());        Store.connect (Emailservice.gethostport (), Emailserviceenity.username, Emailserviceenity.password); Folder= Store.getfolder ("INBOX"); //The above method no longer repeat, do not understand a chapter, here just change the following open method for (Folder.read_write); }        /*** * Simple Main Method Test * * **/     Public Static voidMain (string[] args) {markemailfunction mark; Try{Mark=Newmarkemailfunction (); //Note that flag is a static inner class, and you want to use an external class to getMark.markemail (Flags.Flag.DELETED); } Catch(unsupportedencodingexception e) {e.printstacktrace (); } Catch(messagingexception e) {e.printstacktrace (); }    }}


    • Messages are marked to be built on a view basis, so directly inherit the view business class
    • To open the folder Mail folder as read-write (read_write) when you want to delete a message
    • After tagging the message, remember to refresh the Mail folder when you close the Mail folder (parameter: TRUE)
    • As long as the previous practice, here will not wordy, ... omitted here .... 1w Word .....

JavaMail API Series:

    1. JavaMail Email Demo Email-Basic Combat
    2. JavaMail e-Mail Demo email-Intermediate Combat article "new attachment Sending method"
    3. JavaMail Demo mailbox to get the message Content-Intermediate Combat chapter "Content | Attachment Download Method"
    4. JavaMail Simulation Mailbox Function--mail Reply-Intermediate combat article "Mail Reply Method"
    5. JavaMail Demo Mailbox Function--------------intermediate combat article "message tagging method"

Writing is not easy, inevitably there are omissions and mistakes, but also please be generous, feel helpful also please recommend Oh

PS: Welcome reprint, Reprint please indicate source:

                                      Learn a little bit more everyday code less knock a little bit

JavaMail Simulated mailbox Function-------------------intermediate combat article "message tagging method" (JavaMail API Email Instance

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: 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.