1 2 3 4

利用C++套接字发送邮件

#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,
	 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)
{
	uint32 i, j;
	for (i = 0, j = 0; i + 3 <= text_len; i += 3)
	{
		encode[j++] = alphabet_map[text[i] >> 2];                             //取出第一个字符的前6位并找出对应的结果字符
		encode[j++] = alphabet_map[((text[i] << 4) & 0x30) | (text[i + 1] >> 4)];     //将第一个字符的后2位与第二个字符的前4位进行组合并找到对应的结果字符
		encode[j++] = alphabet_map[((text[i + 1] << 2) & 0x3c) | (text[i + 2] >> 6)];   //将第二个字符的后4位与第三个字符的前2位组合并找出对应的结果字符
		encode[j++] = alphabet_map[text[i + 2] & 0x3f];                         //取出第三个字符的后6位并找出结果字符
	}

	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);  //如果它的条件返回错误,则终止程序执行。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]];//分组,每组四个分别依次转换为base64表内的十进制数
		}

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

		plain[j++] = (quad[0] << 2) | (quad[1] >> 4); //取出第一个字符对应base64表的十进制数的前6位与第二个字符对应base64表的十进制数的前2位进行组合

		if (quad[2] >= 64)
			break;
		else if (quad[3] >= 64)
		{
			plain[j++] = (quad[1] << 4) | (quad[2] >> 2); //取出第二个字符对应base64表的十进制数的后4位与第三个字符对应base64表的十进制数的前4位进行组合
			break;
		}
		else
		{
			plain[j++] = (quad[1] << 4) | (quad[2] >> 2);
			plain[j++] = (quad[2] << 6) | quad[3];//取出第三个字符对应base64表的十进制数的后2位与第4个字符进行组合
		}
	}
	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;
	//建立连接
	c = connect_server(host, port);
	//收到信息
	recv_ms(c);
	//发送信息 hello
	send_ms(c, (char*)"helo qq\r\n\0");
	//告知登录
	recv_ms(c);

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

	//开始登录
	
	cout << "输入邮箱\n";
	cin >> email;
	
	//rcpt to:<zwt0216@126.com> 
	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表示授权码的base64加密
	recv_ms(c);//授权码



	memset(buf, 0, sizeof(buf));
	sprintf(buf, "mail from: <%s>\r\n\0", email);
	send_ms(c, buf);
	recv_ms(c);//发送去哪里

	printf("输入收件人\n");
	cin >> email;
	memset(buf, 0, sizeof(buf));
	sprintf(buf, "rcpt to: <%s>\r\n\0", email);
	
	send_ms(c, buf);
	recv_ms(c);//发送去哪里

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

	memset(buf, 0, sizeof(buf));
	cout << "输入标题\n";
	cout << "输入内容\n";
	cin >> buf;//标题
	cin >> email;//内容
	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;
}

//1972776062@qq.com
//zwt0216@126.com

 刚学,照着大佬写的,主要就是调用库,对理解计算机网络有重要作用

posted @ 2020-04-23 00:34  Lesning  阅读(202)  评论(0编辑  收藏  举报