Some basic concepts involved in 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, and so they have their own mail server.
These mail servers are similar to real-life post offices, which are primarily responsible for receiving mail from users and posting them to the e-mail recipient's email address.
e-mail (e-mail address) to obtain the need to apply on the mail server, exactly, e-mail is actually the user in the e-mail server application of an account, the user in the mail server application for an account, mail server will allocate a certain amount of space for this account, Users can then use this account and space to send e-mail and save emails sent over by others.
1.2. Mail Transfer Protocol
1.2.1, SMTP protocol
When a user connects to a mail server and wants to send an e-mail message to it, it is necessary to follow certain communication rules that the SMTP protocol uses to define this communication rule. Therefore, we often call the server that handles user SMTP requests (mail-sending requests) The SMTP server (the mail-sending server).
1.2.2, POP3 protocol
Similarly, if a user wants to receive an e-mail message from a mail server-managed email address, it must follow a certain form of communication after connecting to the mail server, which is used to define the POP3 protocol. Therefore, we usually refer to the server that handles the user POP3 request (mail receive request) as the POP3 server (mail receiving server).
1.3, the process of sending and receiving e-mail
A picture shows the process of sending and receiving a message, as shown in the following illustration:
Simply say the process of sending and receiving mail:
1, xdp@sohu.com users to write an email sent to the Sohu SMTP server. The steps corresponding to the previous figure ①
2, the Sohu of the SMTP server to start processing xdp@sohu.com user requests, it will be based on the address of the recipient to determine whether the current recipient is their own jurisdiction of the user, if it is, directly to the recipient of the email to the allocation of the mailbox space. Sohu SMTP server to determine the recipient address found that this email to the recipient gacl@sina.com is Sina's mail server management, and then sent the email to the Sina SMTP server. The steps corresponding to the previous figure ②
3, Sina's SMTP server to start processing Sohu SMTP server sent over the Email,sina SMTP server based on the recipient's address, found the recipient of their own jurisdiction of the user, The email is then deposited directly into the mailbox space allocated to the gacl@sina.com user. The steps corresponding to the previous figure ③.
4, xdp@sohu.com users will send out the mail, notify gacl@sina.com users to collect. The gacl@sina.com user then collects mail for the POP3 server connected to Sina, which corresponds to the steps in the previous figure ④.
5, POP3 server from the gacl@sina.com user's mailbox space to take out the email, corresponding to the steps ⑤.
6, POP3 server will be taken out of the email sent to gacl@sina.com users, corresponding to the steps ⑥.
Ii. send mail using SMTP protocol
2.1. SMTP protocol explanation
Using the SMTP protocol to send mail to a mail server requires that you do the following
1. Use the "EHLO" command and the SMTP server on the connection to say hello, for example:
EHLO GaCl
2. Log on to the SMTP server using the "Auth login" command, and the username and password used by the login must be Base64 encrypted, for example:
①, input command: Auth Login
②, enter user name after encryption using BASE64: z2fjba==
③, enter the password after BASE64 encryption: MTIzNDU2
3. Indicate the sender and recipient of the message
Mail from:<gacl@sohu.com>
RCPT to:<xdp_gacl@sina.cn>
4, write to send the content of the message, the writing format of the message has a certain rules, a good format of the message should contain the message headers and the main content of the message.
The message header uses the following three fields to indicate
- The From field is used to indicate who sent the message
- To field is used to indicate the recipient of a message
- The Subject field is used to indicate the subject of the message
The message is a well-formed message after it contains the information.
①, enter the "Data" command
Data
②, writing email content
from:<gacl@sohu.com>----Message Headers
to:<xdp_gacl@sina.cn>----Message Headers
Subject:hello----Message Headers
-----Blank Line
Hello GaCl----The details of the message
5, enter one. Tell the mail server that the content of the message has been written.
6, enter the QUIT command to disconnect from the mail server
The above 6 steps are the SMTP protocol required to send an email must do.
2.2, use the SMTP protocol to send mail manually
After a certain understanding of the SMTP protocol, we can use the SMTP protocol to send mail. Here's 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 the most original Telnet client to complete the process of sending the message, Telnet is a window with the network client program, It allows you to connect to any host on the Internet.
Use the Telnet client to connect to the SMTP server for Sohu, as shown in the following illustration:
Email via Telnet client, as shown in the following illustration:
We log into the <xdp_gacl@sina.cn> mailbox, we can receive the email sent by <gacl@sohu.com> , as shown in the following figure:
This is the process of sending mail using the SMTP protocol.
Iii. receiving mail using the POP3 protocol
3.1, POP3 agreement to explain
The POP3 agreement stipulates the following things to do when receiving mail
①, enter user name and password login to POP3 server, username and password do not need to go through Base64 encryption
User Username--Username for logon mailbox
Pass password--the password used to log in to the mailbox
②, use RETR command to collect mail
RETR digital Mail, RETR 1 means to receive the first mail in the mailbox, this is the POP3 agreement in the most important command.
Before using the RETR command to collect 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 the mailbox and the amount of space that the message occupies
List Number
Returns statistics for a message
③ the connection to the POP3 server using the QUIT command after the message collection completes.
Quit disconnect from the POP3 server
3.2. Use the POP3 protocol to receive mail manually
To receive mail we do not use any Third-party client tools, but rather using Telnet client to connect to the POP3 server for collection.
For example: Now my Sohu mailbox has such an email, as shown in the following picture:
Instead of using client tools like Foxmail or Outlook, we use Telnet client to connect to Sohu's POP3 server to collect it manually.
1, use Telnet to connect to Sohu's POP3 server, use the command: Telnet pop3.sohu.com 110, as shown in the following figure:
The mail is collected according to the procedure of the POP3 agreement. As shown in the following illustration:
Can see, we POP3 Agreement pure manual from Sohu's POP3 server to collect back a email,email inside content has been BASE64 encoded processing, the following we write a small program after BASE64 encoded message content to decode, restore back to the content of the message, 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 ("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 ("The encoded user name is:" + Encoder.encode (Username.getbytes ()));
SYSTEM.OUT.PRINTLN ("Encoded password is:" + Encoder.encode (Password.getbytes ())), * * Base64decoder decoder = new Base64decoder (); Message subject BASE64 encoded String emailSubject = "=? GBK?
B?08q8/rliytq=?= ";
BASE64 encoding String emailplaincontent = "vpk1pbxe08q8/reiy82y4sruo6e=" for message text content;
BASE64 encoded String emailhtmlcontent = "pfa+vpk1pbxe08q8/reiy82y4sruo6e8l1a+" with HTML tags and message contents; You will use the 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 = new String (Decoder.decodebuffer (emailhtmlcontent), "GBK");
System.out.println ("Mail title:" +emailsubject);
System.out.println ("Mail content:" +emailplaincontent);
SYSTEM.OUT.PRINTLN ("Message content with HTML tags:" +emailhtmlcontent);
}
}
The results of the operation are as follows:
This is the process of using the POP3 protocol to collect mail.
This is the message sent and received the principle of the content, this article is mainly about the mail sent and received the use of the SMTP protocol and the POP3 protocol. Does not involve too many code aspect thing, later will specifically introduce uses JavaMail to carry on the mail to send, clicks this article : "Javaweb JavaMail creates the mail and sends the mail".
There are so many things about the javaweb implementation of the message send and receive function.