JavaMail entry 4 receiving emails, javamail 4

Source: Internet
Author: User

JavaMail entry 4 receiving emails, javamail 4

In the third article of the last article JavaMail, we learned how to use the Transport class provided by the JavaMail API to send emails. Similarly, the JavaMail API also provides some specialized classes for receiving emails. Before introducing these classes, let's take a look at the architecture of the mail receiving API, the JavaMail API defines a java. mail. store class, which is used to execute the mail receiving task. We can call this class in the program to obtain the information of each folder in the mailbox. JavaMail uses the Folder object to indicate the Folder. You can use the Folder object method to obtain all the mail Information in the Folder. We know that the mail information can be expressed by the Message object, the Message class contains various methods for operating the mail, such as obtaining the mail sender, subject, body content, and sending time. Their working relationships are as follows:

1. Obtain the Store object that implements a mail sending protocol from the Session object;

2. log on to your mailbox and connect to the POP3 or IMAP4 server;

3. Call the getFolder method of Store to obtain the Folder object of a Folder in the mailbox;

4. Call the getMessage or getMessages method in the Folder object to obtain one or all emails in the Folder. Each email is returned as one Message object.

Next, we use the above class to receive emails.

1 import java. util. properties; 2 3 import javax. mail. address; 4 import javax. mail. folder; 5 import javax. mail. message; 6 import javax. mail. session; 7 import javax. mail. store; 8 9 class MailReceives {10 public static void main (String [] args) throws Exception {11 // defines the attribute information 12 String pop3Server = "pop.qq.com "; 13 String protocol = "pop3"; 14 String username = "552482793@qq.com"; 15 String password = "gvbrkikfckxgbeac"; 16 17 Properties props = new Properties (); 18 props. setProperty ("mail. transport. protocol ", protocol); // used protocol (JavaMail specification requirements) 19 props. setProperty ("mail. smtp. host ", pop3Server); // SMTP server address of the sender's mailbox 20 21 // get connection 22 Session session = Session. getDefaultInstance (props); 23 session. setDebug (false); 24 25 // get the Store object 26 Store = session. getStore (protocol); 27 store. connect (pop3Server, username, password); // POP3 Server login authentication 28 29 // when the Store object is obtained through POP3 protocol to call this method, the Folder name can only be specified as "INBOX" 30 folder Folder = store. getFolder ("INBOX"); // get your email account 31 folder. open (Folder. READ_WRITE); // set the email account access permission 32 33 Message [] messages = folder. getMessages (); // get all emails in the email account 34 35 for (Message message: messages) {36 String subject = message. getSubject (); // obtain the subject 37 Address from = (Address) message. getFrom () [0]; // get the sender address 38 System. out. println ("subject:" + subject + "\ t sender address:" + from); 39 System. out. println ("email content:" + message. getContent (); 40} 41 42 folder. close (false); // close the folder object 43 store. close (); // close the connection object 44} 45}

The getContent method in Message is used to return the content of the email body. This method returns the content of the email body as a Java object. The object type is determined by the body content, when the MIME type of the mail content is "text/plain", a String object is returned. When the mail content is of the "multipart" type, a Multipart implementation class is returned, when the MIME type of the mail content is unknown, the InputStream stream associated with the content is returned.

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.