Basic knowledge of e-mail development

Source: Internet
Author: User

First, the mail development involves some basic concepts 1.1, mail server and e-mail

To provide e-mail functionality on the Internet, you must have a dedicated e-mail server. For example, the Internet is now a lot of providers of mail services: Sina, Sohu, 163, etc. they all have their own mail server.

These mail servers are similar to real-life post offices, which are primarily responsible for receiving incoming mail from the user and delivering it to the email recipient's e-mail address.

e-mail (e-mail address) to obtain the need to apply on the mail server, in fact, e-mail is actually the user in the mail server application of an account, the user on the mail server to apply for an account, the mail server will allocate a certain amount of space for this account, Users can then use this account and space to send e-mails and save emails sent by others.

1.2. Mail Transfer Protocol 1.2.1, SMTP protocol

After the user connects to the mail server, to send it an e-mail message, you need to follow a certain rules of communication, the SMTP protocol is used to define such communication rules. Therefore, we often refer to servers that handle user SMTP requests (mail-sending requests) as SMTP servers (mail-sending servers).

1.2.2, POP3 protocol

Similarly, if a user wants to receive an e-mail message from an e-mail server managed by the email servers, it will follow a certain communication format after the mail server is connected, and the POP3 protocol is used to define the communication format. Therefore, we often refer to servers that handle user POP3 requests (mail receiving requests) as POP3 servers (mail receiving servers).

1.3. e-mail sending and receiving process

The sending and receiving process of a message is illustrated by a graph, as shown in:


Let's just say this message and send and receive process:

1, [email protected] User write an email sent to Sohu SMTP server. The steps that correspond to ①

2, Sohu SMTP server to start processing [email protected] User's request, it will be based on the recipient's address, the current recipient is not their own jurisdiction of the user, if so, directly to the email to the recipient's assigned mailbox space. Sohu SMTP server to determine the recipient address found, this email recipient [email protected] is Sina Mail Server Management, and then forwarded to Sina SMTP server. The steps that correspond to ②

3, Sina SMTP server to start processing Sohu SMTP server sent over the Email,sina SMTP server based on the recipient's address, found the recipient's own jurisdiction of the user, so the email will be directly stored to the [email protected] The user's assigned mailbox space. The step ③ corresponds to.

4, [email protected] users will send the mail after the notification [email protected] users to collect. [Email protected] users will be connected to the Sina POP3 server to receive mail, corresponding to the steps ④.

5, POP3 server from [email protected] User's mailbox space to take out email, corresponding to step ⑤.

6, POP3 server will be taken out of the email sent to [email protected] users, corresponding to step ⑥.

Ii. using the SMTP protocol to send mail 2.1, SMTP protocol explained

When sending mail to a mail server using the SMTP protocol, there are a few things that are required to do

1. Use the "ehlo" command and the SMTP server on the connection to say hello, for example:


2. Log in to the SMTP server using the "Auth login" command, the username and password used by the login must be Base64 encrypted, for example:

①, input command: Auth Login

②, enter the user name after encrypting with Base64: z2fjba==

③, enter the password after BASE64 encryption: MTIzNDU2

3. Indicate the sender and recipient of the message

Mail from:<[email protected]>

RCPT to:<[email protected]>

4, write the content of the message to be sent, the format of the message is certain rules, a well-formed message should contain the message header and the main content of the message.

The message header uses the following three fields to indicate

      1. The From field is used to indicate the sender of the message
      2. To field to indicate the recipient of the message
      3. The Subject field is used to indicate the subject of the message

After the message contains this information, it is a well-formed message.

①, enter "Data" command


②, writing mail content

from:<[email protected]>----headers

to:<[email protected]>----headers

Subject:hello----Mail Header

-----Blank Line

Hello gacl----The specific contents of the message

5. Enter a. Tell the mail server that the contents of the message have been finished.


6. Enter the QUIT command to disconnect from the mail server


The 6 steps above are what the SMTP protocol requires to do to send an email.

2.2. Use the SMTP protocol to send messages manually

After a certain understanding of the SMTP protocol, we can use the SMTP protocol to send mail. Here is a demonstration of using Telnet client to connect to Sohu's mail server and send an email to Sina's mailbox

