65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 MimeSniffer 编/解码 MIME 内容

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.88/5 (23投票s)

2002年4月28日

2分钟阅读

viewsIcon

397397

downloadIcon

6987

符合 RFC 标准的 Mime 编/解码器

引言

The Code Project 提供了许多用于发送 (SMTP) 和检索 (POP3) 互联网邮件的辅助类。但是这些模块不会对您有所帮助,因为内容仍然是原始的,必须进行处理。内容采用 MIME 格式,在 RFC2045、RFC1521 和 RFC822 中有记载。如果您看一下这些 RFC,您就会知道为什么(免费)实现很少。

技术详情

MimeSniffer 是一个 VC++ ATL 项目。解析器已使用 lex 和 yacc (flex 和 bison) 实现。它带有一个简洁的 COM 对象(没有 GUI),实现了 IPersistStream。尽管 MimeSniffer 包含一个用于文件的流助手(用于演示目的),但流对象的实现由您自己决定(例如套接字)。 COM 对象提供了一个调度接口,以便 VB 开发人员可以使用它。

用法

首先,不要忘记使用 regsvr32.exe 注册 COM 服务器 MimeSniffer.dll - 它与两个演示一起提供。您可以创建、读取或修改 MIME 内容。接口非常简单,我只会在本文中提供 VBS 代码示例。下载演示项目以获取 VC++ 示例。

编码

这是创建电子邮件消息所需的最少代码 (VBScript) 示例。这非常简单。

rem -- Altough the Object's name is "Decoder", it's able to "encode"
rem -- mime content
set Dec = CreateObject("MimeSniffer.Decoder") rem -- supply minimal information Dec.From = "me@home.com" Dec.To = "friend@server.com" Dec.Subject = "Lunch" Dec.Body = "Hi! Let's do lunch....." rem -- "Send" the email to a stream Dec.SaveToFile "mail1.txt"

如果您随后查看文件 mail1.txt,您会看到 COM 对象正确处理了字符编码

Subject: =?iso-8859-1?Q?Lunch?=

该接口具有“原始”属性,用于访问此类字段,而无需进行编/解码。例如,使用属性 SubjectRaw

当今大多数邮件消息都是多部分邮件消息。例如,Outlook 通常会生成具有多部分正文的邮件消息,即使只有一个部分。下一个示例显示了如何创建包含两部分的多部分电子邮件 - 文本和作为附件的文件。

set Dec	= CreateObject("MimeSniffer.Decoder")

rem -- supply head information
Dec.From	= "me@home.com"
Dec.To		= "friend@server.com"
Dec.Subject	= "Test"

rem -- get root body object...
set Body	= Dec.Body

rem -- ...wich will consist of multiple parts
Body.MajorContentType	= "multipart"
Body.MinorContentType	= "mixed"

rem -- insert a new body object
set TextBody = Body.AddNew()

rem -- set header information for this part
TextBody.MajorContentType	= "text"
TextBody.MinorContentType	= "plain"
TextBody.Encoding		= "quoted-printable"

rem -- set the content
TextBody.Value = "This is the text part!"

rem -- insert a new body object
set PicBody = Body.AddNew()

rem -- set header information for this part
PicBody.MajorContentType	= "image"
PicBody.MinorContentType	= "jpeg"
PicBody.Encoding		= "Base64"

rem -- import the content
PicBody.ImportFromFile "duke.jpg", True

rem -- declare the second part as an attachment
PicBody.FileName = "duke.jpg"

rem -- "Send" the email to a stream
Dec.SaveToFile "mail2.txt"

解码

解码甚至比编码更容易。您所需要做的就是创建一个流对象,然后从该流中 IPersistStream::Load MimeSniffer 的持久化。然后访问/修改对象并重新发送内容。所以 MimeSniffer 可以充当一个过滤器。以下示例使用先前创建的文件 mail2.txt

set Dec	= CreateObject("MimeSniffer.Decoder")

rem -- IPersistStream::Load the content
Dec.LoadFromFile "mail2.txt"

rem -- throw the subject
MsgBox "Subject is: " & Dec.Subject

rem -- modify the subject
Dec.Subject = "Extended " & Dec.Subject

rem -- get root body object...
set Body	= Dec.Body

rem -- get the text part, wich is the first part
set TextBody = Body(0)

rem -- throw the content
MsgBox "Body-Text is: " & TextBody.Value

rem -- modify the text part
TextBody.Value = TextBody.Value & Chr(13) & Chr(10) & "Nice, isn't it?"

rem -- get the pic part, wich is the second part
set PicBody = Body(1)

rem -- extract the content to a stream
PicBody.ExportAsFile "copy of " & PicBody.FileName

rem -- "Send" the modified email to a stream
Dec.SaveToFile "mail3.txt"

还有更多属性,我认为它们非常简单。欢迎提出问题和评论。

更新历史

  • 2002 年 2 月 12 日 - 纠正了 yystype.cpp 中的“月枚举”错误
  • 2002 年 2 月 12 日 - 通过修改流实现提高了大型电子邮件的速度
  • 2002 年 3 月 2 日 - 使用 VS .NET 编译
  • 2002 年 3 月 2 日 - 删除了一个错误,其中嵌套的多部分得到了错误的边界标识符
© . All rights reserved.