Send mail using C ++ socket

#include<iostream>
#include<Windows.h>
#include<winsock.h>
#include <assert.h>
#include<string>
using namespace std;
#pragma comment(lib, "ws2_32.lib")
typedef char     uint8;
typedef unsigned long    uint32;


static uint8 alphabet_map[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static uint8 reverse_map[] =
{
	 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
	 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
	 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 255, 63,
	 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, 255, 255, 255, 255, 
	 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 
{ 
	uint32 i, j; 
	for (i = 0, j = 0; i + 3 <= text_len; i + = 3) 
	{ 
		encode [j ++] = alphabet_map [text [i] >> 2]; // Remove the first 6 digits of the first character and find the corresponding result character
	 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255, 
	 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 
	 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 255, 255, 255, 255, 255 
}; 
uint32 base64_encode ( const uint8 * text, uint32 text_len, uint8 * encode) 
		encode [j ++] = alphabet_map [((text [i] << 4) & 0x30) | (text [i + 1] >> 4)]; // will Combine the last 2 digits of one character with the first 4 digits of the second character and find the corresponding character 
		encode [j ++] = alphabet_map [((text [i + 1] << 2) & 0x3c) | (text [ i + 2] >> 6)]; // Combine the last 4 digits of the second character with the first 2 digits of the third character and find the corresponding character 
		encode [j ++] = alphabet_map [text [i + 2 ] & 0x3f]; // Remove the last 6 digits of the third character and find the resulting character 
	} 

	if (i <text_len) 
	{ 
		uint32 tail = text_len-i;
		if (tail == 1) 
		{ 
			encode [j ++] = alphabet_map [text [i] >> 2]; 
			encode [ j ++] = alphabet_map [(text [i] << 4) & 0x30]; 
			encode [j ++] = '='; 
			encode [j ++] = '=';
		}
		else //tail==2
		{
			encode[j++] = alphabet_map[text[i] >> 2]; 
			encode [j ++] = alphabet_map [((text [i] << 4) & 0x30) | (text [i + 1] >> 4)]; 
			encode [j ++] = alphabet_map [(text [i + 1] << 2) & 0x3c ]; 
			encode [j ++] = '='; 
		} 
	} 
	return j; 
} 
uint32 base64_decode (const uint8 * code, uint32 code_len, uint8 * plain) 
{ 
	// assert ((code_len & 0x03) == 0); // if If its condition returns an error, the program execution is terminated. A multiple of 4. 

	uint32 i, j = 0; 
	uint8 quad [4]; 
	for (i = 0; i <code_len; i + = 4) 
	{ 
		for (uint32 k = 0; k <4; k ++) 
		{
			quad [k] = reverse_map [code [i + k]]; // Grouping, each group of four are converted into decimal numbers in the base64 table in turn 
		} 

		assert (quad [0] <64 && quad [1] <64) ;

		plain [j ++] = (quad [0] << 2) | (quad [1] >> 4); // Remove the first 6 digits of the decimal number of the base64 table corresponding to the first character and the base64 table of the second character The first two digits of the decimal number are combined 

		if (quad [2]> = 64) 
			break; 
		else if (quad [3]> = 64) 
		{ 
			plain [j ++] = (quad [1] << 4) | (quad [2] >> 2); // Remove the last 4 digits of the second character corresponding to the base64 table decimal number and the third character corresponding to the first 4 digits of the base64 table decimal number 
			break; 
		} 
		else 
		{ 
			plain [j ++ ] = (quad [1] << 4) | (quad [2] >> 2); 
			plain [j ++] = (quad [2] << 6) | quad [3]; // Remove the third character corresponding The last 2 digits of the decimal number of the base64 table are combined with the fourth character 
		} 
	} 
	return j; 
} 

char * base64 (char * input) ( 

	uint8 * text = (uint8 *) input; 
	uint32 text_len = (uint32) strlen (( char *) text); 
	uint8 buffer [1024],buffer2[4096];
	uint32 size = base64_encode(text, text_len, buffer2);
	
	buffer2[size] = '\0';
	return buffer2;
}//改变编码



SOCKET connect_server(char * host, int port);//连接服务器
void disconnect(SOCKET c) {//断开
	closesocket(c);
	WSACleanup();
}
void recv_ms(SOCKET c) {
	char text[2000];
	strnset(text, 0, sizeof(text));
	recv(c, text, 2000, 0);
	cout << text << endl << endl;
}
void send_ms(SOCKET c, char *msg) {
	send(c, msg, strlen(msg), 0);
}

char email[200], pas[200];

int main() {

	char host[] = "smtp.qq.com";
	char *pstr = NULL;
	int port = 25;
	SOCKET c;
	// establish connection 
	c = connect_server (host, port); 
	// receive message 
	recv_ms (c); 
	// send message hello 
	send_ms (c, (char *) "helo qq \ r \ n \ 0"); 
	// Inform login 
	recv_ms (c); 

	send_ms (c, (char *) "auth login \ r \ n \ 0"); 
	
	recv_ms (c); 

	// start login 
	
	cout << "enter mailbox \ n"; 
	cin >> email ; 
	
	// rcpt to: <[email protected]> 
	char buf [200]; 
	char * a = base64 (email); 
	strcat (a, "\ r \ n \ 0"); 
	send_ms (c, a); 
	recv_ms (c); 

	send_ms (c, (char *) "abc \ r \ n \ 0"); // abc represents the base64 encryption of the authorization code 
	recv_ms (c); // authorization code 



	memset (buf, 0, sizeof (buf )); 
	sprintf (buf, "mail from: <% s> \ r \ n \ 0", email);
	send_ms(c, buf);
	recv_ms (c); // Where to send 

	printf ("Enter recipient \ n"); 
	cin >> email; 
	memset (buf, 0, sizeof (buf)); 
	sprintf (buf, "rcpt to: <% s > \ r \ n \ 0 ", email); 
	
	send_ms (c, buf); 
	recv_ms (c); // Where to send 

	send_ms (c, (char *)" data \ r \ n \ 0 "); 
	recv_ms ( c); 

	memset (buf, 0, sizeof (buf)); 
	cout << "input title \ n"; 
	cout << "input title \ n"; 
	cin >> buf; // title 
	cin >> email; // Content 
	char ans [2000]; 
	strcpy (ans, "subject:"); 
	strcat (ans, buf); 
	strcat (ans, "\ r \ n \ r \ n"); 
	strcat (ans, email); 
	strcat (ans , "\ r \ n. \ r \ n \ 0"); 
	send_ms (c, ans); 
	recv_ms (c);

	disconnect(c);
	
	return 0;
}


SOCKET connect_server(char * host, int port) {
	WSADATA wd;
	SOCKET c;
	SOCKADDR_IN saddr;
	struct hostent *pHostent;
	int ret = 0;
	ret = WSAStartup(MAKEWORD(2, 2), &wd);
	if (ret != 0) return 0;
	c = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

	pHostent = gethostbyname(host);

	saddr.sin_addr.S_un.S_addr = *((unsigned long*)pHostent->h_addr_list[0]);//地址

	saddr.sin_family = AF_INET;//ipv4
	saddr.sin_port = htons(port);//端口
	connect(c, (SOCKADDR*)&saddr, sizeof(SOCKADDR));
	return c;
}

//[email protected]
//[email protected]

 Just learned, according to the big brother, it is mainly called the library, which has an important role in understanding the computer network

Guess you like

Origin www.cnblogs.com/lesning/p/12757988.html