本文共 3790 字,大约阅读时间需要 12 分钟。
-
-
-
- #include "stdafx.h"
- #include <stdlib.h>
- #include <stdio.h>
- #include <conio.h>
- #include <winsock.h>
-
- #include "base64.h"
-
- #pragma comment( lib,"ws2_32.lib" )
-
- #define WSVERS MAKEWORD(2, 0)
-
- SOCKET connectTCP(const char *host, const char *service );
- void errexit(const char *format, ...);
-
- FILE* pf_log;
-
- void ReveiveMsg(SOCKET sock, char* checkstr = "/r/n")
- {
- char buffer[1024];
-
- fwrite("Server: ",1,strlen("Server: "),pf_log);
- while(1){
- int len = recv(sock,buffer,1024,0);
- if (len <= 0) break;
-
- fwrite(buffer,1,len,pf_log);
-
- buffer[len] = 0;
- printf("Server: %s",buffer);
-
-
- if (strstr(buffer,checkstr)) break;
- }
-
- }
-
- void SendMsg(SOCKET sock,char* msg)
- {
- fwrite("Client: ",1,strlen("Client: "),pf_log);
-
- int msgLen = strlen(msg);
- send(sock,msg,msgLen,0);
- printf("Client: %s",msg);
- fwrite(msg,1,msgLen,pf_log);
- }
-
- int main(int argc, char* argv[])
- {
- WSADATA wsadata;
-
- if (WSAStartup(WSVERS, &wsadata) != 0)
- errexit("WSAStartup failed/n");
-
- pf_log = fopen("log.txt","wb");
-
-
- SOCKET sock = connectTCP("smtp.163.com","smtp");
-
- ReveiveMsg(sock);
- SendMsg(sock,"EHLO 163.com/r/n");
- ReveiveMsg(sock,"250 8BITMIME/r/n");
- SendMsg(sock,"AUTH LOGIN/r/n");
- ReveiveMsg(sock);
-
-
- int codelen;
- char username[]="ficefjb";
- char *base64code;
- base64code = base64_encode(username,strlen(username));
- codelen = strlen(base64code);
- char username_base64[256];
- strcpy(username_base64,base64code);
- username_base64[codelen] = 0x0d;
- username_base64[codelen + 1] = 0x0a;
- username_base64[codelen + 2] = 0;
-
- SendMsg(sock,username_base64);
- ReveiveMsg(sock);
-
-
- printf("Please input password:/n");
- char password[256];
- SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE),ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT);
- scanf("%s",password);
- base64code = base64_encode(password,strlen(password));
- codelen = strlen(base64code);
- char password_base64[256];
- strcpy(password_base64,base64code);
- password_base64[codelen] = 0x0d;
- password_base64[codelen + 1] = 0x0a;
- password_base64[codelen + 2] = 0;
-
- SendMsg(sock,password_base64);
- ReveiveMsg(sock);
-
- SendMsg(sock,"MAIL FROM:<ficefjb@163.com>/r/n");
- ReveiveMsg(sock);
- SendMsg(sock,"RCPT TO:<ficefjb@163.com>/r/n");
- ReveiveMsg(sock);
-
-
-
- SendMsg(sock,"DATA/r/n");
- ReveiveMsg(sock);
-
-
- SendMsg(sock,"TO: ficefjb@163.com/r/nFROM: ficefjb@163.com/r/nSUBJECT: SMTP协议测试/r/nDate:2009-12-5/r/nX-Mailer:fice's mailer/r/n");
- SendMsg(sock,"MIMI-Version:1.0/r/n");
- SendMsg(sock,"Content-Type:multipart/mixed;boundary=/"#BOUNDARY#/"/r/n/r/n");
- SendMsg(sock,"Content-Transfer-Encoding:7bit/r/n/r/n");
- SendMsg(sock,"This is a multi-part message in MIME format/r/n/r/n");
-
-
- SendMsg(sock,"--#BOUNDARY#/r/n");
- SendMsg(sock,"Content-Type: text/plain;charset=gb2312/r/n");
- SendMsg(sock,"Content-Transfer-Encoding:printable/r/n/r/n");
-
-
- SendMsg(sock,"SMTP协议测试:发送附件/n----by fice 2009.12.5/r/n");
-
-
- SendMsg(sock,"--#BOUNDARY#/r/n");
- SendMsg(sock,"Content-Type:text/plain;name=student.txt/r/n");
- SendMsg(sock,"Content-Transfer-Encoding:base64/r/n");
- SendMsg(sock,"Content-Disposition:attachment;filename=/"student.txt/"/r/n/r/n");
-
-
- FILE* pf = fopen("d://student.txt","rb");
- fseek(pf,0,SEEK_END);
- int filelen = ftell(pf);
- fseek(pf,0,SEEK_SET);
- char* filebuf = (char*) malloc(filelen);
- fread(filebuf,1,filelen,pf);
- char* filebase64 = base64_encode(filebuf,filelen);
-
-
- SendMsg(sock,filebase64);
-
- SendMsg(sock,"/r/n./r/n");
-
- ReveiveMsg(sock);
- SendMsg(sock,"QUIT/r/n");
- ReveiveMsg(sock);
-
- fclose(pf_log);
-
- closesocket(sock);
- WSACleanup();
-
- printf("Press any key to quit./n");
- getche();
-
- return 0;
- }
转载地址:http://fbhvi.baihongyu.com/