The principle and simple implementation of e-mail sending

Source: Internet
Author: User
Tags auth base64 sprintf

Before the code starts, we manually simulate the process of sending an e-mail message, so the next code is easier to understand.

Let's take 163 mailboxes for example.

Type the telnet command first, then connect to NetEase's SMTP server, using port 25th.

Then look at the following figure:

<<<<<<

From now on, we can simulate the process of sending a letter!

Send command: EHLO hello (hello can be arbitrarily replaced)

Then we asked to log in.

Send command: AUTH LOGIN

Then send the user name of your account Base64 code (do not @xxx.com) the past! I used a test mailbox It_is_just_a_test@163.com,it_is_just_a_test base64 encoding is axrfaxnfanvzdf9hx3rlc3q=

Then send the base64 code of your own password to the past. The Base64 code for my password is mtk5mza3mtrsewg=.

The server tells you that the certificate was successful. We can send the mail now!

Send a message note: Mail from: <it_is_just_a_test@163.com>

Then send the destination mailbox description: RCPT to: <it_is_just_a_test@126.com>

Then request to send the message: DATA

Then send the header and message body to send the data:

From:it_is_just_a_test@163.com

To:it_is_just_a_test@126.com

Subject: Test

mime-version:1.0



This is a test email!!! .

(Enter a carriage return at the end of the body input.) and then return to the end of the body part. This will display the message that the messages were sent successfully. ) <CR><LF> indicates a carriage return.

Once the letter has been sent, we can use the QUIT command to turn off the connection to the server.

The rationale is this: then let's look at the code. (Use the program to send the time is nothing more than to replace "\ r \ n"!)

#include <iostream> #include <WinSock2.h> #include <windows.h> #include <string.h> using
namespace Std;

