|
1鱼币
#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[256]={0};
char buf[350]="0";
char server[250];
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[1024] = "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);
}
|
|