SMTP Protocol Learning Notes

Source: Internet
Author: User
Tags base64 socket error

These days glad did not update the blog in time, one is due to the day work in the lock is more complicated, and second consecutive watched the two-night SMTP protocol.

I prefer to use practice to prove everything so that I can deepen my understanding.

First, the preparatory work:

1, the local environment: Windows XP SP3, ADSL 10M Fiber

2, development tools: WildPackets OmniPeek V5.1.4

Visual C + + 6.0

Outlook Express6.0

Flexedit V2.3.1871

Second, SMTP command:

1, HELO to the server identity user identity
2, mail initialization mail transfer mail from: <xxx>
3, RCPT identify a single recipient of the message, often after the mail command can have multiple RCPT to: <xxx>
4, data in a single or multiple RCPT command, indicating that all the recipient of the message has been identified, initialization data transmission, to. End
5, NOOP No operation, the server should respond to OK
6, RSET reset session, the current transfer is canceled
7. QUIT End Session

third, analysis of the data package:

1. Open Outlook Express6.0 and create a new message with the following contents:

2, open OmniPeek, select SMTP:

3, start to grasp the package, and then send the test message above, get the data package as follows:

(To be aware of source and destination, that is, origin and destination address)

4, only to see the 20th packet, other can be more easily understood:

5. Other Tips

<CR> <LF> that corresponds to "/r/n" in C + +

For example line 8:charset= "gb2312" <cr><lf>, because the escape character needs to be used

So the corresponding string should be char * sData = "charset=/" gb2312/"/r/n";

Iv. Related code:

1, the following code through the socket with the SMTP server to establish a connection and verify identity. Open Visual C + +, create a new console project, and add a CPP file:

/************************************************************************//* main.cpp SMTP Protocol Learning notes-login authentication/by Koma 2009.9.10 11:35/* Http://blog.csdn.net/wangningyu/************************************************************** /#include "stdio.h" #include "ZBase64.h" #include "winsock2.h" #pragma comment (lib, "Ws2_32.lib") int main (int ARGC, char* argv[]) {sockaddr_in saserver; Lphostent lphostent; Wsadata Wsadata; SOCKET Hsocket; int nret; char* host_name= "smtp.tom.com"; char* req=//Each send a row of data server will respond "EHLO komawang/r/n" "AUTH login/r/n"///Two line is the login user and password (with Base64 encryption) "bm**********20=/r/n" "d2**" w4=/r/n "" quit/r/n "; Initializes the socket if (Makeword (2,2), &wsadata) printf ("Initialization of the socket error.") WSAStartup. "); The SMTP port defaults to Lphostent=gethostbyname (HOST_NAME); if (lphostent==null) printf ("Lphostent is empty. "); Hsocket = socket (AF_INET,SOCK_STREAM,IPPROTO_TCP); saserver.sin_family = af_inet; Saserver.sin_port = htons (25); SASERVER.SIN_ADDR = * ((LPIN_ADDR) *lphostent->h_addr_list); Connecting NR with a socketET = Connect (Hsocket, (LPSOCKADDR) &saserver,sizeof (sockaddr_in)); if (nret = = socket_error) {printf ("An error occurred while making the connection.") /n "); Closesocket (Hsocket); return 0; ///Use socket to send nret = Send (Hsocket,req,strlen (req), 0); if (nret==socket_error) {printf ("An error occurred while sending the packet. "); Closesocket (Hsocket); } Char dest[3000]; memset (dest,0,3000); Nret=1; while (nret>0) {//Receive return packet Nret=recv (Hsocket, (LPSTR) dest,sizeof (Dest), 0); if (nret>0) dest[nret]=0; else dest[0]= 0; Displays the size of the returned packet, content printf ("/n return Packet size:%d/n", nret); printf ("Return packet contents:/n%s", Dest); return 0; }

2, the following is the BASE64 and the CPP file:

/************************************************************************//* ZBase64.h/************************* /#ifndef _zbase64 #define _ZBASE64 #pragma warning (disable:4786) # Include <string> using namespace std; Class ZBase64 {public:/* encoded databyte [in] the length of the data entered in bytes/string Encode (const unsigned char* data,int);/* Decoding Da Tabyte [in] the length of the data entered, in bytes Outbyte [out] The length of the data output, in bytes, do not compute the length of the output data by the return value/String Decode (const char* Data,int databyte, int& outbyte); }; #endif

/************************************************************************//* ZBase64.cpp/*********************** /#include "ZBase64.h" string zbase64::encode (const unsigned char* Data,int databyte) {//encoded table const char encodetable[]= "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+ /"; return value string Strencode; unsigned char tmp[3]={0}; int linelength=0; for (int i=0;i< (int) (DATABYTE/3); i++) {tmp[1] = *data++; TMP[2] = *data++; TMP[3] = *data++; strencode+= encodetable[tmp[1] >> 2]; strencode+= encodetable[(Tmp[1] << 4) | (Tmp[2] >> 4)) & 0x3F]; strencode+= encodetable[(tmp[2] << 2) | (Tmp[3] >> 6)) & 0x3F]; strencode+= encodetable[tmp[3] & 0x3F]; if (linelength+=4,linelength==76) {strencode+= "/r/n"; linelength=0;} //encode the remaining data int mod=databyte% 3; if (mod==1) {tmp[1] = *data++ strencode+= encodetable[(tmp[1] & 0xFC) >> 2]; strencode+= encodetable[((tmp[1) & Amp 0x03) << 4)]; strencode+= "= ="; else if (mod==2) {tmp[1] = *data++; TMP[2] = *data++; strencode+= encodetable[(tmp[1] & 0xFC) >> 2]; strencode+= encodetable[((tmp[1] & 0x03) << 4) | ((Tmp[2] & 0xF0) >> 4)]; strencode+= encodetable[((tmp[2] & 0x0f) << 2)]; strencode+= "="; return strencode; String ZBase64::D ecode (const char* data,int databyte,int& outbyte) {//decoding Table const char decodetable[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /' + ' 0, 0, 0, 63,//'/' 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,//' 0 '-' 9 ' 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, one, one, (0, 0, 0, 0, 0, 0, 26, 27, 28, 29, 30, 31,)., K, B, K, K, C, A '-Z ' -------------------------------------, return value string Strdecode; int nvalue; int i= 0; while (I < databyte) {if (*datA!= '/R ' && *data!= '/n ') {nvalue = decodetable[*data++] << nvalue + = decodetable[*data++] << 12; strdecode+= (Nvalue & 0x00ff0000) >> 16; outbyte++; if (*data!= ' = ') {nvalue + = decodetable[*data++] << 6; strdecode+= (Nvalue & 0x0000ff00) >> 8; outbyte++; if (*data!= ' = ') {nvalue + = decodetable[*data++]; Strdecode+=nvalue & 0x000000ff; outbyte++; } i = 4; else//carriage return line break, skip {data++; i++}} return strdecode; }

3, the program operation effect:

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.