In order to be able to have an intuitive understanding of the SMTP protocol, here we do not use any third-party mail client tools, but using the most original Telnet client to complete the sending process of the message, Telnet is a window comes with the network client program, It allows you to connect to any host on the Internet.

Use the Telnet client to connect to the Sohu SMTP server as shown in:


Email via Telnet Client as shown in:

By logging into <[email protected]> email, you can receive an email from <[email protected]>, as shown in:


This is the process of sending mail using the SMTP protocol.

Third, using the POP3 protocol to receive mail 3.1, POP3 protocol explanation

The POP3 agreement stipulates the following things to do when receiving mail

①, enter user name and password to log on to POP3 server, user name and password do not need BASE64 encryption

User Username--Username of login Mailbox
Pass password--password used to log in to the mailbox

②, use RETR command to collect mail
RETR digital Receive mail, RETR 1 means to receive the first email in the mailbox, which is the most important command in the POP3 protocol.

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


Returns the number of messages in a mailbox and the amount of space the message occupies
List Number

Returns statistics for a message

③, after the message is received, use the QUIT command to disconnect from the POP3 server.
Quit disconnecting from the POP3 server

3.2. Use the POP3 protocol to receive mail manually

Receive mail We also do not use any third-party client tools, but instead connect to the POP3 server using a Telnet client to receive it.

For example: Now my Sohu mailbox has such an email, as shown in:


Instead of using client-side tools such as Foxmail or Outlook, we use Telnet client to connect to Sohu's POP3 server to collect it manually.

1. Using Telnet to connect the POP3 server on Sohu, use the command: Telnet 110, as shown in:


Collect the mail according to the steps in the mail that are stipulated in the POP3 Agreement. As shown in the following:


Can be seen, we POP3 protocol purely manual from the Sohu POP3 server received back a email,email inside the content has been BASE64 encoding processing, below we write a small program will be BASE64 encoded message content to decode, restore back to the content of the message, The code is as follows:

 1 package me.gacl.encrypt; 2 3 Import; 4 Import; 5 Import; 6 7 Import Sun.misc.BASE64Decoder; 8 Import Sun.misc.BASE64Encoder; 9 public class Base64encrypt {one-to-ten public static void Main (String args[]) throws Ioexception{13/*SYSTEM.O Ut.print ("Please enter user name:"), bufferedreader in = new BufferedReader (new InputStreamReader (; UserName = In.readline (); System.out.print ("Please enter password:"); n String password = in.readline (); BASE6          4Encoder encoder = new Base64encoder (), System.out.println ("Username after encoding:" + Encoder.encode (Username.getbytes ())); 20 SYSTEM.OUT.PRINTLN ("Encoded password:" + Encoder.encode (Password.getbytes ())); */21 Base64decoder decode R = new Base64decoder (); 23//Message subject BASE64 encoded in String emailSubject = "=? GBK? B?08q8/rliytq=?= "; 25//Message text content BASE64 encoded in String emailplaincontent =" Vpk1pbxe08q8/reiY82y4sruo6e= "; 27//BASE64 encoding with HTML tags and message content, String emailhtmlcontent =" pfa+vpk1pbxe08q8/reiy82y4sruo6e8l1a+ "; 29//will use BASE64 encoded text content and then use Base64 to decode the EmailSubject = new String (Decoder.decodebuffer (EmailSubject)," GBK " ): Emailplaincontent = new String (Decoder.decodebuffer (emailplaincontent), "GBK"), Emailhtmlcontent = n EW String (Decoder.decodebuffer (emailhtmlcontent), "GBK"); System.out.println ("Mail title:" +emailsubject); Sys Tem.out.println ("Mail content:" +emailplaincontent); System.out.println ("Message content with HTML tags:" +emailhtmlcontent); 36}37}

The results of the operation are as follows:

This is the process of collecting mail using the POP3 protocol.

The above is the message to send and receive the principle of the relevant content, this article is mainly to introduce the mail to send and receive the process of using the SMTP protocol and POP3 protocol. There's not much code involved, and it's going to be about using JavaMail to send mail.

Basic knowledge of e-mail development

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: 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.