Previous articleIntroduction to JavaMail In the third article to send emails, we learned how to use the Transport class provided by the JavaMail API to send emails. Similarly, JavaMail API also provides some special classes to perform related operations on the reception of emails. Before introducing these classes, let’s first understand the architecture of the email reception API. The JavaMail API defines a java.mail.Store class, which is used to perform email reception tasks. We call methods in this class in the program to obtain information about each mail folder in the mailbox. JavaMail uses the Folder object to represent the mail folder. Through the Folder object method, all the email information in the mail folder can be obtained. We know that the information of the email can be represented by the Message object. The Message class contains various methods to operate the email, such as obtaining the sender, subject, body content, sending time, etc. Their working relationship is as follows:
1. Obtain a Store object that implements a certain email sending protocol from the Session object;
2. Log in to the email address and connect to the POP3 or IMAP4 server;
3. Call the getFolder method of the Store to get the Folder object of a certain mail folder in the mailbox;
4. Call the getMessage or getMessages method in the Folder object to get a certain email or all emails in the mail folder. Each email is returned as a Message object.
Next, we use the above class to receive emails
import java.util.Properties;import javax.mail.Address;import javax.mail.Folder;import javax.mail.Message;import javax.mail.Session;import javax.mail.Store;class MailReceives { public static void main(String[] args) throws Exception { // Define the attribute information for connecting to the POP3 server String pop3Server = "pop.qq.com"; String protocol = "pop3"; String username = "[email protected]"; String password = "******************"; // The SMTP authorization code of QQ mailbox, what is the authorization code, and how is it set? Properties props = new Properties(); props.setProperty("mail.transport.protocol", protocol); // Protocol used (required by JavaMail specification) props.setProperty("mail.smtp.host", pop3Server); // The SMTP server address of the sender's mailbox// Get the connection Session session = Session.getDefaultInstance(props); session.setDebug(false); // Get the Store object Store store = session.getStore(protocol); store.connect(pop3Server, username, password); // Login authentication of POP3 server // When obtaining Store object through POP3 protocol, the mail folder name can only be specified as "INBOX" Folder folder = store.getFolder("INBOX");// Get the user's email account folder.open(Folder.READ_WRITE); // Set access to the mail account Message[] messages = folder.getMessages();// Get all messages in the mail account for (Message message : messages) { String subject = message.getSubject();// Get the email subject Address from = (Address) message.getFrom()[0];// Obtain the sender address System.out.println("The subject of the message is: " + subject + "/tSender address is: " + from); System.out.println("The content of the message is: "); message.writeTo(System.out);// Output the email content to the console} folder.close(false);// Close the folder object store.close(); // Close the connection object}}Console output result:
You can see that the console outputs the original content of the email, which has not been parsed yet, so it needs to be parsed before reading. The next article, Introduction to JavaMail, Chapter 5, parsing emails, will explain how to parse emails.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.