JavaMail Getting started the fourth post receiving mail

Source: Internet
Author: User
Tags how to send mail mail account

In the third post of JavaMail, we learned how to send mail using the transport class provided by the JavaMail API, andJavaMail API Also provides some specialized classes to perform related operations on the receipt of messages, before introducing these classes, let us first understand the architecture of the mail receiving API, JavaMail An Java.mail.Store class is defined in the API that is used to perform the receiving task of the message, and the methods in this class are called in the program to obtain information about the individual mail folders in the mailbox. JavaMail uses a Folder object to represent a mail folder, and you can get all the message information in the Mail folder by means of a folder object, and we know that the message can be represented by the message object, and the message class contains various ways to manipulate it. For example, get mail sender, subject, body content, send time, etc., they work as follows:

1, from the session object to achieve a certain type of mail delivery protocol store object;

2, login mailbox, connect on POP3 or IMAP4 server;

3. Call the store's GetFolder method to get the folder object of a mail folder in the mailbox;

4. Call the GetMessage or Getmessages method in the Folder object to get a message or all messages in the Mail folder, and each message is returned as a message object.

Next we use the above class to receive mail

1 Importjava.util.Properties;2 3 Importjavax.mail.Address;4 ImportJavax.mail.Folder;5 ImportJavax.mail.Message;6 Importjavax.mail.Session;7 ImportJavax.mail.Store;8 9 classmailreceives {Ten      Public Static voidMain (string[] args)throwsException { One         //Defining property information for a connection POP3 server AString pop3server = "pop.qq.com"; -String protocol = "POP3"; -String username = "[email protected]"; theString password = "GVBRKIKFCKXGBEAC"; -  -Properties props =NewProperties (); -Props.setproperty ("Mail.transport.protocol", protocol);//protocol Used (JavaMail specification requirements) +Props.setproperty ("Mail.smtp.host", pop3server);//SMTP server address for sender's mailbox -  +         //Get Connections ASession session =session.getdefaultinstance (props); atSession.setdebug (false); -  -         //Get Store Object -Store store =session.getstore (protocol); -Store.connect (pop3server, username, password);//login authentication for POP3 server -          in         //when this method is called by the store object through the POP3 protocol, the mail folder name can only be specified as "INBOX" -Folder folder = Store.getfolder ("INBOX");//get the user's mail account toFolder.open (Folder.read_write);//set access rights to mail accounts +  -message[] messages = Folder.getmessages ();//get all messages in a mailbox account the  *          for(Message message:messages) { $String subject = Message.getsubject ();//get the message subjectPanax NotoginsengAddress from = (address) message.getfrom () [0];//Get Sender Address -System.out.println ("The subject of the message is:" + subject + "\ T sender address is:" +from ); theSYSTEM.OUT.PRINTLN ("Message content is:" +message.getcontent ()); +         } A  theFolder.close (false);//Close the Mail folder object +Store.close ();//Close the Connection object -     } $}

The GetContent method in message is used to return the contents of the message body, which returns the contents of the message body as a Java object, the type of the object is determined by the message body content, and when the MIME type of the message content is "Text/plain", a String object is returned. When the message content is a "multipart" type, the return is an implementation class for the multipart, and when the MIME type of the message content is indeterminate, the InputStream stream associated with the content is returned.

JavaMail Getting started the fourth post receiving mail

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.