Click here to Skip to main content
14,271,606 members

Advanced MIME Parser/Creator/Editor

Rate this:
4.91 (65 votes)
Please Sign up or sign in to vote.
4.91 (65 votes)
5 Oct 2005
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:

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

Creating a new simple message:

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:

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

Share

About the Author

Ivar Lumi
Estonia Estonia
No Biography provided

Comments and Discussions

 
GeneralMimeEntity.ParentEntity always null Pin
kamiwa5-Jan-10 2:11
memberkamiwa5-Jan-10 2:11 
GeneralRe: MimeEntity.ParentEntity always null Pin
Ivar Lumi5-Jan-10 2:34
memberIvar Lumi5-Jan-10 2:34 
GeneralLatest version of Lumisoft.Net for imap client testing Pin
Mikla7829-Nov-09 10:22
memberMikla7829-Nov-09 10:22 
GeneralRe: Latest version of Lumisoft.Net for imap client testing Pin
Ivar Lumi29-Nov-09 20:59
memberIvar Lumi29-Nov-09 20:59 
GeneralRe: Latest version of Lumisoft.Net for imap client testing Pin
Mikla7830-Nov-09 9:18
memberMikla7830-Nov-09 9:18 
GeneralThis is a multi-part message in MIME format. Pin
Eric Johannsen24-Oct-09 20:28
memberEric Johannsen24-Oct-09 20:28 
GeneralRe: This is a multi-part message in MIME format. Pin
Ivar Lumi24-Oct-09 21:54
memberIvar Lumi24-Oct-09 21:54 
GeneralRe: This is a multi-part message in MIME format. [modified] Pin
Eric Johannsen25-Oct-09 9:18
memberEric Johannsen25-Oct-09 9:18 
After removing everything up to the first thing that looks like a correct header ("Content-Disposition: inline ..."), the email parses without throwing an Exception. However, there are no useful results in the Mime object returned by Mime.Parse.

BodyHtml and BodyText are both null.

MainEntry.Data and MainEntry.DataEncoded contain zero bytes (0x00).

Date is 1/1/0001.

MessageID, MimeVersion, ReplyTo, Sender, Subject, To are all null.

HeaderString contains the entire input message, including encoding like "= ", except that some CR LF have been inserted.

Header.Name = "Content-Disposition:";
Header.Value starts "inline Content-Transfer-Encoding: 7bit Content-Type: message/rfc822 >From My SenderName Oct 12...."

Somehow Content-Transfer-Encoding is not being recognized as a new header.

I'm sure it's something I'm doing wrong, or something that's happening to the email before it reaches me, but I'm lost as to what. By the way, I'm processing feedback loop messages from Yahoo and AOL... so I guess they create properly formatted emails.

modified on Sunday, October 25, 2009 3:47 PM

GeneralRe: This is a multi-part message in MIME format. Pin
Ivar Lumi26-Oct-09 4:02
memberIvar Lumi26-Oct-09 4:02 
GeneralMail Server Pin
sibi jacob Abraham14-Oct-09 21:22
membersibi jacob Abraham14-Oct-09 21:22 
Questionhow to save attachment from mail to disk Pin
ano&gete3-Oct-09 2:23
memberano&gete3-Oct-09 2:23 
AnswerRe: how to save attachment from mail to disk Pin
Ivar Lumi3-Oct-09 19:53
memberIvar Lumi3-Oct-09 19:53 
GeneralRe: how to save attachment from mail to disk Pin
RajdipS29-Apr-10 3:01
memberRajdipS29-Apr-10 3:01 
GeneralRe: how to save attachment from mail to disk Pin
Ivar Lumi29-Apr-10 3:37
memberIvar Lumi29-Apr-10 3:37 
GeneralRe: how to save attachment from mail to disk Pin
RajdipS29-Apr-10 5:09
memberRajdipS29-Apr-10 5:09 
GeneralRe: how to save attachment from mail to disk Pin
RajdipS29-Apr-10 4:03
memberRajdipS29-Apr-10 4:03 
GeneralParsing date error Pin
Elvandar1-Oct-09 22:26
memberElvandar1-Oct-09 22:26 
GeneralRe: Parsing date error Pin
Ivar Lumi1-Oct-09 22:51
memberIvar Lumi1-Oct-09 22:51 
NewsRe: Parsing date error [modified] Pin
Elvandar1-Oct-09 23:20
memberElvandar1-Oct-09 23:20 
GeneralRe: Parsing date error Pin
Ivar Lumi2-Oct-09 0:18
memberIvar Lumi2-Oct-09 0:18 
GeneralRe: Parsing date error Pin
Elvandar2-Oct-09 0:25
memberElvandar2-Oct-09 0:25 
Questionlatest version, mime parsing Pin
Member 274315628-Sep-09 23:21
memberMember 274315628-Sep-09 23:21 
AnswerRe: latest version, mime parsing Pin
Ivar Lumi28-Sep-09 23:51
memberIvar Lumi28-Sep-09 23:51 
GeneralNice job Pin
Steve Trefethen3-Sep-09 21:43
memberSteve Trefethen3-Sep-09 21:43 
GeneralRe: Nice job Pin
Ivar Lumi4-Sep-09 1:21
memberIvar Lumi4-Sep-09 1:21 

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.

Article
Posted 5 Oct 2005

Stats

839.1K views
7.4K downloads
116 bookmarked