JavaMail mail introduction and API overview article 1

Nowadays, e-mail is playing a more and more important role in our life, Almost all of us deal with it (at least from time to time, we will receive inexplicable spam). In work, using email for communication 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 let us confirm whether it is OK Register to prevent malicious registration. Of course, it is impossible to operate manually in our work. JavaMail, one of the 13 core technologies of Java, provides us with APIs to operate mail.

1、 Mail server

To provide e-mail function on the Internet, you must have a special e-mail server. A large number of e-mail servers have been set up on the Internet, such as Sina com、qq. com、163. COM and other websites provide free e-mail servers for the public, and many companies also provide e-mail servers for internal employees. These e-mail servers can send and receive e-mail from each other. What is the role of the mail server? It can help us send and receive mail. It can also provide us with the service of getting mail. This is similar to the post office in real life. It can not only receive mail, but also provide services for us to get mail, but also help us send mail. The following pictures vividly illustrate the functions of the mail server

(1) Receive the mail delivered by the user; (2) forward the mail delivered by the user to the target mail server; (3) receive the mail forwarded by other mail servers and store the mail in the user mailbox managed by them; (4) provide reading services for users who come to read the mail.

Mail servers can be divided into two types according to communication protocol: SMTP server and POP3 / imap server. The functions of 1, 2 and 3 in the above figure are completed by SMTP server, while the functions of 4 are completed by POP3 server.

2、 E-mail

Multiple e-mail boxes can be set up on each e-mail server. E-mail is also called e-mail address. It is similar to the communication address in real life. Users can receive and send e-mail to others through this address. The acquisition of e-mail needs to be applied on the mail server. Specifically, e-mail is actually an account applied by users on the mail server. The mail server saves the received mail to the mailbox space allocated for an account (so we need to clean our mail regularly, otherwise we can't receive new mail if it exceeds the allocated space). Users log in to the mail server through their applied user name and password to check the received email at this address.

3、 E-mail transmission process

The transmission process of e-mail can be represented by the following figure

Solid line process description above:

1、 lisi@sina.com The mail client establishes a network connection with Sina's SMTP server, logs in with Lisi's user name and password, and sends the mail to Sina's SMTP server using SMTP protocol;

2. Sina's SMTP server received lisi@sina.com After submitting the email address, first judge whether the recipient's email address belongs to the jurisdiction of the SMTP server according to the recipient's address suffix. If so, directly store the email in the recipient's mailbox. Otherwise, Sina's SMTP server queries the DNS server for the MX record of the domain name represented by the recipient's email address suffix (Sohu. Com), So as to get the SMTP server information of Sohu, then establish a connection with the SMTP server of SOHO, and send the mail to the SMTP server of Sohu using SMTP protocol;

3. After receiving the e-mail from Sina's SMTP server, SOHO's SMTP server will also judge whether the e-mail belongs to the jurisdiction of the SMTP server according to the recipient's address. If so, it will directly store the e-mail in the recipient's mailbox. Otherwise, Sohu's SMTP server may continue to forward the e-mail;

4、 wangwu@sohu.com The account establishes a network connection with SOHO's POP3 / imap server through the client, logs in successfully, and then views it through POP3 / imap protocol wangwu@sohu.com Whether there are new messages in the mailbox. If so, use POP3 or IMAP protocol to read the messages in the mailbox.

The dotted line process is similar to the solid line process and will not be repeated.

JavaMail API is a set of standard development package adopted by Sun company to facilitate Java developers to send and receive solid line mail in applications. It supports some common mail protocols, such as SMTP, POP3, IMAP and mime. To use this API, of course, you need to download the relevant jar package, the latest version 1.5.0 6. Download address: https://java.net/projects/javamail/pages/Home 。

JavaMail API can generally be divided into the following three categories according to its functions: 1. API for creating and parsing mail content: message class is the core API for creating and parsing mail, and its instance object represents an e-mail; 2. API for sending mail: transport class is the core API class for sending mail. Its instance object represents the mail sending object that implements a mail sending protocol, such as SMTP protocol; 3. API for receiving mail: store class is the core API class for receiving mail. Its instance object represents the mail receiving object that implements a mail receiving protocol, such as POP3 protocol.

Session class:

Session class is used to define the environment information required by the whole application and collect the session information for establishing network connection between the client and the mail server, such as the host name, port number, adopted mail sending and receiving protocols, etc. The session object builds transport and store objects for sending and receiving mail based on this information, and provides information support when creating message objects for clients.

The relationship between the above four classes is as follows:

Most of the above contents are extracted from the book "detailed explanation of java mail development". The second article on creating mail in the next introduction to JavaMail will introduce how to create mail using the API provided by JavaMail.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>