Click here to Skip to main content
15,919,178 members
Articles / Programming Languages / C#
Article

Advanced MIME Parser/Creator/Editor

Rate me:
Please Sign up or sign in to vote.
4.91/5 (66 votes)
5 Oct 2005 1M   7.7K   116   322
An advanced MIME parser/creator/editor application.

Introduction

This article provides an advanced MIME editor. You can use it for parsing email messages, changing messages and for creating new email messages. It supports creation of complex messages with nested MIME entities. For more info, see help file: LumiSoft.Net.Mime namespace.

Message examples

Simple message:

//--- Beginning of message
From: sender@domain.com
To: recipient@domain.com
Subject: Message subject.
Content-Type: text/plain

Message body text. Bla blaa
blaa,blaa.
//--- End of message

In simple message, the MainEntity is the whole message.

Message with attachments:

//--- Beginning of message
From: sender@domain.com
To: recipient@domain.com
Subject: Message subject.
Content-Type: multipart/mixed; boundary="multipart_mixed"

--multipart_mixed    /* text entity */
Content-Type: text/plain

Message body text. Bla blaa
blaa,blaa.
--multipart_mixed    /* attachment entity */
Content-Type: application/octet-stream

attachment_data
--multipart_mixed--
//--- End of message

Here MainEntity is the multipart_mixed entity, and the text and attachment entities are child entities of MainEntity.

Using the code

Parsing example:

C#
Mime m = Mime.Parse("message.eml");
// Do your stuff with mime

Creating a new simple message:

C#
Mime m = new Mime();
MimeEntity mainEntity = m.MainEntity;
// Force to create From: header field
mainEntity.From = new AddressList();
mainEntity.From.Add(new MailboxAddress("dispaly name","user@domain.com"));
// Force to create To: header field
mainEntity.To = new AddressList();
mainEntity.To.Add(new MailboxAddress("dispaly name","user@domain.com"));
mainEntity.Subject = "subject";
mainEntity.ContentType = MediaType_enum.Text_plain;
mainEntity.ContentTransferEncoding = ContentTransferEncoding_enum.QuotedPrintable;
mainEntity.DataText = "Message body text.";

m.ToFile("message.eml");

Creating a message with text and attachments:

C#
Mime m = new Mime();
MimeEntity mainEntity = m.MainEntity;
// Force to create From: header field
mainEntity.From = new AddressList();
mainEntity.From.Add(new MailboxAddress("dispaly name","user@domain.com"));
// Force to create To: header field
mainEntity.To = new AddressList();
mainEntity.To.Add(new MailboxAddress("dispaly name","user@domain.com"));
mainEntity.Subject = "subject";
mainEntity.ContentType = MediaType_enum.Multipart_mixed;

MimeEntity textEntity = mainEntity.ChildEntities.Add();
textEntity.ContentType = MediaType_enum.Text_plain;
textEntity.ContentTransferEncoding = ContentTransferEncoding_enum.QuotedPrintable;
textEntity.DataText = "Message body text.";

MimeEntity attachmentEntity = mainEntity.ChildEntities.Add();
attachmentEntity.ContentType = MediaType_enum.Application_octet_stream;
attachmentEntity.ContentDisposition = ContentDisposition_enum.Attachment;
attachmentEntity.ContentTransferEncoding = ContentTransferEncoding_enum.Base64;
attachmentEntity.ContentDisposition_FileName = "yourfile.xxx";
attachmentEntity.DataFromFile("yourfile.xxx");
// or
attachmentEntity.Data = your_attachment_data;

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


Written By
Estonia Estonia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionMime Message Body parser for windows mobile 6 Pin
ChincoCodus3-Nov-10 0:10
ChincoCodus3-Nov-10 0:10 
AnswerRe: Mime Message Body parser for windows mobile 6 Pin
Ivar Lumi3-Nov-10 1:07
Ivar Lumi3-Nov-10 1:07 
GeneralLicense question Pin
Yaron Naveh25-Oct-10 8:17
Yaron Naveh25-Oct-10 8:17 
GeneralRe: License question Pin
Ivar Lumi25-Oct-10 19:12
Ivar Lumi25-Oct-10 19:12 
GeneralRe: License question Pin
Yaron Naveh25-Oct-10 23:26
Yaron Naveh25-Oct-10 23:26 
Questionhow to set rtf data to a mime message body Pin
ravi malik10-May-10 20:40
ravi malik10-May-10 20:40 
AnswerRe: how to set rtf data to a mime message body Pin
Ivar Lumi10-May-10 20:53
Ivar Lumi10-May-10 20:53 
QuestionCreating a Signed Email Pin
aschreiber18-Mar-10 8:01
aschreiber18-Mar-10 8:01 
First of all let me say BRAVO! WOW! WOW! WOW!

