這個程式目前126的郵箱經測試成功傳送,新浪的雖然顯示傳送成功但是總也收不到,有空再慢慢實驗吧,其他郵箱還未測試。

程式如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <unistd.h>
#include <netinet/in.h>
#include <limits.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <ctype.h>

// Function prototype
void StreamClient(char *szServer, short nPort, char *szMessage);
// Helper macro for displaying errors

////////////////////////////////////////////////////////////

int main(int argc, char **argv)
{
char hostname[255];
int nRet;
short nPort;
nPort =25;
StreamClient("smtp.126.com", nPort, "AUTH LOGIN\r\n");
return 0;
}

////////////////////////////////////////////////////////////

void StreamClient(char *szServer, short nPort, char *szMessage)
{

int sockfd;
char buffer[1024];
struct sockaddr_in server_addr;
struct hostent *host;

if((host=gethostbyname(szServer))==NULL)/*取得主機IP地址*/
{
fprintf(stderr,"Gethostname error, %s\n", strerror(errno));
exit(1);
}

if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)/*建立SOCKET連線*/
{
fprintf(stderr,"Socket Error:%s\a\n",strerror(errno));
exit(1);
}

/* 客戶程式填充服務端的資料 */
bzero(&server_addr,sizeof(server_addr));
server_addr.sin_family=AF_INET;
server_addr.sin_port=htons(nPort);
server_addr.sin_addr=*((struct in_addr *)host->h_addr);

/* 客戶程式發起連線請求 */
if(connect(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1)/*連線網站*/
{
fprintf(stderr,"Connect Error:%s\a\n",strerror(errno));
exit(1);
}

printf("\nStream Client connecting to server: %s on port: %d",szServer, nPort);

char szBuf[1024];
memset(szBuf,0,1024);
int nRet;
// strcpy(szBuf, "From the Client");
char buf[350]="0";
char server[250];
gethostname(server,250);
strcpy(buf, "HELO ");

strcat(buf, szServer);
strcat(buf, " \r\n");
printf("%s:::%d",buf,strlen(buf));

//
// Wait for a reply
//
nRet = recv(sockfd,szBuf,sizeof(szBuf)+1,0);  printf("\nData received OVER DATA: %s", szBuf);

nRet = send(sockfd, buf, strlen(buf), 0);printf("\nsend %s",buf);
nRet = recv(sockfd, szBuf, sizeof(szBuf), 0); printf("\nData received2: %s", szBuf);

//傳送準備登陸資訊
nRet = send(sockfd, "AUTH LOGIN\r\n", strlen("AUTH LOGIN\r\n"), 0);
nRet = recv(sockfd, szBuf, sizeof(szBuf), 0); printf("\nData received LOGIN: %s", szBuf);

//傳送使用者名稱和密碼,這裡的使用者名稱和密碼必須用base64進行轉碼,傳送轉碼以後的字串,對於126郵箱來說使用者名稱是@前面的字串
nRet = send(sockfd,"轉碼後的使用者名稱\r\n", strlen("轉碼後的使用者名稱\r\n"),0);printf("\nData Send USERNAME");
nRet = recv(sockfd, szBuf, sizeof(szBuf),0);printf("\nData received USERNAME: %s", szBuf);printf("\nData Send PASS");

//傳送使用者密碼
nRet = send(sockfd,"轉碼後的密碼\r\n", strlen("轉碼後的密碼\r\n"), 0);
//printf("\nData Send PASS");
nRet = recv(sockfd, szBuf, sizeof(szBuf),0); printf("\nData received USERPASSWORD: %s", szBuf);

//傳送[傳送郵件]的信箱(改成你的郵箱!),該郵箱要與使用者名稱一致,否則傳送不成功
send(sockfd,"MAIL FROM: <server_ip_alert@126.com>\r\n",strlen("MAIL FROM: <server_ip_alert@126.com>\r\n"),0);printf("\nsend MAIL FROM\n");
nRet = recv(sockfd, szBuf, sizeof(szBuf), 0);printf("\nData received MAILFROM: %s", szBuf);

//傳送[接收郵件]的郵箱
nRet= send(sockfd,"RCPT TO: <tootoogo@hotmail.com>\r\n",strlen("RCPT TO: <tootoogo@hotmail.com>\r\n"),0);printf("\nsend RCPT TO\r\n");
nRet = recv(sockfd, szBuf, sizeof(szBuf), 0);   printf("\nData received TO MAIL: %s", szBuf);
/*
nRet= send(sockfd,"RCPT TO: <server_ip_alert@hotmail.com>\r\n",strlen("RCPT TO: <server_ip_alert@hotmail.com>\r\n"),0);printf("\nsend RCPT TO\r\n");
nRet = recv(sockfd, szBuf, sizeof(szBuf), 0);   printf("\nData received TO MAIL: %s", szBuf);
*/
char MailData[] ="From: \"server_ip_alert@sina.com\"<server_ip_alert@sina.com>\r\nTo: tootoogo@hotmail.com\r\nSubject: IP Address\r\n\r\n"; //主題可以改成別的

//各訴郵件伺服器,準備傳送郵件內容

send(sockfd,"DATA\r\n", strlen("DATA\r\n"),0);printf("\nsend DATA\n");
//  nRet = recv(sockfd, szBuf, sizeof(szBuf)+1, 0); printf("\nData receivedSEND DATA: %s", szBuf);
//傳送郵件標題
send(sockfd,MailData, strlen(MailData),0);
//傳送郵件內容
send(sockfd,"test!!!!\r\n", strlen("abcdefg\r\n"),0);//我隨便寫了幾個字元進行測試
//傳送郵件結束
send(sockfd,"\r\n.\r\n", strlen("\r\n.\r\n"),0);
//接收郵件伺服器返回資訊
nRet = recv(sockfd,szBuf,sizeof(szBuf),0);  printf("\nData received OVER DATA: %s", szBuf);
send(sockfd,"QUIT\r\n", strlen("QUIT\r\n"),0);
nRet = recv(sockfd,szBuf,sizeof(szBuf),0);  printf("\nData received QUIT: %s", szBuf);

//
// Display the received data
//
//printf("\nData received3: %s", szBuf);

close(sockfd);
return;
}