邮件发送程序,请大家研究下为什么实现不了功能,互相学习
#include <stdio.h>#include <winsock2.h>
#include <stdlib.h>
#include <string.h>
#pragma comment(lib,"ws2_32.lib")
void StreamClient(char *szServer, short nPort, char *szMessage);
int main()
{
WORD wVersionRequested = MAKEWORD(2,0);
WSADATA wsaData;
int nRet;
short nPort;
nPort =25;
nRet = WSAStartup(wVersionRequested, &wsaData);
StreamClient("smtp.126.com", nPort, "AUTH LOGIN\r\n");
WSACleanup();
return 0;
}
void StreamClient(char *szServer, short nPort, char *szMessage)
{
printf("\nStream Client connecting to server: %s on port:%d\n",szServer,nPort);
LPHOSTENT lpHostEntry;
lpHostEntry = gethostbyname(szServer);
if (lpHostEntry == NULL)
{
printf("gethostbyname()/n");
return;
}
SOCKET theSocket; //创建套字节
theSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (theSocket == INVALID_SOCKET)
{
printf("socket()");
return;
}
SOCKADDR_IN saServer;
saServer.sin_family = AF_INET;
saServer.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
saServer.sin_port = htons(nPort);
//进行连接
int nRet;
nRet = connect( theSocket,(LPSOCKADDR)&saServer,sizeof(struct sockaddr));
printf("\n%d1lL=====================================%d\n",nRet,SOCKET_ERROR);
if (nRet == SOCKET_ERROR)
{
printf("连接失败\n");
closesocket(theSocket);
return;
}
char szBuf={0};
char buf="0";
char server;
gethostname(server,250);
strcpy(buf, "HELO ");
strcat(buf, server);
strcat(buf, " \r\n");
printf("%s:::%d\n",buf,strlen(buf));
nRet = recv(theSocket,szBuf,sizeof(szBuf)+1,0);
printf("接受服务器已连接: %s\n", szBuf);
nRet = send(theSocket, buf, strlen(buf), 0);
nRet = recv(theSocket, szBuf, sizeof(szBuf),0);
printf("连接通过: %s\n",szBuf);
//发送准备登陆信息
nRet = send(theSocket, "AUTH LOGIN \r\n", sizeof("AUTH LOGIN \r\n"), 0);
nRet = recv(theSocket, szBuf, sizeof(szBuf), 0);
printf("验证:%s\n",szBuf);
//发送用户名注意户名必须用base64进行转码,发送转码以后的字符串
nRet = send(theSocket,"cWJ5dTIwMDNAMTI2LmNvbQ==\r\n", strlen("cWJ5dTIwMDNAMTI2LmNvbQ==\r\n"),0);
nRet = recv(theSocket, szBuf, sizeof(szBuf),0);
printf("输入账户:%s\n", szBuf);
//发送用户密码必须用base64进行转码,发送转码以后的字符串
nRet = send(theSocket,"NDIwODQxNjk4\r\n", strlen("NDIwODQxNjk4\r\n"),0);
nRet = recv(theSocket, szBuf, sizeof(szBuf),0);
printf("要求输入密码:%s",szBuf);
//发送的信箱
send(theSocket,"MAIL FROM:<qbyu2003@126.com>\r\n",sizeof("MAIL FROM:<qbyu2003@126.com>\r\n"),0);
printf("\n发送的邮箱:%s\n","qbyu2003@126.com");
//接收邮件的邮箱
nRet= send(theSocket,"RCPT TO:<qbyu2007@163.com> \r\n",sizeof("RCPT TO:<qbyu2007@163.com> \r\n"),0);
printf("\n收信邮箱:%s\n","qbyu2007@163.com");
char MailData = "From:\"qbyu2003@126.com\"\r\n"
"Subject: IP Address\r\n.\r\n";
//告示邮件服务器,准备发送邮件内容
send(theSocket,"DATA\r\n", strlen("DATA\r\n"),0);
nRet = recv(theSocket, szBuf, sizeof(szBuf)+1, 0);
printf("\n发送状况:%s\n",szBuf);
//发送邮件标题
send(theSocket,MailData, strlen(MailData),0);
//发送邮件内容
send(theSocket,"邮件内容来了!\r\n", strlen("邮件内容来了!\r\n"),0);
//发送邮件结束
send(theSocket,"\r\n.\r\n", strlen("\r\n.\r\n"),0);
//接收邮件服务器返回信息
nRet = recv(theSocket,szBuf,sizeof(szBuf)+1,0);
printf("返回信息:%s\n",szBuf);
closesocket(theSocket);
}
调试了下,发现前面都还正常,但是一旦执行MAIL TO:<XXX>命令后就出现了502错误,说是命令无法解析。有些无语了~
若只是学习,还望楼主多多百度,若确实需要,建议使用封装好的模块来写。 是用来学习的,因为在网上找了很多,都插不多一样的所以才在这里请大家讨论,纠正,至于为什么不用c++来模块化,是为给c语言的朋友也学习一下! smtp.126.com 沉醉东风 发表于 2012-4-23 13:24 static/image/common/back.gif
smtp.126.com
http://elearning.hnteacher.net/fck/file/2012-4-23/61854ba3-9dd7-464b-977f-70964070d5a2.png
VC还是不行,不知道怎么回事,用python试了下,是可以的。
from socket import *
from sys import exit
s=socket(AF_INET,SOCK_STREAM)
host=gethostbyname('smtp.163.com')
local=gethostname()
try:
s.connect((host,25))
print '[+] Connected!\n'
except:
print '[!] Connecte failed!'
exit(0)
print 'HELO '+local+':'
s.send('HELO '+local+'\r\n')
print s.recv(1024)
print 'AUTH LOGIN:'
s.send('AUTH LOGIN \r\n')
print s.recv(1024)
print 'UserName:'
s.send('base64(username)\r\n')
print s.recv(1024)
print 'PassWord:'
s.send('base64(pass)\r\n')
print s.recv(1024)
print s.recv(1024)
print 'MAIL FROM:'
s.send('MAIL FROM:<hack_littleevil@163.com>\r\n')
print s.recv(1024)
print 'RCPT TO:'
s.send('RCPT TO:<1227763328@qq.com>\r\n')
print s.recv(1024)
print 'Send DATA:'
s.send('data\r\n')
print s.recv(1024)
print 'Send Mail:'
s.send('From:"hack_littleevil@163.com"\r\n'\
'Subject: test it!\r\n'\
'Content-Type: text/plain\r\n'\
'Hello little evil!\r\n'\
'\r\n.\r\n')
print s.recv(1024)
print 'Send end flag:'
s.send('\r\n.\r\n')
print s.recv(1024)
s.close()
我也纳闷为什么vc不行 本帖最后由 □ル豪﹏? 于 2012-8-20 10:13 编辑
xuexi i xia 学习学习
页:
[1]