经过了这个系列的前几篇文章的学习,现在要写出一个完整的 smtp 邮件发送过程简直易如反掌。
例如我们可以轻松地写出以下的纯 C 语言代码(引用的其他C语言文件请看文末的 github 地址):
#include <stdio.h> #include <windows.h> #include <time.h> #include <winsock.h> #include "lstring.c" #include "socketplus.c" #include "lstring_functions.c" #include "base64_functions.c" //vc 下要有可能要加 lib //#pragma comment (lib,"*.lib") //#pragma comment (lib,"libwsock32.a") //#pragma comment (lib,"libwsock32.a") //SOCKET gSo = 0; SOCKET gSo = -1; //收取一行,可再优化 lstring * RecvLine(SOCKET so, struct MemPool * pool, lstring ** _buf) { int i = 0; int index = -1; int canread = 0; lstring * r = NULL; lstring * s = NULL; lstring * buf = *_buf; for (i=0;i<10;i++) //安全起见,不用 while ,用 for 一定次数就可以了 { //index = pos(" ", buf); index = pos(NewString(" ", pool), buf); if (index>-1) break; canread = SelectRead_Timeout(so, 3);//是否可读取,时间//超时返回,单位为秒 if (0 == canread) break; s = RecvBuf(so, pool); buf->Append(buf, s); } if (index <0 ) return NewString("", pool); r = substring(buf, 0, index); buf = substring(buf, index + 2, Length(buf)); *_buf = buf; return r; }// //解码一行命令,这里比较简单就是按空格进行分隔就行了 //这是用可怕的指针运算的版本 void DecodeCmd(lstring * line, char sp, char ** cmds, int cmds_count) { int i = 0; int index = 0; int count = 0; cmds[index] = line->str; for (i=0; i<line->len; i++) { if (sp == line->str[i]) { index++; line->str[i] = '