邮件作为最先和最广的互联应网用之一,已经与人们的生活息息相关。咱们虽然常常使用 Outlook Express/Outlook/Foxmail 等邮件客户端发送邮件,但并不关心发送过程的细节。若是您是一名程序员,则偶尔须要本身写一个小程序来实现发送邮件的目的,本文将会介绍如何使用 acl 里的 smtp_client 模块快速实现一个邮件发送的客户端程序。下面是发送邮件一个简单流程:程序员
220 inc365.com ESMTP mail for UNIX (mail1.0)小程序
ehlo localhost服务器
250-inc365.com并发
250-PIPELININGdom
250-SIZE 51200000svn
250-VRFY函数
250-ETRNoop
250-AUTH LOGINui
250-AUTH=LOGIN编码
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
auth login
334 dXNlcm5hbWU6
emhlbmdzaHV4aW5AaW5jMzY1LmNvbQ==
334 cGFzc3dvcmQ6
YWFhYWFh
235 2.0.0 Authentication successful
mail from: aaaa@sss.ssss.ssss
250 2.1.0 Ok
rcpt to: zhengshuxin@inc365.com
250 2.1.5 Ok
data
354 End data with <CR><LF>.<CR><LF>
subject: hello world
from: aaaa@sss.ssss.ssss
to: zhengshuxin@inc365.com
hello!
.
250 2.0.0 Ok: queued
quit
221 2.0.0 Bye
其中,黄色部分为邮件客户端的命令请求过程,红色部分为邮件服务器的命令响应过程。该交互过程并不复杂,其实客户端发送的命令主要有:
1)ehlo/helo: 问候服务器命令
2)auth login: SMTP 身份认证命令
3)mail from: 发送发件人邮箱地址命令
4)rcpt to: 发送收件人邮箱地址命令
5)data: 发送开始发邮件数据的命令,以行为单位发送数据,所发送数据须要通过编码处理(采用 base64/qp 等编码)转为可打印的字符串,当发送一行数据中只有 . 时则表示邮件数据完毕
6)quit: 退出发送过程命令
邮件发送过程虽然简单,但每次都从新写发送过程未免罗嗦,因此在 acl 项目的 lib_protocol 库中提供了 smtp_client 模块用来发送邮件。在 lib_protocol/include/smtp/smtp_client.h 中提供了经常使用的 SMTP 发送协议的函数接口,以下所示:
/** * 远程链接 SMTP 服务器 * @param addr {const char*} SMTP 服务器地址,格式:domain:port * @param timeout {int} 链接超时时间及IO读写超时时间 * @param line_limit {int} SMTP 会话过程当中每行的最大长度限制 * @return {SMTP_CLIENT*} 链接成功返回非空值,不然返回 NULL */ SMTP_API SMTP_CLIENT *smtp_open(const char *addr, int timeout, int line_limit); /** * 关闭由 smtp_open 打开的 SMTP 链接并释放对象 * @param client {SMTP_CLIENT*} SMTP 链接对象 */ SMTP_API void smtp_close(SMTP_CLIENT *client); /** * 得到 SMTP 服务器的欢迎信息 * @param client {SMTP_CLIENT*} SMTP 链接对象 * @return {int} 0 表示成功(SMTP_CLIENT::smtp_code 表示返回码, * SMTP_CLIENT::buf 存储响应内容),不然表示出错,应该关闭链接对象 */ SMTP_API int smtp_get_banner(SMTP_CLIENT *client); /** * 向 SMTP 服务器发送 HELO/EHLO 命令 * @param client {SMTP_CLIENT*} SMTP 链接对象 * @param name {const char*} 握手信息,通常用域名 * @param ehlo {int} 非 0 时使用 EHLO,不然使用 HELO * @return {int} 0 表示成功(SMTP_CLIENT::smtp_code 表示返回码, * SMTP_CLIENT::buf 存储响应内容),不然表示出错,应该关闭链接对象 */ SMTP_API int smtp_greet(SMTP_CLIENT *client, const char* name, int ehlo); /** * 向 SMTP 服务器发送 HELO 命令 * @param client {SMTP_CLIENT*} SMTP 链接对象 * @param helo {const char*} 握手信息,通常用域名 * @return {int} 0 表示成功(SMTP_CLIENT::smtp_code 表示返回码, * SMTP_CLIENT::buf 存储响应内容),不然表示出错,应该关闭链接对象 */ SMTP_API int smtp_helo(SMTP_CLIENT *client, const char *helo); /** * 向 SMTP 服务器发送 EHLO 命令 * @param client {SMTP_CLIENT*} SMTP 链接对象 * @param ehlo {const char*} 握手信息,通常用域名 * @return {int} 0 表示成功(SMTP_CLIENT::smtp_code 表示返回码, * SMTP_CLIENT::buf 存储响应内容),不然表示出错,应该关闭链接对象 */ SMTP_API int smtp_ehlo(SMTP_CLIENT *client, const char *ehlo); /** * 向 SMTP 服务器发送验证信息 * @param client {SMTP_CLIENT*} SMTP 链接对象 * @param user {const char*} SMTP 邮件帐号 * @param pass {const char*} SMTP 邮件帐号密码 * @return {int} 0 表示成功(SMTP_CLIENT::smtp_code 表示返回码, * SMTP_CLIENT::buf 存储响应内容),不然表示出错,应该关闭链接对象 */ SMTP_API int smtp_auth(SMTP_CLIENT *client, const char *user, const char *pass); /** * 向 SMTP 服务器发送 MAIL FROM 命令 * @param client {SMTP_CLIENT*} SMTP 链接对象 * @param from {const char*} 发送者邮箱 * @return {int} 0 表示成功(SMTP_CLIENT::smtp_code 表示返回码, * SMTP_CLIENT::buf 存储响应内容),不然表示出错,应该关闭链接对象 */ SMTP_API int smtp_mail(SMTP_CLIENT *client, const char *from); /** * 向 SMTP 服务器发送 RCPT TO 命令 * @param client {SMTP_CLIENT*} SMTP 链接对象 * @param to {const char*} 接收者邮箱 * @return {int} 0 表示成功(SMTP_CLIENT::smtp_code 表示返回码, * SMTP_CLIENT::buf 存储响应内容),不然表示出错,应该关闭链接对象 */ SMTP_API int smtp_rcpt(SMTP_CLIENT *client, const char *to); /** * 向 SMTP 服务器发送 DATA 命令 * @param client {SMTP_CLIENT*} SMTP 链接对象 * @return {int} 0 表示成功(SMTP_CLIENT::smtp_code 表示返回码, * SMTP_CLIENT::buf 存储响应内容),不然表示出错,应该关闭链接对象 */ SMTP_API int smtp_data(SMTP_CLIENT *client); /** * 向 SMTP 服务器发送邮件体内容,能够循环调用本函数直至数据发送完毕 * @param client {SMTP_CLIENT*} SMTP 链接对象 * @param src {const char*} 遵照邮件 MIME 编码格式的邮件体内容 * @param len {size_t} src 数据长度 * @return {int} 0 表示成功(SMTP_CLIENT::smtp_code 表示返回码, * SMTP_CLIENT::buf 存储响应内容),不然表示出错,应该关闭链接对象 */ SMTP_API int smtp_send(SMTP_CLIENT *client, const char* src, size_t len); /** * 向 SMTP 服务器发送邮件体内容,能够循环调用本函数直至数据发送完毕 * @param client {SMTP_CLIENT*} SMTP 链接对象 * @param fmt {const char*} 格式字符串 * @param ... 变参 * @return {int} 0 表示成功(SMTP_CLIENT::smtp_code 表示返回码, * SMTP_CLIENT::buf 存储响应内容),不然表示出错,应该关闭链接对象 */ SMTP_API int smtp_printf(SMTP_CLIENT *client, const char* fmt, ...); /** * 发送完邮件内容后调用本函数告诉 SMTP 服务器邮件数据完毕 * @param client {SMTP_CLIENT*} SMTP 链接对象 * @return {int} 0 表示成功(SMTP_CLIENT::smtp_code 表示返回码, * SMTP_CLIENT::buf 存储响应内容),不然表示出错,应该关闭链接对象 */ SMTP_API int smtp_data_end(SMTP_CLIENT *client); /** * 向 SMTP 服务器发送指定件路径的邮件文件 * @param client {SMTP_CLIENT*} SMTP 链接对象 * @param filepath {const char*} 邮件文件路径 * @return {int} 0 表示成功(SMTP_CLIENT::smtp_code 表示返回码, * SMTP_CLIENT::buf 存储响应内容),不然表示出错,应该关闭链接对象 */ SMTP_API int smtp_send_file(SMTP_CLIENT *client, const char *filepath); /** * 向 SMTP 服务器发送给定文件流的邮件内容 * @param client {SMTP_CLIENT*} SMTP 链接对象 * @param int {ACL_VSTREAM*} 邮件文件输入流 * @return {int} 0 表示成功(SMTP_CLIENT::smtp_code 表示返回码, * SMTP_CLIENT::buf 存储响应内容),不然表示出错,应该关闭链接对象 */ SMTP_API int smtp_send_stream(SMTP_CLIENT *client, ACL_VSTREAM *in); /** * 向 SMTP 服务器发送退出(QUIT)命令 * @param client {SMTP_CLIENT*} SMTP 链接对象 * @return {int} 0 表示成功(SMTP_CLIENT::smtp_code 表示返回码, * SMTP_CLIENT::buf 存储响应内容),不然表示出错,应该关闭链接对象 */ SMTP_API int smtp_quit(SMTP_CLIENT *client); /** * 向 SMTP 服务器发送 NOOP 命令 * @param client {SMTP_CLIENT*} SMTP 链接对象 * @return {int} 0 表示成功(SMTP_CLIENT::smtp_code 表示返回码, * SMTP_CLIENT::buf 存储响应内容),不然表示出错,应该关闭链接对象 */ SMTP_API int smtp_noop(SMTP_CLIENT *client); /** * 向 SMTP 服务器发送 RSET 命令 * @param client {SMTP_CLIENT*} SMTP 链接对象 * @return {int} 0 表示成功(SMTP_CLIENT::smtp_code 表示返回码, * SMTP_CLIENT::buf 存储响应内容),不然表示出错,应该关闭链接对象 */ SMTP_API int smtp_rset(SMTP_CLIENT *client);
读者也许注意到每一个函数前都有一个前缀:SMTP_API,这个前缀当你使用 VC 编译应用程序且选择了动态连接库时才会用到,此时须要增长条件编译开关:SMTP_DLL,以告诉VC编译器须要导出函数接口;在使用VC的静态连接方式或在UNIX下编译时,SMTP_API 仅是一个空定义而已。
灵活使用这些函数接口,您即可以轻松实现一个邮件发送客户端,如今给一个具体的实例来(该实例在 samples/smp_client/ 目录下)说明函数接口的调用过程,以下:
/* smtp_client.cpp : 定义控制台应用程序的入口点 */ #include "lib_acl.h" #include "lib_protocol.h" #ifdef WIN32 #define snprintf _snprintf #endif static int smtp_sender(void) { SMTP_CLIENT* conn; char addr[128], line[256]; acl_printf("please enter smtp server addr: "); if (acl_gets_nonl(line, sizeof(line)) == NULL) { acl_puts("invalid smtp server addr"); return -1; } if (strchr(line, ':') == NULL) snprintf(addr, sizeof(addr), "%s:25", line); else snprintf(addr, sizeof(addr), line); /* 链接 SMTP 服务器 */ conn = smtp_open(addr, 60, 1024); if (conn == NULL) { acl_printf("connect %s error %s\r\n", addr, acl_last_serror()); return -1; } else acl_printf("connect smtpd(%s) ok\r\n", addr); /* 从 SMTP 服务器得到欢迎信息 */ if (smtp_get_banner(conn) != 0) { acl_puts("get banner from server error"); smtp_close(conn); return -1; } else acl_printf(">smtpd: %s\r\n", conn->buf); /* 向 SMTP 服务器发送 EHLO/HELO 命令 */ if (smtp_greet(conn, "localhost", 0) != 0) { acl_printf("send ehlo cmd error: %s\r\n", conn->buf); smtp_close(conn); return -1; } else acl_printf(">smtpd: %s\r\n", conn->buf); /* 用户是否须要进行 SMTP 身份认证 */ acl_printf("Do you want to auth login? n[y|n]: "); if (acl_gets_nonl(line, sizeof(line)) == NULL) { acl_puts("invalid input"); smtp_close(conn); return -1; } /* 对用户身份进行 SMTP 身份认证 */ else if (strcasecmp(line, "Y") == 0) { char user[128], pass[128]; acl_printf("Please input user account: "); if (acl_gets_nonl(user, sizeof(user)) == NULL) { acl_puts("input invalid"); smtp_close(conn); return -1; } acl_printf("Please input user password: "); if (acl_gets_nonl(pass, sizeof(pass)) == NULL) { acl_puts("input invalid"); smtp_close(conn); return -1; } /* 开始进行 SMTP 身份认证 */ if (smtp_auth(conn, user, pass) != 0) { acl_printf("smtp auth(%s, %s) error: %s, code: %d\r\n", user, pass, conn->buf, conn->smtp_code); smtp_close(conn); return -1; } else acl_printf(">smtpd: %s\r\n", conn->buf); } /* 得到发件人邮箱地址 */ acl_printf("please input sender's email: "); if (acl_gets_nonl(line, sizeof(line)) == NULL) { acl_puts("invalid sender's email"); smtp_close(conn); return -1; } /* 发送 MAIL FROM: 命令 */ if (smtp_mail(conn, line) != 0) { acl_printf("smtp send MAIL FROM %s error\r\n", line); smtp_close(conn); return -1; } else acl_printf(">smtpd: %s\r\n", conn->buf); /* 发送 RCPT TO: 命令 */ while (1) { acl_printf("please input mail recipients: "); if (acl_gets_nonl(line, sizeof(line)) == NULL) { acl_puts("invalid mail recipients"); smtp_close(conn); return -1; } /* 发送 RCPT TO: 命令 */ else if (smtp_rcpt(conn, line) != 0) { acl_printf("send RCPT TO: %s error: %s, code: %d\r\n", line, conn->buf, conn->smtp_code); smtp_close(conn); return -1; } else acl_printf(">smtpd: %s\r\n", conn->buf); acl_printf("Do you want to add another recipients? n[y|n]: "); if (acl_gets_nonl(line, sizeof(line)) == NULL) { acl_puts("input invalid"); smtp_close(conn); return -1; } else if (strcasecmp(line, "y") != 0) break; } /* 发送 DATA: 命令 */ if (smtp_data(conn) != 0) { acl_printf("send DATA error %s, code: %d\r\n", conn->buf, conn->smtp_code); smtp_close(conn); return -1; } else acl_printf(">smtpd: %s\r\n", conn->buf); /* 从终端接收用户的输入的邮件内容并发往 SMTP 服务器 */ acl_puts("Please enter the email data below, end with \\r\\n.\\r\\n"); while (1) { if (acl_gets_nonl(line, sizeof(line)) == NULL) { acl_puts("readline error"); smtp_close(conn); return -1; } if (strcmp(line, ".") == 0) break; if (smtp_printf(conn, "%s\r\n", line) != 0) { acl_printf("send data to smtpd error, data: %s\r\n", line); smtp_close(conn); return -1; } } /* 发送 \r\n.\r\n 表示邮件数据发送完毕 */ if (smtp_data_end(conn) != 0) { acl_printf("send . error: %s, code: %d\r\n", conn->buf, conn->smtp_code); smtp_close(conn); return -1; } else acl_printf(">smtpd: %s\r\n", conn->buf); /* 发送 QUIT 命令 */ if (smtp_quit(conn) != 0) { acl_printf("smtp QUIT error: %s\r\n", conn->buf); smtp_close(conn); return -1; } else acl_printf(">smtpd: %s\r\n", conn->buf); smtp_close(conn); return 0; } int main(void) { int ret; #ifdef WIN32 acl_init(); #endif while (1) { char line[128]; ret = smtp_sender(); if (ret == -1) break; acl_printf("Do you want to send another email? n[y|n]: "); if (acl_gets_nonl(line, sizeof(line)) == NULL) { acl_puts("invalid input"); break; } else if (strcasecmp(line, "y") != 0) break; } #ifdef WIN32 acl_vstream_printf("enter any key to exit\r\n"); acl_vstream_getc(ACL_VSTREAM_IN); #endif return ret; }
不管使用 VC2003 编译仍是在LINUX下用GCC编译,运行可执行程序,均会获得以下结果:
please enter smtp server addr: mail.inc365.com:25
connect smtpd(mail.inc365.com:25) ok
>smtpd: 220 inc365.com ESMTP mail for UNIX (mail1.0)
>smtpd: 250 inc365.com
Do you want to auth login? n[y|n]:
please input sender's email: zsxxsz@sina.com
>smtpd: 250 2.1.0 Ok
please input mail recipients: zhengshuxin@inc365.com
>smtpd: 250 2.1.5 Ok
Do you want to add another recipients? n[y|n]:
>smtpd: 354 End data with <CR><LF>.<CR><LF>
Please enter the email data below, end with \r\n.\r\n
subject: hello world
from: zsxxsz@sina.com
to: zhengshuxin@inc365.com
hello world
.
>smtpd: 250 2.0.0 Ok: queued
>smtpd: 221 2.0.0 Bye
acl 库下载: https://sourceforge.net/projects/acl/
acl svn 地址:svn://svn.code.sf.net/p/acl/code/