In-depth analysis of JavaWeb Item41-Principles of sending and receiving emails

Source: Internet
Author: User

In-depth analysis of JavaWeb Item41-Principles of sending and receiving emails
I. Some basic concepts involved in email Development1.1. email server and email

To provide email on the Internet, you must have a dedicated email server. For example, many email service providers on the Internet, such as sina, sohu, and 163, all have their own email servers.

These email servers are similar to post offices in real life. They are mainly responsible for receiving mails delivered by users and delivering the mails to the email address of the recipients.

To obtain an email address, you must apply on the email server. Specifically, an email address is actually an account applied for on the email server, after you apply for an account on the mail server, the mail server will allocate a certain amount of space for this account, you can use this account and space to send emails and save emails sent by others.

1.2. Mail transmission protocol

1.2.1 SMTP protocol

After you connect to the email server, you must follow certain communication rules to send an email to it. The SMTP protocol is used to define such communication rules. Therefore, we usually call the smtp server (mail sending server) that processes user SMTP requests ).

1.2.2 POP3 protocol

Similarly, if you want to receive an email from the email address managed by the mail server, it must follow a certain communication format after connecting to the mail server, POP3 protocol is used to define this communication format. Therefore, we usually call a server that processes users' pop3 requests (Mail receiving requests) as a POP3 server (Mail receiving server ).

1.3 email sending and receiving process

A figure shows the sending and receiving processes of an email, as shown in:

  

Let's briefly describe the mail sending and receiving process:

1. [email protected] the user writes an Email to the Smtp server of sohu. Corresponding to Step 1

2. the Smtp server of sohu starts to process [email protected] user requests. It determines whether the current recipient is a user under its jurisdiction based on the recipient's address. If yes, the Email is directly stored in the mailbox space allocated to the recipient. The Smtp server of sohu judges the recipient address and finds that the recipient [Email protected] of this email is managed by the Sina mail server, and then forwards the Email to the Smtp server of Sina. Step 2

3. The Sina Smtp server starts to process the emails sent from the sohu Smtp server. The Sina Smtp server determines Based on the recipient's address and finds users under the recipient's jurisdiction, therefore, the Email is directly stored in the mailbox space allocated to the [email protected] user. Corresponding to step 3.

4. After the [email protected] user sends the email, the [email protected] user is notified to collect the email. [Email protected] the user connects to the Sina POP3 server to receive emails, corresponding to step 4.

5. the POP3 server extracts the email from the [Email protected] user's mailbox, which corresponds to Step 5.

6. the POP3 server sends the obtained Email to the [email protected] user, corresponding to Step 6.

Ii. Use Smtp protocol to send emails 2.1 Smtp protocol explanation

When sending an email to the mail server using the smtp protocol, you must do the following:

1. Use"Ehlo"Say hello to the smtp server connected, for example:

 ehlo gacl

2. Use"Auth login"To log on to the Smtp server, the user name and password used for Logon must be Base64 encrypted. For example:

① Enter the command: auth login

② Enter the username after Base64 encryption: Z2FjbA =

③ Enter the Base64 encrypted password: MTIzNDU2

3. Specify the sender and recipient of the email.

Mail from:<[email protected]>

Rcpt:

4. Write the content of the email to be sent. The format of the email is regular. A well-formatted email should contain the header and body content of the email.

Use the following three fields to specify the mail header

The from field indicates the mail sender to field indicates the mail recipient

The subject field specifies the subject of the email.
   
The content of the email contains the information, which is a well-formed email.

① Enter the "data" command

Data

②. Write email content

From:<[email protected]>-- Mail header

To: -- Mail header

Subject: hello -- mail header

-- Empty line

Hello gacl-specific content of the email

5. Enter a message to inform the email server that the email has been written.

.

6. Enter the quit command to disconnect the email server.

Quit

The above six steps are required for sending an Email as stipulated in the Smtp protocol.

2.2 Use Smtp protocol to send emails manually