#pragma comment (lib, "Ws2_32.lib")/* link ws2_32.lib dynamic link library/const int MAXSIZE = 1024;
int srvport = 25;
Char srvdomain[256] = "smtp.163.com"; Char username[256] = "it_is_just_a_test@163.com"; Own mailbox name char password[256] = "19930714LYH"; The password of own mailbox char targetemail[256] = "it_is_just_a_test@126.com"; The mailing address to send char emailtitle[256] = "Hello." I am the flower of Li Shu. "; Message subject char content[256] = "This is a test message." I'm glad my first mail client was made successfully. ";//message body Socket Createconn (char* pwebsite,int port) {//For setting up socket object, initializing environment socket sockclient = socket (af_inet,sock_st     ream,0);   
	Establish socket object Sockaddr_in addrsrv;
    hostent* phostent;  Phostent = gethostbyname (pwebsite); Get information about the domain name ADDRSRV.SIN_ADDR. S_un.	S_ADDR = * ((DWORD *) phostent->h_addr_list[0]);   
	The IP address addrsrv.sin_family=af_inet that gets the network byte order of the SMTP server;   
	Addrsrv.sin_port=htons (port); int tf = connect (sockclient, (sockaddr*) &AMP;ADDRSRV,sizeof (sockaddr));
		Send request to server if (tf!=0) {return 0;
	printf ("link failed \ n");
return sockclient; //int dowhat Accept data void sendandrecvmsg (socket sockclient,//client socket char* PMessage,//messages to be sent int Mes  Sagelen,//message length int dowhat,//artificial type char* recvbuf,//received buffer int Recvbuflen//buffer length) {Char
	LPMESSAGE[256] = {0};
	memcpy (Lpmessage, PMessage, Messagelen);
	printf ("\n\n%s \", lpmessage);
		if (Dowhat = = 0) {Send (Sockclient, lpmessage, Messagelen, 0);
		memset (recvbuf, 0, Recvbuflen);     DWORD num = recv (sockclient, Recvbuf, Recvbuflen, 0); 
		Receive data printf ("%s \ n", recvbuf);
		int i = 0; 
			while (i!= num) {printf ("%02x", recvbuf[i++]); 
			if ((i)%16 = = 0) {printf ("\ n");
		
	printf ("\ n");
		
	else if (Dowhat = 1) {Send (Sockclient, lpmessage, Messagelen, 0);
		else if (Dowhat = 2)//is only receiving data {memset (recvbuf, 0, Recvbuflen);     DWORD num=recv (sockclient,recvbuf,recvbuflen,0); Receive data
		cout << recvbuf << Endl;
		int i = 0; 
			while (I < num) {printf ("%02x", (byte) recvbuf[i++]); 
			if ((i)%16 = = 0) {printf ("\ n");
	printf ("\ n");
	} int Getstrlen (char* pstring)//Gets the length of the string {int i = 0;
	while (pstring[i++]!= 0);
return i-1;
    } void StringToBase64 (const char *src,char *dst) {/* Converts the string to Base64 encoding/int i = 0;
    char *p = DST;
	int d= strlen (SRC)-3;
    static const char base64[] = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/"; for (I=0;i<strlen (SRC) -3;i+=3), if (strlen (SRC)-3) <0 There is a buf for (i=0;i<=d;i+=3) {*p++
        = base64[((* (src + i)) >> 2) & 0x3f];
        *p++ = base64[((* (src + i)) & 0x3) << 4) + ((* (src + i + 1)) >> 4)];
        *p++ = base64[(* (src + i + 1) & 0xf) << 2) + ((* (src + i + 2)) >> 6)];
    *p++ = base64[(* (src + i + 2)) & 0x3f]; } if ((Strlen (SRC)-i) = = 1) {*p++ = BasE64[((* (src + i)) >> 2) & 0x3f];
        *p++ = base64[((* (src + i)) & 0x3) << 4];
        *p++ = ' = ';
    *p++ = ' = ';
        } if ((Strlen (SRC)-i) = = 2) {*p++ = base64[((* (src + i)) >> 2) & 0x3f];
        *p++ = base64[((* (src + i)) & 0x3) << 4) + ((* (src + i + 1)) >> 4)];
        *p++ = base64[(* (src + i + 1) & 0xf) << 2)];
    *p++ = ' = ';
} *p = ' I '; BOOL Formatemail (char* pfrom, char* pTo, char* psubject, char* pMessage, char* email) {/* Format the content to be sent/* * LSTRCAT (email, "
	From: ");
	Lstrcat (Email, pfrom);
	
	Lstrcat (Email, "\ r \ n");
	Lstrcat (Email, "to:");
	Lstrcat (Email, pTo);
	
	Lstrcat (Email, "\ r \ n");
	Lstrcat (Email, "Subject:");
	Lstrcat (Email, psubject);
	
	Lstrcat (Email, "\ r \ n");
	Lstrcat (Email, "mime-version:1.0");
	Lstrcat (Email, "\ r \ n");
	
	Lstrcat (Email, "\ r \ n");
	
	Lstrcat (Email, pMessage);
	Lstrcat (Email, "\r\n.\r\n");
return TRUE;
	} void Main () {WORD wversionrequested; WsadaTA Wsadata;
	
	int err;
	wversionrequested = Makeword (2, 1);
	Err = WSAStartup (wversionrequested, &wsadata); SOCKET sockclient = Createconn (Srvdomain, Srvport);
    NetEase SMTP mailbox, number 25th Port Char Buff[maxsize];  memset (buff, 0, sizeof (char) * MAXSIZE);  Qing 0 sendandrecvmsg (sockclient, 0, 0, 2, buff, MAXSIZE);
	Receive data char usernametosendemail[256] = {0};
	sprintf (Usernametosendemail, "EHLO%s", "it_is_just_a_test@163.com");
	Lstrcat (Usernametosendemail, "\r\n\0"); Sendandrecvmsg (Sockclient, Usernametosendemail, Getstrlen (Usernametosendemail), 0, Buff, MAXSIZE); Both Receive and send Sendandrecvmsg (sockclient, "AUTH login\r\n", strlen ("AUTH login\r\n"), 0, Buff, MAXSIZE);
	Request Login Char puername[256] = {0}; 
	The STRSTR function searches for the first occurrence of a string in another string and returns the pointer DWORD p = strstr (UserName, "@") for the first occurrence of the position-userName; memcpy (Puername, UserName, p);
    Get user name, such as from "13203200199@163.com" to get "13203200199" char base[256]; StringToBase64 (Puername, base);
	Base64 encoded char Str[maxsize] with user name; memset (str, 0, MAXSIZE);
	sprintf (str, "%s\r\n", base/* "mtmymdmymdaxotk="); Sendandrecvmsg (sockclient, str, lstrlen (str), 0, Buff, MAXSIZE);
	Send the username and receive the return StringToBase64 (password, base) of the server;
	memset (str, 0, 1024);
	sprintf (str, "%s\r\n", base); Sendandrecvmsg (sockclient, str, lstrlen (str), 0, Buff, MAXSIZE);
	Send user password and receive server return char mailfrom[256] = {0};
	
	sprintf (Mailfrom, "MAIL from: <%s>\r\n", userName);
	
	Sendandrecvmsg (Sockclient, Mailfrom, Lstrlen (Mailfrom), 0, Buff, MAXSIZE);
	Char rcptto[256] = {0};
	sprintf (Rcptto, "RCPT to: <%s>\r\n", targetemail);

	Sendandrecvmsg (Sockclient, Rcptto, Lstrlen (Rcptto), 0, Buff, MAXSIZE);

	Sendandrecvmsg (sockclient, "data\r\n", Lstrlen ("data\r\n"), 0, Buff, MAXSIZE);
	Char email[1024] = {0};
	
	Formatemail (UserName, Targetemail, Emailtitle, Content, Email);
	
	Sendandrecvmsg (Sockclient,email,lstrlen (Email), 0, Buff, MAXSIZE);

    Sendandrecvmsg (sockclient, "quit\r\n", Lstrlen ("quit\r\n"), 0, Buff, MAXSIZE);   
	Closesocket (sockclient); WsacleanuP (); }

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.