You have provided a wonderful service to the developer community.
You have done something that no one else has done.
You have done it in native C#.
You have made it free.
You have even provided the source.

Thank you so, so, so much.

The world needs more people like you. If you have a PayPal account I will gladly make a contribution.

Now that my praise is done I have a few questions.

1. What would the code look like for a signed email? I found two postings regarding this topic but the last post remained unanswered.

Ideally I am looking for a full example of a message that is signed and encrypted using AES 256 CBC. There are specific requirements for the formatting of the message due to legacy constraints and I can't use multipart/smime, rather I have to use application/x-pkcs7-mime (I would think adding an additional static string property to the MIME_MediaTypes.Application class called x_pkcs7_mime would do the trick). I have been able to sign and encrypt using native .NET code but not with AES 256 CBC because .NET uses COM Interop to interface with the CryptoAPI that ships with Windows and no versions of Windows prior to Vista support AES 256 CBC. Windows Vista, Win2k8, Win7 use the CNG encryption library which replaces the older CryptoAPI. That being said, if you write code in VS and reference the AES 256 bit algorithm it will compile regardless of whether you are programming on Windows XP or Vista but it will only run on Vista, W2k8, Win7 etc. If you try to run the executable on any OS prior to Vista you get a runtime error telling you that the algorithm specified does not exist.

Anyhow, the issue I have been having (and I assume many other developers have as well) is one that you have gone to great lengths in solving, which is easily creating, reading, parsing, manipulating etc. MIME entities. I am also trying to read in body text that may or may not have already been signed/encrypted. How would you approach that type of scenario? Since haven't provided signing and encryption functionality (yet???) I have to do that first and then read in the already signed/encrypted bytes. Unfortunately, if I've already done the signing and encrypting then won't the data with various nested MimeParts that have already been encoded impossible?

2. I see that you have method stubs for signing and validating a signature but no code. Do you plan on adding methods any time soon? This would be fantastic! I would be willing to help you in your quest even if it meant writing all of the documentation, comments, spell check etc.

I know this is alot to ask so whatever help you can provide is greatly appreciated. Regards.
AnswerRe: Creating a Signed Email Pin
Ivar Lumi18-Mar-10 8:29
Ivar Lumi18-Mar-10 8:29 
GeneralRe: Creating a Signed Email Pin
aschreiber18-Mar-10 14:05
aschreiber18-Mar-10 14:05 
GeneralRe: Creating a Signed Email [modified] Pin
Ivar Lumi18-Mar-10 20:12
Ivar Lumi18-Mar-10 20:12 
GeneralMimeEntity.ParentEntity always null Pin
kamiwa5-Jan-10 2:11
kamiwa5-Jan-10 2:11 
GeneralRe: MimeEntity.ParentEntity always null Pin
Ivar Lumi5-Jan-10 2:34
Ivar Lumi5-Jan-10 2:34 
GeneralLatest version of Lumisoft.Net for imap client testing Pin
Mikla7829-Nov-09 10:22
Mikla7829-Nov-09 10:22 
GeneralRe: Latest version of Lumisoft.Net for imap client testing Pin
Ivar Lumi29-Nov-09 20:59
Ivar Lumi29-Nov-09 20:59 
GeneralRe: Latest version of Lumisoft.Net for imap client testing Pin
Mikla7830-Nov-09 9:18
Mikla7830-Nov-09 9:18 
GeneralThis is a multi-part message in MIME format. Pin
Eric Johannsen24-Oct-09 20:28
Eric Johannsen24-Oct-09 20:28 
GeneralRe: This is a multi-part message in MIME format. Pin
Ivar Lumi24-Oct-09 21:54
Ivar Lumi24-Oct-09 21:54 
GeneralRe: This is a multi-part message in MIME format. [modified] Pin
Eric Johannsen25-Oct-09 9:18
Eric Johannsen25-Oct-09 9:18 
GeneralRe: This is a multi-part message in MIME format. Pin
Ivar Lumi26-Oct-09 4:02
Ivar Lumi26-Oct-09 4:02 
GeneralMail Server Pin
sibi jacob Abraham14-Oct-09 21:22
sibi jacob Abraham14-Oct-09 21:22 
Questionhow to save attachment from mail to disk Pin
ano&gete3-Oct-09 2:23
ano&gete3-Oct-09 2:23 
AnswerRe: how to save attachment from mail to disk Pin
Ivar Lumi3-Oct-09 19:53
Ivar Lumi3-Oct-09 19:53 
GeneralRe: how to save attachment from mail to disk Pin
RajdipS29-Apr-10 3:01
RajdipS29-Apr-10 3:01 
GeneralRe: how to save attachment from mail to disk Pin
Ivar Lumi29-Apr-10 3:37
Ivar Lumi29-Apr-10 3:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.