After some understanding of the Smtp protocol, we can use the Smtp protocol to send emails. The following shows how to use the Telnet client to connect to the Sohu mail server and then send an Email to Sina's mailbox.

In order to have an intuitive understanding of the Smtp protocol, we use the original Telnet client instead of any third-party email client tool to complete the mail sending process, telnet is a network client program that comes with windows. It can be used to connect to any host on the Internet.

Use the telnet client to connect to the smtp server of Sohu, as shown in:

  

Send an Email via telnet client, as shown in:

  

After logging on to [email protected], we can receive the email sent by [Email protected], as shown in:

  

This is the process of sending emails using the Smtp protocol.

3. Use POP3 protocol to receive emails 3.1 POP3 protocol explanation

The POP3 Protocol specifies the following items to do when receiving an email:

① Enter the user name and password to log on to the POP3 server. The user name and password do not need to be encrypted by Base64.

User username-username of the logon email address
Pass password-password used to log on to your mailbox

② Use the retr command to receive emails
Retr number receiving email. retr 1 indicates receiving the first email in the mailbox. This is the most important command in POP3 protocol.

Before using the retr command to receive emails, you can use the following two commands to view some information about emails in your mailbox.

Stat

Returns the number of mails in the mailbox and the space occupied by the mails.
List number

Returns the statistics of an email.

③ After receiving the email, use the quit command to disconnect the POP3 server.
Quit disconnect from POP3 server

3.2 manually receive emails using POP3 protocol

We do not use any third-party client tool to receive emails. Instead, we use the Telnet client to connect to the POP3 server.

For example, there is an email like this in my Sohu mailbox, as shown in:

 

Now we do not need to use client tools such as foxmail or outLook to collect fees, but use the Telnet client to connect to the POP3 server of Sohu. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Examples/LXEUE9QM7f + examples/nKvqO6PC9wPg0KPHA + oaGhoaGhPGltZyBhbHQ9 "here write picture description" src = "http://www.bkjia.com/uploads/allimg/160106/0425152033-5.gif" title = "\"/>

Follow the mail receiving steps stipulated in the POP3 protocol to receive mails. As shown in:

  

We can see that we manually received an Email from Sohu's POP3 server, and the content in the Email was base64-encoded, below we write a small program to decode the Base64 encoded mail content and restore the mail content. The Code is as follows:

Package me. gacl. encrypt; import java. io. bufferedReader; import java. io. IOException; import java. io. inputStreamReader; import sun. misc. BASE64Decoder; import sun. misc. BASE64Encoder; public class Base64Encrypt {public static void main (String args []) throws IOException {/* System. out. print ("Enter the User name:"); BufferedReader in = new BufferedReader (new InputStreamReader (System. in); String userName = in. readLine (); S Ystem. out. print ("Enter password:"); String password = in. readLine (); BASE64Encoder encoder = new BASE64Encoder (); System. out. println ("encoded Username:" + encoder. encode (userName. getBytes (); System. out. println ("encoded password:" + encoder. encode (password. getBytes (); */BASE64Decoder decoder = new BASE64Decoder (); // Base64 encoded String emailSubject = "=? GBK? B? 08q8/rLiytQ =? = "; // Base64 encoded String emailPlainContent =" vPK1pbXE08q8/reiy82y4srUo6E = "; // Base64 encoded String emailHtmlContent = "PFA + vPK1pbXE08q8/reiy82y4srUo6E8L1A + "; // use Base64 to decode the text content encoded with Base64. emailSubject = new String (decoder. decodeBuffer (emailSubject), "GBK"); emailPlainContent = new String (decoder. decodeBuffer (emailPlainContent), "GBK"); emailHtmlContent = new String (decoder. decodeBuffer (emailHtmlContent), "GBK"); System. out. println ("mail title:" + emailSubject); System. out. println ("email content:" + emailPlainContent); System. out. println ("html-tagged mail content:" + emailHtmlContent );}}

The running result is as follows:
  
JavaMail to send emails.


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.