Java Web (XIII) send mail using JavaMail (QQ, 163, Sina mailbox server)

Come on, come on.

                    --WZY

1、 Principle of sending mail

Before you know how it works, you need to know two protocols, SMTP and POP3

SMTP: Simple Mail Transfer Protocol, that is, the simple mail transfer protocol, the protocol for sending mail, the default port, 25

POP3: post office protocol 3, i.e. post office protocol, protocol for receiving mail, default port, 110

Knowing these two protocols, let's talk about the schematic diagram of e-mail sending and receiving. There are two kinds. The same e-mail (QQ e-mail to QQ e-mail) and different e-mail (QQ e-mail to 163 e-mail) are different.

Explanation:

For convenience, Sina email, list@sina.com Called user a, Sohu email, wangwu@sohu.com Called user B

User a (Sina email), list@sina.com )To user B (Sohu email, wangwu@sohu.com )When sending an email, the whole process is the solid line part in the figure. User a logs in to his email account through Outlook (browser or client), writes an email, sends it to the SMTP server in Sina's mail server using SMTP protocol (a server specially used for sending), and then transmits it to the SMTP server in Sohu's mail server through SMTP protocol, Then, the e-mail is sent to the storage device of user B through Sohu's SMTP server for storage (each user will have a storage space to store the e-mail). Here, user a is equivalent to sending successfully, because it has reached the destination. If user B needs to view the e-mail content, it must be obtained from its own storage device through POP3 server, Then return to the browser or client for display.

If user B sends an email to user a, the whole process is the dotted line in the figure, which is the same step as a sends an email to B

If user a sends an email to user C who also uses Sina mailbox, the process is much simpler. First, it passes through the SMTP server, and then the SMTP server will send it to the storage device of user C. A successfully sends the email, and user C needs to display the email in his mailbox, Then take all messages from your storage device through the POP3 server for viewing.

2、 Send mail through Java code

2.1. Prepare jar package

Core: mail jar

Dependency: activation Jar, which is used when the mail needs to send attachments

2.2. Use 163 mailbox to send mail

        2.2. 1. First register an account in mailbox 163. If so, omit this step

        2.2. 2. Write java code to send mail, remember three big steps

           2.2. 2.1. Get the connection (connect to the mailbox server and log in)

Code

          2.2. 2.2. Create message (1. Sender, 2. Recipient, 3. Email title, 4. Email content)

Code

          2.2. 2.3 sending mail

Code

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
分享
二维码
< <上一篇
下一篇>>