主题:[讨论]vc发送邮件的奇怪现象
小弟初学c++,写了个发送邮件的代码。
执行时,在发送 auth login 命令到服务器时,出现invalid command 错误。
但在使用telnet方式发送auth login 到服务器时,可以识别。
网上也搜不到原因。所以来这里请各位帮忙解答。小弟在这里谢了!
附上部分代码:
int main()
{
char buff[1024];
struct WSAData wsaData;//这个结构被用来存储 被WSAStartup函数调用后返回的 Windows Sockets 数据
WORD wVersionRequested = MAKEWORD(2, 2);
SOCKET sockSmtp = INVALID_SOCKET;
struct sockaddr_in addrSmtp;
//Windows套接字异步的启动命令,完成对Winsock服务的初始化
//该函数的第一个参数指明程序请求使用的Socket版本,第二个参数返回请求的Socket的版本信息
//操作系统根据请求的Socket版本来搜索相应的Socket库,然后绑定找到的Socket库到该应用程序中
if(WSAStartup(wVersionRequested, &wsaData) != 0)
{
printf("error when execute WSAStartup()\n");
WSACleanup();//必须调用WSACleanup()以允许Windows Sockets DLL释放任何该应用程序的资源
return -1;
}
if(sockSmtp != NULL)
{
printf("close socket...\n");
closesocket(sockSmtp);//关闭socket
printf("closed.\n");
sockSmtp = NULL;
}
if(sockSmtp == NULL)
{
printf("initialize socket...\n");
sockSmtp = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
printf("initialize ok.\n");
}
struct hostent *hosts = gethostbyname("smtp.163.com");//返回对应于给定主机名的包含主机名字和地址信息的hostent结构指针
if(hosts == NULL)
{
printf("the host name is invalid.\n");
WSACleanup();
return -1;
}
struct in_addr in;//用来表示一个32位的IPv4地址
memcpy((char**)&(in), hosts->h_addr_list[0], hosts->h_length);//由一个内存区域复制字节到另一个内存区域
char* addrc = inet_ntoa(in);//将网络地址转换成“.”点隔的字符串格式
addrSmtp.sin_family = AF_INET;//指代协议族,在socket编程中只能是AF_INE
addrSmtp.sin_addr.s_addr = in.S_un.S_addr;//存储IP地址,使用in_addr这个数据结构
// memcpy(&addrSmtp.sin_addr.S_un, hosts->h_addr_list[0], hosts->h_length);
addrSmtp.sin_port = htons(25);//存储端口号,htons()将一个无符号短整型数值转换为网络字节序
int ret = connect(sockSmtp, (LPSOCKADDR)&addrSmtp, sizeof(addrSmtp));
if(ret == SOCKET_ERROR)
{
char *err = new char[1024];
sprintf(err,"failed to connect.%d",GetLastError());
printf("%s\n",err);
WSACleanup();
return -1;
}
printf("Connect to %s...\n", addrc);
ZeroMemory(buff, sizeof(buff));//宏,用0来填充一块内存区域(内存块清零)
ret = recv(sockSmtp, buff, sizeof(buff), 0);//从一个套接口接收数据
if(ret == SOCKET_ERROR)
{
printf("failed to connect.\n");
WSACleanup();
return -1;
}
printf("ehlo %s...\n", buff);
ret = send(sockSmtp, "ehlo 163.com\r\n", sizeof("ehlo 163.com\r\n"), 0);//经套接字传送消息
if(ret == SOCKET_ERROR)
{
printf("send hello to server failure.\n");
WSACleanup();
return -1;
}
ZeroMemory(buff, sizeof(buff));
ret = recv(sockSmtp, buff, sizeof(buff), 0);
if(ret == SOCKET_ERROR)
{
printf("receive data from server failure.\n");
WSACleanup();
return -1;
}
printf("response: %s \n", buff);
printf("auth login\n");
ret = send(sockSmtp, "auth login\r\n", sizeof("auth login\r\n"), 0);
if(ret == SOCKET_ERROR)
{
printf("send auth login to server failure.\n");
WSACleanup();
return -1;
}
ZeroMemory(buff, sizeof(buff));
ret = recv(sockSmtp, buff, sizeof(buff), 0);
if(ret == SOCKET_ERROR)
{
printf("receive data from server failure.\n");
WSACleanup();
return -1;
}
printf("response: %s \n", buff);
}
执行时,在发送 auth login 命令到服务器时,出现invalid command 错误。
但在使用telnet方式发送auth login 到服务器时,可以识别。
网上也搜不到原因。所以来这里请各位帮忙解答。小弟在这里谢了!
附上部分代码:
int main()
{
char buff[1024];
struct WSAData wsaData;//这个结构被用来存储 被WSAStartup函数调用后返回的 Windows Sockets 数据
WORD wVersionRequested = MAKEWORD(2, 2);
SOCKET sockSmtp = INVALID_SOCKET;
struct sockaddr_in addrSmtp;
//Windows套接字异步的启动命令,完成对Winsock服务的初始化
//该函数的第一个参数指明程序请求使用的Socket版本,第二个参数返回请求的Socket的版本信息
//操作系统根据请求的Socket版本来搜索相应的Socket库,然后绑定找到的Socket库到该应用程序中
if(WSAStartup(wVersionRequested, &wsaData) != 0)
{
printf("error when execute WSAStartup()\n");
WSACleanup();//必须调用WSACleanup()以允许Windows Sockets DLL释放任何该应用程序的资源
return -1;
}
if(sockSmtp != NULL)
{
printf("close socket...\n");
closesocket(sockSmtp);//关闭socket
printf("closed.\n");
sockSmtp = NULL;
}
if(sockSmtp == NULL)
{
printf("initialize socket...\n");
sockSmtp = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
printf("initialize ok.\n");
}
struct hostent *hosts = gethostbyname("smtp.163.com");//返回对应于给定主机名的包含主机名字和地址信息的hostent结构指针
if(hosts == NULL)
{
printf("the host name is invalid.\n");
WSACleanup();
return -1;
}
struct in_addr in;//用来表示一个32位的IPv4地址
memcpy((char**)&(in), hosts->h_addr_list[0], hosts->h_length);//由一个内存区域复制字节到另一个内存区域
char* addrc = inet_ntoa(in);//将网络地址转换成“.”点隔的字符串格式
addrSmtp.sin_family = AF_INET;//指代协议族,在socket编程中只能是AF_INE
addrSmtp.sin_addr.s_addr = in.S_un.S_addr;//存储IP地址,使用in_addr这个数据结构
// memcpy(&addrSmtp.sin_addr.S_un, hosts->h_addr_list[0], hosts->h_length);
addrSmtp.sin_port = htons(25);//存储端口号,htons()将一个无符号短整型数值转换为网络字节序
int ret = connect(sockSmtp, (LPSOCKADDR)&addrSmtp, sizeof(addrSmtp));
if(ret == SOCKET_ERROR)
{
char *err = new char[1024];
sprintf(err,"failed to connect.%d",GetLastError());
printf("%s\n",err);
WSACleanup();
return -1;
}
printf("Connect to %s...\n", addrc);
ZeroMemory(buff, sizeof(buff));//宏,用0来填充一块内存区域(内存块清零)
ret = recv(sockSmtp, buff, sizeof(buff), 0);//从一个套接口接收数据
if(ret == SOCKET_ERROR)
{
printf("failed to connect.\n");
WSACleanup();
return -1;
}
printf("ehlo %s...\n", buff);
ret = send(sockSmtp, "ehlo 163.com\r\n", sizeof("ehlo 163.com\r\n"), 0);//经套接字传送消息
if(ret == SOCKET_ERROR)
{
printf("send hello to server failure.\n");
WSACleanup();
return -1;
}
ZeroMemory(buff, sizeof(buff));
ret = recv(sockSmtp, buff, sizeof(buff), 0);
if(ret == SOCKET_ERROR)
{
printf("receive data from server failure.\n");
WSACleanup();
return -1;
}
printf("response: %s \n", buff);
printf("auth login\n");
ret = send(sockSmtp, "auth login\r\n", sizeof("auth login\r\n"), 0);
if(ret == SOCKET_ERROR)
{
printf("send auth login to server failure.\n");
WSACleanup();
return -1;
}
ZeroMemory(buff, sizeof(buff));
ret = recv(sockSmtp, buff, sizeof(buff), 0);
if(ret == SOCKET_ERROR)
{
printf("receive data from server failure.\n");
WSACleanup();
return -1;
}
printf("response: %s \n", buff);
}