Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

En/Decode MIME-Content with MimeSniffer

0.00/5 (No votes)
2 Dec 2002 2  
RFC-compliant Mime-En/Decoder

Introduction

The Code Project offers many helper-classes for sending (SMTP) and retrieving (POP3) internet mail. But these modules won't help you any further because the content is still raw and must be cooked. The content is in MIME format wich is documented in RFC2045, RFC1521 and RFC822. If you take a look at these RFCs you'll know why (free) implementations are rare.

Technical Details

The MimeSniffer is a VC++ ATL project. The parser has been realized with lex and yacc (flex and bison). It comes with a neat COM object (without a GUI), that implements IPersistStream. Although the MimeSniffer includes a stream-helper for files (for demonstration purposes), the implementation of the stream-object is left up to you (e.g. socket). The COM object provides a dispatch-interface so that VB developers could come along.

Usage

First of all, don't forget to register the COM server MimeSniffer.dll - which comes with both demos - using regsvr32.exe. You may create, read or modify MIME content. The interface is very easy and I will only provide VBS code samples in this article. Download the demo project for a VC++ sample.

Encoding

Here's a sample of the minimum amount of code (VBScript) that you must use to create an email message. It's very straight forward.

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"

If you take a look at the file mail1.txt afterward, you'll see that the COM object handles char-encoding properly:

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

The interface has "raw" properties to access this and similar fields without being en/decoded. E.g. use the property SubjectRaw.

Most mail messages today are multipart mail messages. E.g. Outlook generally generates mail messages with a multipart body even if there's only one part. The next sample shows how to create an multipart email with two parts - a text and a file as an attachment.

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"

Decoding

Decoding is even easier than encoding. All you need to do is to create a stream object and to IPersistStream::Load the MimeSniffer persistent from this stream. Then access/modify the objects and re-send the content. So MimeSniffer can act like a filter. The following sample uses the previously created file 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"

There are many more properties which are very straight forward I think. Questions and comments are welcome.

Update History

  • 12/02/2002 - Corrected the "month-enum" bug in yystype.cpp
  • 12/02/2002 - Enhanced speed for large emails by modifications in Stream-implementation
  • 02/03/2002 - Compiles with VS .NET
  • 02/03/2002 - Removed a bug, where nested Multiparts got wrong boundary-identifiers

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here