Summary: This article describes a method of using SMTP protocol specifications and implementing email transmission in a program directly using SMTP protocol commands. And some key implementation code is given in the VC development environment.
Foreword
Email Service is a very broad application as the most widely used service items on the Internet, which also plays a very important role in web applications. As with other network services, email systems also have their transport protocols, including SMTP (Simple Mail Transfer Protocol, Simple Mail Transfer Protocol, POP (Post Office Protocol, Post Office Agreement), and IMAP (Internet Message Access Protocal, Message Access Protocol These protocols apply to email transmission and reception. Some email processing software such as Outlook Express and Foxmail are designed by SMTP and POP3 protocols to send and receive emails in combination with Windows Sockets sockets. This article uses the SMTP protocol as a research object, and in the Visual C 6.0 programming environment, in accordance with the SMTP protocol, send SMTP commands to the SMTP command, receive and process feedback from the mail server, thereby implementing the email.
Communication model and session process of SMTP protocol
SMTP protocol communication model
The SMTP protocol is one of the TCP / IP protocols, mainly to how to transmit emails from the sender address to the recipient address, that is, the rule of the transmission is specified. The communication model of the SMTP protocol is not complicated. The main work is concentrated on sending SMTP and receiving SMTP: First, the mail request issued by the user is established by sending SMTP to receive a two-worker communication link that receives SMTP. The receiving SMTP is relative. In the transmission of SMTP, it can actually be the final recipient or an intermediate transmitter. Send SMTP is responsible for sending an SMTP command to the receiving SMTP, and receiving SMTP is responsible for receiving and feedback. It can be used to represent the following communication models.
SMTP protocol command and response
From the previous communication model, it can be seen that the SMTP protocol is sent between the session between SMTP and the receiving SMTP is done by the SMTP command that sends SMTP and receives SMTP feedback. After the communication link is established, send the SMTP Send the mail command instruction mail sender. If you receive SMTP, you can receive the message to make an OK response, then send the SMTP to continue the RCPT command to confirm whether the email is received, if it is received OK's response, otherwise I will issue a refusal to receive response, but this does not affect the entire mail operation. The two sides repeated multiple times until the email is complete. The SMTP protocol contains 10 SMTP commands, the list is as follows:
SMTP Command Command Description Hello
Each command of the SMTP protocol returns a response code. Each number of responses has a specific meaning. If the first digit is 2, it indicates that the command is successful; 5 tables fail; 3 tables have not been completed. Some more complicated mail programs use this feature, first check the first number of responses, and determine the next action based on its value. The list of SMTP's response code is as follows:
Responsive code 501 Parameter format Error 502 Command Uncover 503 Error command sequence 504 Command parameters Do not implement 211 system status or system help response 214 Help information 220
Use the SMTP protocol in your application
SMTP protocol session process
It is necessary to clarify the session process of the SMTP protocol before the programming is performed. In fact, the contents of the previous introduction can be substantially out of the framework of SMTP, and for a normal mail, the process is roughly: set up TCP connections first. The client will then send the Hello command to identify the sender's own identity, and continue to send a mail command by the client. If the server answers "OK", continue to send the RCPT command to identify the recipient of the email, here you can have Multiple RCPT lines, and the server side indicates whether it is willing to accept the message for the recipient. After the end of the negotiation of both parties, send the mail with the command DATA, which is ended "." Also sent out. Subsequently end this delivery process, exit with a Quit command. By an instance, send mail from langrui@sohu.com to renping@sina.com to describe this session process in more detail: r: 220 sina.com Simple mail transfer service readys: hello sohu.comr: 250 sina.coms : Mail from:
Mail format
Due to the particularity of the email structure, it is possible to format the message header and the mail body in a certain format to format the message header and the mail body in a certain format. The parties that need to be formatted are mainly: sender address, recipient address, topic, and send date. There is a detailed description of the formatting of the email in the RFC documentation of the RFC documentation. For details, please refer to this document. Below the RFC 822 documentation in accordance with the RFC 822 documentation in accordance with the RFC 822 documentation (partial code):
// Mailhead Prepare strTemp = _T ("from:") m_strfrom; file: // sender address add_header_line ((lpctstr) strTemp); strTemp = _t ("to:") m_STRTO; file: // Piece address add_header_line ((lpctstr) strTemp); m_tdatetime = m_tdatetime.getCurrentTime (); // Send time strTemp = _t ("data:"); strTemp = m_tdatetime.format ("% a,% D% b% y) % H:% m:% s% z "); add_header_line ((lpctstr) strTemp); strTemp = _t (" Subject: ") m_strsubject; file: // Topic add_header_Line ((lpctstr) strTemp); file: // Email head end m_strheader = _t ("/ r / n"); file: // Mail body preparation IF (m_StrBody.right (2)! = _T ("/ r / n")) file: // confirm the last Enter the back of the Rollert M_StrBody = _t ("/ r / n"); where the add_header_line (lpctstr szheaderline "function is used to append the string of SzheaderLine points to the m_strheader. Among them, the formatted message header is saved in m_strheader, and the formatted mail body is saved in m_strbody.
Network communication foundation for SMTP by Socket socket
Many network programs are implemented with Socket sockets. For some standard network protocols such as HTTP, FTP, and SMTP, the programming is also based on socket, but the port number is no longer arbitrary setting and strike to be protocol To specify, such as the HTTP port at 80, FTP is 21, while SMTP is 25. Socket just provides communication capabilities on the network on the specified server on the specified port, which is confirmed by the network protocol between the client and the server, which is completely transparent to the socket. Therefore, you can use the Socket socket to provide the ability to provide network communication, and what kind of communication response to the network communication connection is well established, it is necessary to press the SMTP protocol. Socket socket network programming article information is very rich, limited to this parameters, here no more details, please refer to the relevant documentation for details. For the sake of simplicity, there is no programming with more complicated Windows Sockets APIs, but use the CSocket class that passes through a better package. Before formally use the socket, you must first initialize the socket with the AFXSocketinit () function, then create a socket object with create (), and the socket is established with the mail server through connect (). . If everything is normal, follow-up work is to follow the SMTP protocol to use the Send (), and receive () functions to send the SMTP command and receive the response code sent by the mail server to complete the email.
SMTP session response
After the link connection is established with the mail server, you can print it as a result of the session process described above. For the transmission of the SMTP command, you can use the CSocket class with the CSocket class after the sending format. Send it to the server and receives the response code from the mail server through the CSocket class, and responds according to the response code table of the SMTP protocol. Below is a part of the function get_response () for receiving the response code: BOOL CSMTP:: GET_RESPONSE (uint response_expected) // Enter the parameter for the desired response code {... // m_wssmtpserver for the class object of CSocket, call receive ) the reply code in the received buffer // response_buf m_wsSMTPServer.Receive (response_buf, RESPONSE_BUFFER_SIZE) sResponse = response_buf; sscanf ((LPCTSTR) sResponse.Left (3), _T ( "% d"), & response); pResp = & response_table [ Response_expected]; file: // Check whether the received response code is the desired IF (Response! = PRESP-> NRESPONSE) {... // is not equal, error handling return false;} return true;}
The various parts of the session is similar, all of which are command-answer methods, and all the appearances, the following is the key part of this article is also a critical part of the implementation - complete the formatting of the SMTP command under program control and the transmission of the command and Test processing for the email server response code:
/ / Format and send a hello command, and receive, verify the server response code gethostname (local_host, 80); shello.format (_T ("Helo% S / R / N"), local_host); m_wssmtpserver.send ((LPCTSTR) Shello, Shello.getLength ()); if (! get_response (gener_success)) file: // Check if the answer is 250 {... returnaf false;} file: // format and sends a mail command, and receive, verify the server Respond to Sfrom.Format (_T ("Mail from: <% s> / r / n"), (lpctstr) msg-> m_strfrom); m_wssmtpserver.send (lpctstr) sfrom, sfrom.getlength ()); if ( ! get_response (gener_success)) File: // Check if the answer is 250 Return false; file: // format and send the RCPT command, and receive, verify the server response code semail = (lpctstr) msg-> m_strto; sto.format _T ("RCPT TO: <% S> / R / N"), (lpctstr) semail); m_wssmtpserver.send ((lpctstr) sto, sto.getlength ()); if (! Get_response (gener_success)) File: / / Check if the answer is 250 Return false; file: / / format and send the data command, and receives, verify the server response code STEMP = _t ("DATA / R / N"); m_wssmtpserver.send ((lpctstr) STEMP, STEMP .Getlength ()); if (! Get_response (data_success)) file: // Check if the answer is 354 Return False; file: // Send the message header M_WssMtpserver.send, based on the RFC 822 document specified, formatted, MSG ((LPCTSTR) MSG -> m_strheader, msg-> m_strheader.getlength ()); ... f ILE: // Send the mail body STEMP = msg-> m_StrBody; if (STEMP.LEFT (3))), based on RFC 822 document specification; if ("./r/n")) Stemp = _t. ") STEMP; while ((npos = step)> -1) {scooked = STEMP.MID (nStart, npos); scooked = szgood; STEMP = Scooked STEMP.Right (STEMP.GETLENGTH ( ) - (NPOS NBADLENGTH));} m_wssmtpserver.send ((lpctstr) STEMP, STEMP.GETLENGTH ()); file: // Send content data end flag "
If (! get_response (gener_success)) // check whether the response code is 250 Return false; until this, the session process of the SMTP protocol is basically in the program, which can be based on the network communication capabilities provided by the Socket socket. The communication interaction process of the SMTP command and the SMTP response code is the basic session content, so that the SMTP protocol is ultimately transmitted to the email.
in conclusion
As an application on the Internet, email class software, its design development must comply with the mature technical specifications (such as RFC document series specifications) and related protocols (such as POP, SMTP, IMAP, and LDAP, etc.). Only on the basis of compling the above specifications and protocols can truly implement openness and standardization of mail class software products and services. This article focuses on the SMTP protocol and its application in VC programming, and performs openness and standardized programming in accordance with the SMTP protocol. The procedures described herein are compiled by Microsoft Visual C 6.0 under Windows 98.