Javamail Study Notes (1). Understand the mail Transmission Protocol (SMTP, POP3, IMAP, and mime)

Source: Internet
Author: User
Tags imap all mail telnet program

E-mail must be transmitted between the mail client and the mail server, and between the two mail servers. These rules must follow the mail transmission protocol. The SMTP protocol defines the communication rules between the mail client and the SMTP service, and between the two SMTP servers for sending mail; POP3/IMAP Protocol defines the communication rules for sending and receiving mails between the mail client and the POP3 server.

I. SMTP protocol

SMTP (Simple Mail Transfer Protocol) defines the communication rules between the mail client and the SMTP server, and between the two SMTP servers to send mail. The SMTP protocol belongs to the TCP/IP protocol family. The communication parties use a single-Answer command/Response Form for a conversation, and define the dialog rules and the syntax format of all commands/responses.

There are 18 commands in the SMTP protocol. Generally, you only need 6 of them to send an email, the following table lists the six commands in sequence and describes their syntax and functions,<SP>Space,<CRLF>Returns the carriage return and line feed.

SMTP Command Format Description
EHLO <SP> <domain> <CRLF> EHLOThe command is the first SMTP command that must be sent after the SMTP mail sender establishes a connection with the SMTP mail receiver. The <domain> parameter indicates the Host Name of the SMTP mail sender.
The EHLO command is used to replace the HELO command in the traditional SMTP protocol.
Auth <SP> <para> <CRLF> If the SMTP mail recipient requires the SMTP mail sender to authenticate, it will prompt the SMTP mail sender with the authentication method, the SMTP mail sending program should then use this command to respond to the SMTP mail receiving program. The parameter <para> indicates the authentication method in response, which is usually the authentication method previously prompted by the SMTP mail receiving program.
Mail <SP> from: <reverse-path> <CRLF> This command is used to specify the email address of the sender. The <reverse-path> parameter indicates the email address of the sender.
RCPT <SP> to: <forword-path> <CRLF> This command is used to specify the email address of the recipient. The <forward-path> parameter indicates the email address of the recipient. If an email is to be sent to multiple recipients, use multiple RCPT <SP> to commands to specify the email addresses of each receiver.
Data <CRLF> This command is used to indicate that the SMTP mail sending program is preparing to enter the mail content. All data sent after this command will be treated as the mail content until "<CRLF>.<CRLF> "flag indicates that the email content ends.
Quit <CRLF> This command ends the mail sending process. After receiving this command, the SMTP mail recipient closes the network connection with the SMTP mail sending program.

For syntax and function descriptions of other SMTP commands, see rfc821 and rfc1869.

For each command sent by the SMTP mail sender, the SMTP mail recipient responds with a response message. Each response message starts with a response status, for example, 250 OK. The response status indicates the processing result and status of the SMTP server's request command. It is a three-digit decimal number. The highest digit of the response status code represents different categories. If it is 2, the command is successfully executed. If it is 5, the command fails to be executed. If it is 3, the command is not completed. For more information about the meaning of the response status code, see RFC821.

The SMTP protocol is an application layer protocol based on TCP/IP. The default network listening port number of the SMTP server is 25. The following will send an email by sending the SMTP command through the telnet program, to understand the interaction process of the SMTP protocol.

Description of the simulated environment: connect to the sina SMTP server and send an email to the 163 SMTP server. The operation process is shown in:

Note:

1. the user name and password used to connect to the SMTP server must be base64 encoded. The following is a JAVA program that uses base64 encoding for the user name and password:

