Javamail simulate mailbox function-delete mails-intermediate practice-[mail Tag Method] (javamail API email instance), javamailapi

Source: Internet
Author: User

Javamail simulate mailbox function-delete mails-intermediate practice-[mail Tag Method] (javamail API email instance), javamailapi

Preface:

  JavaMail jar package:Http://java.sun.com/products/javamail/downloads/index.html

  This chapter may be the last article to explain javamail. After the last update, I planned to update it over the weekend. I didn't think it would be idle on Friday. I just figured it out... I will finish writing it before work. Before reading this section, we recommend that you read the previous section, because this chapter is based on the Business Methods Added previously, and may be a bit confused without looking at the previous sections.Click me to go

Email Flag: sets an attribute field in the Flag internal class for the message object. The Flag type is provided in the Code.

This section describes how to delete an email by giving it a delete sign.

Start code //////

MarkEmailFunction marking mail business class

Package com. java. mail; import java. io. unsupportedEncodingException; import javax. mail. flags; import javax. mail. flags. flag; import javax. mail. folder; import javax. mail. message; import javax. mail. messagingException;/*** mark the mail service class (include deleting) * @ author liuyt * @ date 3:04:39 * bolgs http://www.cnblogs.com/liuyitian/ */public class MarkEmailFunction extends LookEmailService {/*** because I inherited the view service class, therefore, when obtaining an email, I can directly call the method of the parent class to obtain it, reducing the amount of code * @ throws UnsupportedEncodingException * @ throws MessagingException */public MarkEmailFunction () throws UnsupportedEncodingException, messagingException {}/*** mark the mail method (in fact, there is a contact in the previous chapter, that is, Folder. READ_ONLY opens the folder in read-only mode) * Each message has a corresponding real-time mark, defined by the server, there are also user-defined * server pre-defined all internal class Flags. * @ param Flag status identifier * @ throws MessagingException * @ throws UnsupportedEncodingException */public void markEmail (flag Flag) throws UnsupportedEncodingException, messagingException {/** get an email. Here, we get the latest email in the inbox for deletion. Other methods can be used to test the Message. */message Message = this. getMessageByIndex (this. getAllMessage (). length); message. setFlag (Flags. flag. DELETED, true); // true indicates the setting, and false indicates the identifier found in the source code, for reference only * ANSWERED reply * DELETED Delete * DRAFT * FLAGGED read (personal understanding) * RECENT .. * SEEN .. * USER .. (not clear yet -. -) * // important step. The email has not been deleted yet, but there is a delete mark, you need to refresh the email folder like a refresh stream. // the previous section says "false" does not refresh, and "true" refreshes the email. Delete the email at this time (perform this operation with caution for important emails. getFolder (). close (true);}/*** the initialization folder method of the parent class needs to be rewritten here, because in the previous chapter, for security, we open the folder in read-only mode * Here we should change the attribute of the parent class in the previous chapter to protected, and the sub-class can be accessed (the code is not pasted here) */@ Override public void initStoreAndFolder () throws UnsupportedEncodingException, MessagingException {this. emailService = new EmailServiceEnity ("", "", EmailServiceEnity. MAIL_PROTOCOL_POP3); this. store = emailService. getSession (). getStore (emailService. getMailProtocol (); store. connect (emailService. getHostPort (), EmailServiceEnity. username, EmailServiceEnity. password); folder = store. getFolder ("INBOX"); // do not repeat the above method. If you do not understand it, read the previous chapter. Here you only need to change the following method to READ_WRITE folder. open (Folder. READ_WRITE);}/***** test the simple main method *****/public static void main (String [] args) {MarkEmailFunction mark; try {mark = new MarkEmailFunction (); // note that the Flag is a static internal class and you must use an external class to obtain the mark. markEmail (Flags. flag. DELETED);} catch (UnsupportedEncodingException e) {e. printStackTrace ();} catch (MessagingException e) {e. printStackTrace ();}}}

Summary:

  • The mail tag is based on the view to be created, so it directly inherits the view service class
  • To delete a mail, you must open the Folder in READ_WRITE mode.
  • After adding a tag to the email, remember to refresh the folder when closing the email folder (specifically, the parameter is set to true)
  • As long as you have practiced all the exercises before, you will not be so embarrassed here ,........ omitted here ....... 1 w words .........

Javamail API series:

 

It is not easy to write, and it is inevitable that there are omissions and errors. Please make a generous correction and recommend it if you think it is helpful.

Ps: Welcome to reprint, reprint please indicate the source: http://www.cnblogs.com/liuyitian/p/4065654.html

Learn a little more code every day

 


When using the JavaMail API to send an email, you can obtain the correct session Object

The session object has nothing to do with the JavaMail API. Of course, you can use the session object as the parameter when using the JavaMail API.
 
How to Use javamail to receive emails in enterprise mail

This is the display format of a Netease enterprise email sending mail.
Sender: "cloud computing" <w ***** @ ***. com>

Recipient: "***** @ *****. com" <****** @ ****. com>

Compare the sender: abc: def <xxx@nappor.net> def is the sender, abc: def should be the sender's custom nickname, can be arbitrarily modified.

The imap protocol is usually called the mail synchronization protocol. The mail system you use must determine whether the protocol is supported. Yes. Please give the score. Thank you.
 

Related Article

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.