c语言的smtp协议和它的mime扩展

一说写代码发邮件,好多人第一反应就是找开源库。其实看下smtp协议还是挺简单的,不简单哪对得起那个s呐。以我个人的理解这里主要有以下几个基本命令:

helo hostname交换,对付垃圾邮件时dns反查会用到
ehlo 比helo返回信息多些,如支持的登录认证方式、最大邮件尺寸等等
auth 请求服务器认证,参数为认证方式。现在一般的邮件服务器都要求认证后才能发信,如auth login后要依次发送username、passwd的base64字符串到服务器,完成login认证
mail from: 提交发件人地址,要求与auth的username对应
rcpt to: 提交收件人地址
data 开始邮件内容
\r\n.\r\n 结束data输入
quit 退出与服务器的会话

有了这些就可以发送普通基本的文本邮件了。
当然,对于更丰富的内容这点东西就显得有些单薄了。
这时就需要MIME(Multipurpose Internet Mail Extensions 多功能Internet 邮件扩充服务)的扩展支持了。本着以实践应用为目的指导思想来说,这里需要的mime就是遵循mime的标准,把电子邮件切成多个部分,分别表示文本内容和二进制内容(附件)。至于具体怎么切呢,主要有下面几个基本字段需要说明:

MIME-Version mime版本
Content-Type 内容数据类型
Content-Transfer-Encoding 数据编码方式,如7bit,8bit,base64等等
Content-Disposition 这里决定是否在行内显示附件或作为单独的附件
boundary 定义内容边界,一个唯一字符串,使用时要在前面加两个横杠

嗯,有了这些,就可以按照如下格式构建稍微不那么简单点的smtp会话了

#include <iostream>
#include "tns.h"
#include <unistd.h>
#include <string.h>
#include <stdio.h>

#define PORT 25
#define HOST "smtp.sina.com"

const char *msg[]={
		"ehlo mailer.com\r\n",//与服务器交互hostname,用作垃圾邮件过滤
		"auth login\r\n",//请求login方式登陆认证
		"dGVzdGFhYXd3\r\n",//用户名的base64
		"cGFzc3dkMTIz\r\n",//密码的base64
		"mail from:testaaaww@sina.com\r\n",//发件人,一般服务器限制与用户名对应,
		"rcpt to:testaaaww@sina.com\r\n",//收件人
		"data\r\n",//内容开始
		"subject:title long long long\r\n"//邮件标题
		"from:testaaaww@sina.com\r\n"//发件人
		"to:testaaaww@sina.com\r\n"//收件人
		"MIME-Version: 1.0\r\n"
		"Content-Type: multipart/mixed;\r\n"
		"\tboundary=\"this_is_a_unique_string\";\r\n"//定义内容边界,任意唯一字符串即可
		"Content-Transfer-Encoding: 8bit\r\n"
		"\r\n"
		"This is a multi-part message in MIME format.\r\n"
		"\r\n"
		"--this_is_a_unique_string\r\n"//开始文本内容
		"Content-Type: text/plain; charset=us-ascii\r\n"
		"Content-Transfer-Encoding: 8bit\r\n"
		"\r\n"
		"this is a pure text content\r\n"
		"\r\n"
		"--this_is_a_unique_string\r\n"//开始附件img.jpg
		"Content-Type: image/jpg;\r\n"
		"\tname=\"img.jpg\";\r\n"
		"Content-Transfer-Encoding: base64\r\n"
		"Content-Disposition: attachment; filename=\"img.jpg\"\r\n"//它是attachment:img.jpg
		"\r\n"//图片内容的base64
		"/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMFBwYHBwcGBwcICQsJCAgKCAcHCg0KCgsMDAwMBwkODw0MDgsMDAz/2wBDAQICAgMDAwYDAwYMCAcIDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAz/wAARCAAoAC8DASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8Rc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MYrEma/Yr9nH9nzwr+yj8CvCvw48EaculeFfB2nx6bp1uDlgiDl3bA3SOxZ3c8s7sx5JoA+HP8Aght/wVp+EX7Umkat8JIfhzo/7O/xn8OXU1z4i8AJpsWkxX16T/pV1axhUZ2LjMkciiaPgHeoDn9GK+HP+CpH/BA34N/8FPNei8Y30ur/AA8+LNhFGll4z8PMEuWMX+p+0wnCzhMDDApKAqgShQBXhf8AwbP/ALSHxW8ReO/2rfgN8UvH2s/FGf8AZ38bx6JpvibWLiWe+vUafUbSRWaV3cR7tNEiKzuV8913EAUAeW/8HY3xJsfgF+1X+wZ8TPEWk6pqvgz4f+Ob/WNcjs7ZZ2mhgvNDuWgUSMsTSyRW8+xHdQ21uQAxGVc/Hn9sL/g5I1qbTvhXHq/7MP7KLytDdeJ7vcuteKIclGVDGytMCNwMUDrAvzrJPIQq0UUAfqH/AME3v+CdXw//AOCYH7Mem/DH4fRXc1nDO99qWqXxRr3Wr2TAe4mKhVztVEVQMKkaLzgk+90UUAFfjh+xT4X8Y/8ABMv/AIOb/jD8M7jTL7WvAH7X1veePNO1VLcEWlzE15fkyOMhI4ZZdQtimcsZ7RzjcASigD//2Q==\r\n"
		"\r\n"
		"--this_is_a_unique_string\r\n"//内容分隔结束
		".\r\n",//data内容结束
		"quit\r\n",//断开链接
	};

