In-depth analysis of javaweb Item41--the principle of sending and receiving messages

Source: Internet
Author: User
Tags readline

I. Some basic concepts involved in e-mail development 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. sending mail using the SMTP protocol 2.1. SMTP protocol explanation

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:

 ehlo gacl

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

    • The From field is used to indicate the sender of the message
    • To field to indicate the recipient of the message
    • 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

      Data

      ②, writing mail content

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

      To: <[email protected]> --Message header

      subject:hello--Mail Header

      --Blank line

      Hello gacl--Message Details

      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

      Quit

      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:

  

When we log in to [email protected] , we can receive email sent by [email protected] , as shown in:

  

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

Iii. receiving mail using the POP3 protocol 3.1, POP3 agreement 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 name username– Login mailbox
Pass password– password used for login 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.

Stat

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 pop3.sohu.com 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:

 PackageMe.gacl.encrypt;ImportJava.io.BufferedReader;ImportJava.io.IOException;ImportJava.io.InputStreamReader;ImportSun.misc.BASE64Decoder;ImportSun.misc.BASE64Encoder; Public  class base64encrypt {     Public Static void Main(String args[])throwsioexception{/*system.out.print ("Please enter user name:");        BufferedReader in = new BufferedReader (new InputStreamReader (system.in));        String userName = In.readline ();        System.out.print ("Please enter password:");        String password = in.readline ();        Base64encoder encoder = new Base64encoder ();        SYSTEM.OUT.PRINTLN ("Encoded User name:" + Encoder.encode (Username.getbytes ())); SYSTEM.OUT.PRINTLN ("Encoded password:" + Encoder.encode (Password.getbytes ())); * /Base64decoder decoder =NewBase64decoder ();//BASE64 encoding of the subject of the messageString EmailSubject ="=? GBK? B?08q8/rliytq=?= ";//BASE64 encoding of message text contentString emailplaincontent ="Vpk1pbxe08q8/reiy82y4sruo6e=";//BASE64 encoding with HTML tags and message contentString emailhtmlcontent ="pfa+vpk1pbxe08q8/reiy82y4sruo6e8l1a+";//will use BASE64 encoded text content and then use Base64 to decodeEmailSubject =NewString (Decoder.decodebuffer (EmailSubject),"GBK"); Emailplaincontent =NewString (Decoder.decodebuffer (emailplaincontent),"GBK"); Emailhtmlcontent =NewString (Decoder.decodebuffer (emailhtmlcontent),"GBK"); System.out.println ("Message title:"+emailsubject); System.out.println ("message content:"+emailplaincontent); System.out.println ("message content with HTML tags:"+emailhtmlcontent); }}

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.

In-depth analysis of javaweb Item41--the principle of sending and receiving messages

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.