Nowadays, email plays an increasingly important role in our lives, and each of us will deal with it (at least from time to time we receive inexplicable spam). At work, using email to communicate can make our work traceable and more formal. This is manually operated by us. In life, after registering an account on a website, the website will automatically send a welcome email and ask us to confirm whether to register to prevent malicious registration. Of course, this is impossible to operate it manually like we work. JavaMail in 13 core technologies of Java provides us with APIs to operate related to emails.
1. Mail server
To provide email functionality on the Internet, a dedicated email server must be available. A large number of email servers are set up on the Internet. For example, websites such as sina.com, qq.com, 163.com provide free email servers for the public, and many companies also provide email servers for internal employees, which can send and receive emails from each other. So what is the function of the mail server? It can help us send emails, receive emails, and provide us with mail pickup services. This is similar to the post office in real life. It can not only receive emails, but also provide services for us to pick up emails, and also help us to send emails. The following pictures vividly illustrate the functions of the mail server
(1) Receive mails delivered by users;
(2) Forward the email sent by the user to the target mail server;
(3) Receive emails forwarded by other mail servers and store the emails in the user's mailbox they manage;
(4) Provide reading services to users who come to read emails.
Mail servers can be divided into two types according to communication protocols: SMTP server and POP3/IMAP server. The functions 1, 2, and 3 in the figure above are completed by the SMTP server, while the functions 4 are completed by the POP3 server.
2. Email
Multiple email addresses can be opened on each email server. The email address is also called an email address. It is similar to the mailing address in real life. Users can receive emails from others and send emails to others through this address. Obtaining an email requires applying on the mail server. To be precise, the email is actually an account that the user applies on the mail server. The mail server saves the received mail to the mailbox space allocated for a certain account (so we need to clean our mail regularly, otherwise we will not be able to receive new mail if the allocated space exceeds the allocated space). The user logs in to the mail server through the username and password he requests to check the emails received by the address.
3. Email transmission process
The transmission process of email can be represented by the following figure
The solid line process description in the figure above:
1. After the mail client of [email protected] establishes a network connection with sina's SMTP server, logs in with lisi's username and password, and uses SMTP protocol to send the email to sina's SMTP server;
2. After receiving the address email submitted by [email protected], the SMTP server of sina first determines whether the recipient's email address falls within the jurisdiction of the SMTP server based on the address suffix of the recipient. If so, the mail will be stored directly in the recipient's email address. Otherwise, the SMTP server of sina will query the DNS server for the MX record of the domain name represented by the suffix of the recipient's email address (sohu.com) to obtain the SMTP server information of sohu, and then establish a connection with soho's SMTP server and use the SMTP protocol to send the email to sohu's SMTP server;
3. After soho's SMTP server receives the email from sina's SMTP server, it will also determine whether the email falls within the jurisdiction of the SMTP server based on the recipient's address. If so, store the email directly in the recipient's mailbox. Otherwise, sohu's SMTP server may continue to forward the email;
4. The [email protected] account establishes a network connection with soho's POP3/IMAP server through the client, and after successfully logging in, then check whether there is new email in the [email protected] email through the POP3/IMAP protocol. If so, use POP3 or IMAP protocol to read the email in the mailbox.
The dotted line process is similar to the solid line process and is no longer repeated.
JavaMail API is a standard development package adopted by Sun to facilitate Java developers to send and receive solid-line mail in applications. It supports some commonly used mail protocols, such as SMTP, POP3, IMAP and MIME. To use this API, of course, you need to download the relevant jar package first. The latest version is 1.5.6, download address: https://java.net/projects/javamail/pages/Home.
JavaMail API can usually be divided into the following three categories according to its functions:
1. API for creating and parsing email content: The Message class is the core API for creating and parsing emails, and its instance object represents an email;
2. API for sending emails: The Transport class is the core API class for sending emails. Its instance object represents the mail sending object that implements a certain email sending protocol, such as the SMTP protocol;
3. API for receiving emails: The Store class is the core API class for receiving emails. Its instance object represents the email receiving object that implements a certain email receiving protocol, such as the POP3 protocol.
Session class:
The Session class is used to define the environment information required by the entire application, as well as collecting session information for the client to establish a network connection with the mail server, such as the host name, port number, and the mail sending and receiving protocol used. Session objects build Transport and Store objects for mail transmission and reception based on this information, and provide information support when creating Message objects for clients.
The relationships of the above 4 classes are as follows:
Most of the above content is excerpted from the book "Detailed Explanation of Java Mail Development". Next articleBeginner of JavaMail The second article Create email will introduce how to create email using the API provided by JavaMail.