Click here to Skip to main content
15,881,172 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 990.1K   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

 
QuestionProblem viewing From: email address Pin
chezy5252-Nov-07 7:26
chezy5252-Nov-07 7:26 
AnswerRe: Problem viewing From: email address Pin
Ivar Lumi2-Nov-07 7:32
Ivar Lumi2-Nov-07 7:32 
GeneralRe: Problem viewing From: email address Pin
chezy5252-Nov-07 8:35
chezy5252-Nov-07 8:35 
QuestionHaving trouble opening an email Pin
Richard__21-Sep-07 4:07
Richard__21-Sep-07 4:07 
AnswerRe: Having trouble opening an email Pin
Ivar Lumi23-Sep-07 6:17
Ivar Lumi23-Sep-07 6:17 
GeneralRe: Having trouble opening an email Pin
Richard__24-Sep-07 22:06
Richard__24-Sep-07 22:06 
GeneralRe: Having trouble opening an email Pin
Ivar Lumi25-Sep-07 19:40
Ivar Lumi25-Sep-07 19:40 
GeneralAwesome Pin
rodneyrodney20-Sep-07 20:13
rodneyrodney20-Sep-07 20:13 
Really excellent library - it's saved us heaps of time and effort and works great.

Thanks a lot.
GeneralRe: Awesome Pin
Ivar Lumi20-Sep-07 20:35
Ivar Lumi20-Sep-07 20:35 
QuestionHow To Use this class with SMTP Pin
stormbill6-Sep-07 23:49
stormbill6-Sep-07 23:49 
AnswerRe: How To Use this class with SMTP Pin
Ivar Lumi7-Sep-07 3:13
Ivar Lumi7-Sep-07 3:13 
QuestionHow do you deal with embedded attachments Pin
monsieur_jj23-Aug-07 20:10
monsieur_jj23-Aug-07 20:10 
AnswerRe: How do you deal with embedded attachments Pin
Ivar Lumi23-Aug-07 20:18
Ivar Lumi23-Aug-07 20:18 
QuestionUsing Environment.NewLine? Pin
Johann MacDonagh11-Aug-07 14:54
Johann MacDonagh11-Aug-07 14:54 
AnswerRe: Using Environment.NewLine? Pin
Ivar Lumi11-Aug-07 20:03
Ivar Lumi11-Aug-07 20:03 
GeneralBug fix: LumiSoft.Net.Core.CanonicalDecode Pin
EngBol3-Aug-07 12:44
EngBol3-Aug-07 12:44 
GeneralRe: Bug fix: LumiSoft.Net.Core.CanonicalDecode Pin
Ivar Lumi4-Aug-07 21:41
Ivar Lumi4-Aug-07 21:41 
GeneralThis code is the real deal Pin
thefullquid1-Aug-07 14:41
thefullquid1-Aug-07 14:41 
QuestionHow to save a .eml attachment Pin
Sravanthik24-Jul-07 20:31
Sravanthik24-Jul-07 20:31 
AnswerRe: How to save a .eml attachment Pin
Ivar Lumi24-Jul-07 22:18
Ivar Lumi24-Jul-07 22:18 
GeneralRe: How to save a .eml attachment Pin
Sravanthik25-Jul-07 0:54
Sravanthik25-Jul-07 0:54 
GeneralRe: How to save a .eml attachment Pin
Ivar Lumi25-Jul-07 1:44
Ivar Lumi25-Jul-07 1:44 
GeneralLicensing Agreement Pin
WashDad5-Jul-07 13:16
WashDad5-Jul-07 13:16 
GeneralRe: Licensing Agreement Pin
Ivar Lumi5-Jul-07 19:44
Ivar Lumi5-Jul-07 19:44 
GeneralRe: Licensing Agreement Pin
nelsonlam17-Nov-09 20:40
nelsonlam17-Nov-09 20:40 

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.