#include<iostream>
#include<WinSock2.h>
#pragma comment(lib,"ws2_32.lib")
using namespace std;
SOCKET m_socket;
void recv(SOCKET sock);
void Send(SOCKET sock,char *content,int len);
void base64_encodar(char *in_str, int in_len, char*out_str);//解码函数
int getLength(char *s);
int main()
{
WSADATA wsa;
WSAStartup(MAKEWORD(2, 2), &wsa);
char buf1[100];
memset(buf1, '\0', 100);
char buf2[100];
memset(buf2, '\0', 100);
char addr[100];
memset(addr, '\0', 100);
cout << "选择你要登录的邮箱(邮箱需先开通smtp功能!)" << endl;
int choose;
hostent *hptr = NULL;;
cout << "1.qq 2.网易163 3.新浪" << endl;
cin >> choose;
switch (choose)
{
case 1:
hptr = gethostbyname("smtp.qq.com");
break;
case 2:
hptr = gethostbyname("smtp.163.com");
break;
case 3:
hptr = gethostbyname("smtp.sina.com");
break;
}
sockaddr_in sockaddr;
sockaddr.sin_addr.S_un.S_addr = *(unsigned long*)hptr->h_addr_list[0];
sockaddr.sin_family = AF_INET;
sockaddr.sin_port = htons(25);
m_socket = socket(PF_INET, SOCK_STREAM, 0);
if (connect(m_socket, (struct sockaddr*)&sockaddr, sizeof(struct sockaddr)) < 0)
{
cout << "连接服务器失败!,错误代码: " ;
cerr << WSAGetLastError << endl;
return 0;
}
recv(m_socket);//连接后的返回数据
strcpy(buf1, "helo server\r\n");//打招呼
Send(m_socket, buf1, getLength(buf1));
recv(m_socket);
memset(buf1, '\0', 100);
strcpy(buf1, "auth login\n");//登录
Send(m_socket, buf1, getLength(buf1));
recv(m_socket);
memset(buf1, '\0', 100);
cout << "输入邮箱帐号: ";
cin >>addr;
base64_encodar(addr, getLength(addr), buf2);
strcat(buf2, "\r\n");
Send(m_socket, buf2,getLength(buf2));
recv(m_socket);
cout << "输入密码: ";
cin >> buf1;
base64_encodar(buf1, getLength(buf1), buf2);
strcat(buf2, "\r\n");
Send(m_socket, buf2, getLength(buf2));
recv(m_socket);
memset(buf1, '\0', 100);
sprintf(buf1, "mail from:<%s>\r\n", addr);
Send(m_socket, buf1, getLength(buf1));
recv(m_socket);
memset(buf1, '\0', 100);
cout << "输入你要发件的邮箱";
cin >> buf1;
sprintf(buf2, "rcpt to:<%s>\r\n", buf1);
Send(m_socket, buf2, getLength(buf2));
recv(m_socket);
memset(buf1, '\0', 100);
memset(buf2, '\0', 100);
strcpy(buf1, "data\r\n");
Send(m_socket, buf1, getLength(buf1));
recv(m_socket);
memset(buf1, '\0', 100);
cout << "输入邮件内容";
cin >> buf1;/*subject:test email*/
sprintf(buf2, "\r\n%s\r\n\.\r\n",buf1);
Send(m_socket, buf2, getLength(buf2));
recv(m_socket);
memset(buf1, '\0', 100);
memset(buf2, '\0', 100);
system("pause");
return 0;
}
void recv(SOCKET sock)
{
char buf[100];
memset(buf, 0, 100);
int len = recv(sock, buf, 100, 0);
cout <<"Receive: "<< buf << endl;
}
void Send(SOCKET sock, char *content,int len)
{
cout << "Send: " << content << endl;;
send(sock, content, len,0);
}
int getLength(char *s)
{
int i=0 ;
while (s[i] != '\0')
i++;
return i;
}
void base64_encodar(char *in_str, int in_len, char*out_str)
{
int i = 0;
char base64_table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int curr_out = 0;
printf("%d\n", in_len);
if (in_len>0)
{
while (i<in_len)
{
char a = in_str[i];
char b = i + 1 >= in_len ? 0 : in_str[i + 1];
char c = i + 2 >= in_len ? 0 : in_str[i + 2];
if (i + 2<in_len) /*余0*/
{
out_str[curr_out++] = base64_table[(a >> 2)&(0x3f)]; //the first
out_str[curr_out++] = base64_table[((a & 0x03) << 4) + (b >> 4)]; //the first and the second
out_str[curr_out++] = base64_table[((b & 0x0f) << 2) + (c >> 6)]; //the second and the third
out_str[curr_out++] = base64_table[c & 0x3f]; //the third
}
else if (i + 1<in_len) /*余2*/
{
out_str[curr_out++] = base64_table[(a >> 2)&(0x3f)]; //the first
out_str[curr_out++] = base64_table[((a & 0x03) << 4) + (b >> 4)]; //the first and the second
out_str[curr_out++] = base64_table[(b & 0x0f) << 2]; //the last low 4 bits of the second
out_str[curr_out++] = '='; //fill with the '='
// printf("hello!\n");
}
else /*余1*/
{
out_str[curr_out++] = base64_table[(a >> 2)&(0x3f)]; //the first
out_str[curr_out++] = base64_table[(a&(0x03)) << 4]; //the rest of of the first
out_str[curr_out++] = '='; //fill with '=' in the last two bits
out_str[curr_out++] = '=';
}
i += 3;
}
out_str[curr_out] = 0;
}
};
socket发送邮件
最新推荐文章于 2020-07-12 10:13:23 发布