int main(int argc,char** argv)
{
	FZsocket conn;
	char buf[128];
	int ret;
	if(!conn.Open(HOST,PORT))
	{
		for(int i=0;i<sizeof(msg)/sizeof(msg[0]);i++)
		{
			ret = conn.Recv((void*)buf,sizeof(buf));
			printf("%.*s\n",ret,buf);
			printf("%s",msg[i]);
			conn.Send((void*)msg[i],strlen(msg[i]));
		}
		ret = conn.Recv((void*)buf,sizeof(buf));
		printf("%.*s\n",ret,buf);
		conn.Shutdown();
	}
	return 0;
}

执行程序

[cz@myhost mail]$ ./main
220 irxd5-202.sinamail.sina.com.cn ESMTP
ehlo mailer.com
250-irxd5-202.sinamail.sina.com.cn
250-8BITMIME
250-SIZE 83886080
250-AUTH PLAIN LOGIN
250 AUTH=PLAIN LOGIN
auth login
334 VXNlcm5hbWU6
dGVzdGFhYXd3
334 UGFzc3dvcmQ6
cGFzc3dkMTIz
235 #2.0.0 OK Authenticated
mail from:testaaaww@sina.com
250 sender ok
rcpt to:testaaaww@sina.com
250 recipient ok
data
354 go ahead
subject:title long long long
from:testaaaww@sina.com
to:testaaaww@sina.com
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary=”this_is_a_unique_string”;
Content-Transfer-Encoding: 8bit

This is a multi-part message in MIME format.

–this_is_a_unique_string
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 8bit

this is a pure text content

–this_is_a_unique_string
Content-Type: image/jpg;
name=”img.jpg”;
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=”img.jpg”

/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMFBwYHBwcGBwcICQsJCAgKCAcHCg0KCgsMDAwMBwkODw0MDgsMDAz/2wBDAQICAgMDAwYDAwYMCAcIDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAz/wAARCAAoAC8DASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSM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EUJBxJH5kZ8mJTmd4/MYrEma/Yr9nH9nzwr+yj8CvCvw48EaculeFfB2nx6bp1uDlgiDl3bA3SOxZ3c8s7sx5JoA+HP8Aght/wVp+EX7Umkat8JIfhzo/7O/xn8OXU1z4i8AJpsWkxX16T/pV1axhUZ2LjMkciiaPgHeoDn9GK+HP+CpH/BA34N/8FPNei8Y30ur/AA8+LNhFGll4z8PMEuWMX+p+0wnCzhMDDApKAqgShQBXhf8AwbP/ALSHxW8ReO/2rfgN8UvH2s/FGf8AZ38bx6JpvibWLiWe+vUafUbSRWaV3cR7tNEiKzuV8913EAUAeW/8HY3xJsfgF+1X+wZ8TPEWk6pqvgz4f+Ob/WNcjs7ZZ2mhgvNDuWgUSMsTSyRW8+xHdQ21uQAxGVc/Hn9sL/g5I1qbTvhXHq/7MP7KLytDdeJ7vcuteKIclGVDGytMCNwMUDrAvzrJPIQq0UUAfqH/AME3v+CdXw//AOCYH7Mem/DH4fRXc1nDO99qWqXxRr3Wr2TAe4mKhVztVEVQMKkaLzgk+90UUAFfjh+xT4X8Y/8ABMv/AIOb/jD8M7jTL7WvAH7X1veePNO1VLcEWlzE15fkyOMhI4ZZdQtimcsZ7RzjcASigD//2Q==

–this_is_a_unique_string
.
250 ok: Message 196395725 accepted
quit
221 irxd5-202.sinamail.sina.com.cn

收到邮件

那些其他的指令和标准在这里就不再详述,因为我想那也不是大多数人感兴趣的东西。这些就够玩的了。^-^

发表评论?

0 条评论。

发表评论


请输入正确的验证码