Package Org. yangxin. study. JM. util; import Java. io. bufferedreader; import Java. io. ioexception; import Java. io. inputstreamreader; import sun. misc. base64encoder; public class base64util {public static void main (string [] ARGs) throws ioexception {base64encoder encoder = new base64encoder (); system. out. println ("Enter the User name:"); string username = new bufferedreader (New inputstreamreader (system. in )). readline (); system. out. println (encoder. encode (username. getbytes (); system. out. println ("enter the password:"); string Password = new bufferedreader (New inputstreamreader (system. in )). readline (); system. out. println (encoder. encode (password. getbytes ()));}}

2. The text pointed by the red arrow indicates the commands I entered in the telnet program, and the lines starting with numbers 2, 3, and 5 indicate the SMTP server's response to the commands. Through the six SMTP commands in the above table, a simple email is sent. Of course, a complex email contains not only the information, but also the topic, sending date, CC, attachment, and other message headers.

2. The POP3 protocol mail service provider provides a dedicated email storage space for each user's requested email address. The SMTP server saves the received email address to the corresponding user's email address. To obtain your own email from the email address provided by the mail service provider, you must use the POP3 mail server of the mail service provider. POP3 (Post Office Protocol, version 3 of the Post Office Protocol) Protocol defines the specific rules and details for communication between the mail client program and the POP3 server. POP3 is defined in RFC 1939. The default network listening port number is 110. The POP3 protocol defines 12 POP3 commands. The mail client uses these commands to retrieve and obtain the mail Information in the user's email address. The following table lists the 12 POP3 commands and their descriptions. <SP> indicates space, and <CRLF> indicates carriage return and line feed.
POP3 Command Format Description
User <SP> username <CRLF> UserThe command is the first command that is usually sent after the POP3 client establishes a connection with the POP3 mail server. The username parameter indicates the recipient's account name.
Pass <SP> password <CRLF> PassThe command is a command sent by the POP3 client after the USER command is successfully passed. It is used to pass the account password. The parameter password indicates the account password.
APOP <SP> name, digest <CRLF> APOPThe command is used to replace the user and PASS commands. It submits the account password to the POP3 mail server in the form of an MD5 digital digest.
Stat <CRLF> StatThe command is used to query the statistical information in the mailbox, such as the number of mails in the mailbox and the byte size occupied by the mails.
UIDL <SP> MSG # <CRLF> UIDLThe command is used to query the unique identifier of an email. The MSG # parameter indicates the mail serial number, which is a number starting from 1.
List <SP> [MSG #] <CRLF> ListCommand is used to list Mail Information in the mailbox. The MSG # parameter is an optional parameter, indicating the mail serial number. If no parameter is specified, the POP3 server lists all mail Information in the mailbox. If MSG # is specified, the POP3 server returns only the mail information corresponding to the serial number.
RETR <SP> MSG # <CRLF> RETRCommand is used to obtain the content of an email. The MSG # parameter indicates the mail serial number.
Dele <SP> MSG # <CRLF> DeleThe command is used to set the delete tag on an email. The MSG # parameter indicates the mail serial number. When the POP3 server executes the DELE command, it only sets the delete mark for the mail and does not actually Delete the mail. Only after the POP3 client sends the quit command, the POP3 server will delete all emails with deletion tags.
Rest <CRLF> RestCommand is used to clear the delete mark of all emails.
Top <SP> MSG # <SP> n <CRLF> TopThe command is used to obtain the first n lines of content in the header and body of an email. The MSG # parameter indicates the serial number of the email, and the N parameter indicates the first few lines of content to be returned. Use this command to improve the processing efficiency of the mail list displayed in the web mail system (sent and received through the Web site), because in this case, you do not need to obtain the complete content of each mail, instead, you only need to get the mail header information for each mail.
Noop <CRLF> NoopCommand is used to check the connection between the POP3 client and the POP3 server.
Quit <CRLF> QuitThe command ends the mail receiving process. After receiving this command, the POP3 server deletes all emails with deletion tags and closes the network connection with the POP3 client program.

For each POP3 Command sent by a POP3 client program, the POP3 server will respond to some response information. The response information consists of one or more lines of text information. The first line always starts with "+ OK" or "-Err", indicating that the current command is successfully executed or failed.
The following uses the telnet program to connect to the 163 POP3 server to analyze the mail receiving process. For operation steps, see:Interaction Process:1. First, use the tlenet program to connect to the 163 POP3 mailbox and telnet pop3.163.com 110. 2. Run the USER command to specify the user name, user xyang0917. 3. Run the PASS command and enter the password, namely, pass 123456abc. After the verification is successful, the system prompts that there is an email in the mailbox, which occupies 1822 bytes of mailbox space. 4. Execute the STAT command to count the information in the mailbox. The result shows that there is an email in the mailbox, which occupies 1822 bytes of mailbox space. 5. Execute the LIST command to list all emails in the mailbox. The result shows 1 1822, 1 indicates the mail number, and indicates the mail size. If there are multiple emails, the numbers are listed in ascending order starting from 1. 6. Run the RETR 1 command to view the content of the first email. 7. Run the DELE 1 command to set the delete flag for the first mail. 8. Execute the rset command to reset the delete flag of all emails. 9. Run the quit command to exit the mail receiving program. After receiving the quit command sent by the client, the POP3 server deletes all emails with deletion tags and disconnects the network with the client. The telnet program automatically ends and returns to the window command line window. III. The IMAP Protocol IMAP (Internet Message Access Protocol) is an extension of the POP3 protocol and defines the communication rules between the mail client and the mail server. The IMAP protocol is defined in rfc2060. Currently, 4th versions are used, which is also called IMAP4. Compared with the POP3 protocol, the IMAP Protocol provides more powerful mail receiving functions, mainly reflected in the following aspects:

  1. IMAP has the abstract browsing function. After reading the topic, sender, size, and other information of all emails, the user can decide whether to download the email or delete it directly on the server.
  2. IMAP allows users to selectively download email attachments. For example, an email contains three attachments. If you are sure that only two attachments are useful to you, you can download only these two attachments without downloading the entire email, this saves the download time.
  3. IMAP allows users to create their own folders on the mail server and save each email by category.
Question:So what are the differences between POP3 and IMAP protocols? Differences between IMAP and POP3 4. Mime Protocol

In the early days, when people used e-mails, they used the e-mail content of common text for communication. Due to the rapid development of the Internet, people were not satisfied that e-mails were only used to exchange text information, we hope to use emails to exchange more diverse multimedia information, such as embedding binary data such as images, sounds, animations, and attachments in emails. However, as defined in the rfc822 document of the previous mail sending protocol, only text messages can be sent and non-text emails cannot be sent. To address this problem, mime (Multipurpose
Internet Mail Extension, multi-purpose Internet Mail Extension) protocol.

The mime protocol is used to define a complex message body format. It can express multi-segment parallel text content and non-text mail content, such as image data and email attachments embedded in the mail body. In addition, the data format of the mime protocol can also prevent information loss during transmission of mail content. For MIME messages indicating a specific resource, the Data Type of the resource must be specified in the message header. For mime message combination, the combination relationship must be specified in the message. The combination of the Data Type and message of a specific resource is specified by the Content-Type header field in the message header. The content in the Content-Type field is in the form of "main type/subtype". The main types include text, image, audio, video, application, multipart, and message, it represents text, images, audio, video, applications, composite structures, messages, and so on. Each primary type contains multiple subtypes, such as plain, HTML, XML, and CSS. The multipart primary type is used to represent mime composite messages. It is the most important type in the mime protocol. MIME messages in a mime mail can have three types of combinations: mixing, association, and selection. Their MIME types are as follows:

  • Multipart/mixed
Indicates that the content in the message body is a combination type, and the content can be a mixture of text, sound, attachments, and other different mail content. For example, an email contains attachments, and the content of the email also references embedded images or attachment resources. The MIME type of this type of mail must be defined as multipart/mixed.
  • Multipart/related
Indicates that the content in the message body is an association (dependency) combination type. For example, the Mail content has an IMG tag. the src attribute of this tag points to an image resource in the mail. Therefore, the MIME type of this mail should be defined as multipart/related.
  • Multipart/alternative
Indicates that the content in the message body is of the combination type. For example, when the body of an email is expressed in both HTML and plain text formats, they can be nested in a multipart/alterntive type combination message. The advantage of this approach is that if the email reader does not support HTML format, it can be replaced by text format. The basic situation of a most complex email is that it contains the body and attachments of the email. The body of the email can be expressed in both HTML and common text formats, in addition, other embedded resources are referenced in the HTML text. The mime message structure shown in can be used to describe the most complex Email:
We can see that if you want to add attachments to an email, you must define the MIME type of the entire email as multipart/mixed. If you want to reference embedded Resources in the HTML format body, define multipart/related MIME messages. If the common text content and HTML text content coexist, define multipart/alternative MIME messages. Note: If the entire email contains only plain text and HTML text, the MIME type of the entire email should be defined as multipart/alternative; if the entire mail contains HTML text content and embedded resources, but does not contain attachments, the MIME type of the entire mail should be defined as multipart/related.

For more details about the mime protocol, see Chapter 3 Organizational structure of the email in "Zhang Xiaoxiang Java email development details", section 2nd mime Protocol

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.