#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib")
int Base64Enc(char *buf, char*text,int size)
{ //摘抄自网络。
static char *base64_encoding = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int buflen = 0;
while(size>0)
{
*buf++ = base64_encoding[ (text[0] >> 2 ) & 0x3f];
if(size>2)
{
*buf++ = base64_encoding[((text[0] & 3) << 4) | (text[1] >> 4)];
*buf++ = base64_encoding[((text[1] & 0xF) << 2) | (text[2] >> 6)];
*buf++ = base64_encoding[text[2] & 0x3F];
}
else
{
switch(size)
{
case 1:
*buf++ = base64_encoding[(text[0] & 3) << 4 ];
*buf++ = '=';
*buf++ = '=';
break;
case 2:
*buf++ = base64_encoding[((text[0] & 3) << 4) | (text[1] >> 4)];
*buf++ = base64_encoding[((text[1] & 0x0F) << 2) | (text[2] >> 6)];
*buf++ = '=';
break;
}
}
text +=3;
size -=3;
buflen +=4;
}
*buf = 0;
return buflen;
}
int main()
{
//以下代码改编自网上的汇编代码,注意暂时没有添加附件的功能。
//听说telnet也可以实现,但没有试验过,或则没有实验成功。
//原理搞懂,改写成类,那太容易了。
WSADATA wsa;
WSAStartup(MAKEWORD(2,2),&wsa);
SOCKET hSocket = socket( AF_INET, SOCK_STREAM, 0);
hostent * localHost = gethostbyname("smtp.126.com");
char * localIP = inet_ntoa (*(struct in_addr *)*localHost->h_addr_list);
SOCKADDR_IN stSin;
stSin.sin_family = AF_INET;
stSin.sin_port = htons(25);
stSin.sin_addr.s_addr = inet_addr(localIP);//这里可以直接填写ip地址,省去了上面的两行。
connect(hSocket, (const sockaddr *)&stSin, sizeof (stSin));
char szRcvBuffer[1024] = {0};
recv(hSocket, szRcvBuffer,sizeof (szRcvBuffer),0);
int x = send( hSocket,"EHLO Local\r\n", lstrlenA ("EHLO Local\r\n"), 0);//\r\n也可以。\n也可以.
RtlZeroMemory(szRcvBuffer,sizeof (szRcvBuffer));
recv(hSocket,szRcvBuffer,sizeof (szRcvBuffer),0);
x = send(hSocket,"AUTH LOGIN\r\n",lstrlenA ("AUTH LOGIN\r\n"), 0);//千万不要用sizeof.
RtlZeroMemory(szRcvBuffer,sizeof (szRcvBuffer));
recv(hSocket,szRcvBuffer,sizeof (szRcvBuffer),0);
char en_name[260] = {0};
Base64Enc(en_name,"leguanyuan",lstrlenA("leguanyuan"));
x = lstrlenA(en_name);
en_name[x] = 13;
en_name[x + 1] = 10;
send(hSocket, en_name, lstrlenA(en_name), 0);
RtlZeroMemory(szRcvBuffer,sizeof (szRcvBuffer));
recv(hSocket,szRcvBuffer,sizeof (szRcvBuffer),0);
char en_psd[260] = {0};
Base64Enc(en_psd,"**********",lstrlenA("*********"));//注释,密码暂时隐藏。
x = lstrlenA(en_psd);
en_psd[x] = 13;
en_psd[x + 1] = 10;
send(hSocket, en_psd, lstrlenA(en_psd), 0);
RtlZeroMemory(szRcvBuffer,sizeof (szRcvBuffer));
recv(hSocket,szRcvBuffer,sizeof (szRcvBuffer),0);
send(hSocket, "MAIL FROM:<leguanyuan@126.com>\r\n", lstrlenA("MAIL FROM:<leguanyuan@126.com>\r\n"), 0);
RtlZeroMemory(szRcvBuffer,sizeof (szRcvBuffer));
recv(hSocket,szRcvBuffer,sizeof (szRcvBuffer),0);
send(hSocket, "RCPT TO:<112426112@qq.com>\r\n", lstrlenA("RCPT TO:<112426112@qq.com>\r\n"), 0);
RtlZeroMemory(szRcvBuffer,sizeof (szRcvBuffer));
recv(hSocket,szRcvBuffer,sizeof (szRcvBuffer),0);
send(hSocket, "DATA\r\n", lstrlenA("DATA\r\n"), 0);
RtlZeroMemory(szRcvBuffer,sizeof (szRcvBuffer));
recv(hSocket,szRcvBuffer,sizeof (szRcvBuffer),0);
send(hSocket, "From:leguanyuan@126.com\r\n", lstrlenA("From:leguanyuan@126.com\r\n"), 0);
send(hSocket, "TO:112426112@qq.com\r\n", lstrlenA("TO:112426112@qq.com\r\n"), 0);
send(hSocket, "SUBJECT:hello\r\n", lstrlenA("SUBJECT:hello\r\n"), 0);
send(hSocket, "Reply-TO:leguanyuan@126.com\r\n", lstrlenA("Reply-TO:leguanyuan@126.com\r\n"), 0);
send(hSocket, "\r\n", lstrlenA("\r\n"), 0);
send(hSocket, "hello\r\n", lstrlenA("hello\r\n"), 0);
send(hSocket, ".\r\n", lstrlenA(".\r\n"), 0);
RtlZeroMemory(szRcvBuffer,sizeof (szRcvBuffer));
recv(hSocket,szRcvBuffer,sizeof (szRcvBuffer),0);
send(hSocket, "QUIT\r\n", lstrlenA("QUIT\r\n"), 0);
closesocket(hSocket);
WSACleanup();
return 0;
}
//made at 2011.12.06
没有评论:
发表评论