Click here to Skip to main content
15,919,774 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

 
AnswerRe: How do I detect email with attachment as external file? Pin
Ivar Lumi26-Sep-08 20:03
Ivar Lumi26-Sep-08 20:03 
GeneralRe: How do I detect email with attachment as external file? Pin
Member 319672027-Sep-08 7:29
Member 319672027-Sep-08 7:29 
GeneralRe: How do I detect email with attachment as external file? Pin
Ivar Lumi27-Sep-08 21:16
Ivar Lumi27-Sep-08 21:16 
GeneralRe: How do I detect email with attachment as external file? Pin
Member 319672028-Sep-08 11:56
Member 319672028-Sep-08 11:56 
GeneralRe: How do I detect email with attachment as external file? Pin
Ivar Lumi28-Sep-08 19:12
Ivar Lumi28-Sep-08 19:12 
GeneralRe: How do I detect email with attachment as external file? Pin
Member 319672029-Sep-08 8:53
Member 319672029-Sep-08 8:53 
GeneralRe: How do I detect email with attachment as external file? Pin
Ivar Lumi29-Sep-08 19:46
Ivar Lumi29-Sep-08 19:46 
GeneralRe: How do I detect email with attachment as external file? Pin
Member 319672030-Sep-08 5:07
Member 319672030-Sep-08 5:07 
Here we go. It's a complete message that includes references to three external attachments (image01, 02, and 03.jpg)

Received: by 10.114.169.7 with HTTP; Mon, 29 Sep 2008 10:39:57 -0700 (PDT)
Message-ID: <81bce08b0809291039nd0dd0c1q57929d604570695b@mail.gmail.com>
Date: Mon, 29 Sep 2008 13:39:57 -0400
From: "RnD TestCFT1" <rndtestcft1@gmail.com>
To: rndtestcft1@gmail.com
Subject: Fwd: test with 3 image attachments
In-Reply-To: <81bce08b0809291037h49db0e81n4c11499ac1ece3df@mail.gmail.com>
MIME-Version: 1.0
Content-Type: multipart/mixed; 
	boundary="----=_Part_17040_19300988.1222709997546"
References: <81bce08b0809291037h49db0e81n4c11499ac1ece3df@mail.gmail.com>
Delivered-To: rndtestcft1@gmail.com
X-Priority: 3 (Normal)
Status: RO
X-Folder: imap.gmail.com - Inbox

------=_Part_17040_19300988.1222709997546
Content-Type: multipart/alternative; 
	boundary="----=_Part_17041_19719934.1222709997546"


------=_Part_17041_19719934.1222709997546
Content-Type: text/plain; charset=KOI8-R
Content-Transfer-Encoding: base64
Content-Disposition: inline

Rm9yd2FyZGluZyAqb3JpZ2luYWwqICptZXNzYWdlKgoKLS0tLS0tLS0tLSBGb3J3YXJkZWQgbWVz
c2FnZSAtLS0tLS0tLS0tCkZyb206IFJuRCBUZXN0Q0ZUMSA8cm5kdGVzdGNmdDFAZ21haWwuY29t
PgpEYXRlOiAyMDA4LzkvMjkKU3ViamVjdDogdGVzdCB3aXRoIDMgaW1hZ2UgYXR0YWNobWVudHMK
VG86IHJuZHRlc3RjZnQxQGdtYWlsLmNvbQoKCiBKdXN0IHNvbWUgZm9ybWF0dGVkIHRleHQgaW4g
bWFpbiBib2R5CgoqQk9MRCBURVhUKgoKKklUQUxJQyBURVhUKgoKKkJPTEQgSVRBTElDIFRFWFQq
Cgr05evz9Ao=

------=_Part_17041_19719934.1222709997546
Content-Type: text/html; charset=KOI8-R
Content-Transfer-Encoding: base64
Content-Disposition: inline

PGRpdiBkaXI9Imx0ciI+Rm9yd2FyZGluZyA8Z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r8/Q8L2Rpdj48L2Rpdj48
L2Rpdj48YnI+PC9kaXY+Cg==

------=_Part_17041_19719934.1222709997546--

------=_Part_17040_19300988.1222709997546
Content-Type: message/external-body;
	access-type=local-file;
	name=Attach\image01.jpg

Content-Type: image/jpeg; name=image01.jpg
Content-Transfer-Encoding: base64
X-Attachment-Id: f_flpdt5pm0
Content-Disposition: attachment; filename=image01.jpg

------=_Part_17040_19300988.1222709997546
Content-Type: message/external-body;
	access-type=local-file;
	name=Attach\image02.jpg

Content-Type: image/jpeg; name=image02.jpg
Content-Transfer-Encoding: base64
X-Attachment-Id: f_flpdtcst1
Content-Disposition: attachment; filename=image02.jpg

------=_Part_17040_19300988.1222709997546
Content-Type: message/external-body;
	access-type=local-file;
	name=Attach\image03.jpg

Content-Type: image/jpeg; name=image03.jpg
Content-Transfer-Encoding: base64
X-Attachment-Id: f_flpdtk602
Content-Disposition: attachment; filename=image03.jpg

------=_Part_17040_19300988.1222709997546--


When I ingest this emails as follows
Mime m = Mime.Parse("message.eml");


I get
m.MimeEntries
of length 5. The last entry corresponds to image01.jpg and the other two appear to be missing. Thanks.
GeneralRe: How do I detect email with attachment as external file? Pin
Ivar Lumi30-Sep-08 19:40
Ivar Lumi30-Sep-08 19:40 
GeneralRe: How do I detect email with attachment as external file? Pin
Member 31967201-Oct-08 6:22
Member 31967201-Oct-08 6:22 
GeneralRe: How do I detect email with attachment as external file? Pin
Ivar Lumi2-Oct-08 6:25
Ivar Lumi2-Oct-08 6:25 
GeneralRe: How do I detect email with attachment as external file? Pin
Member 31967206-Oct-08 5:16
Member 31967206-Oct-08 5:16 
GeneralRe: How do I detect email with attachment as external file? Pin
Ivar Lumi6-Oct-08 22:19
Ivar Lumi6-Oct-08 22:19 
GeneralRe: How do I detect email with attachment as external file? Pin
Member 31967209-Oct-08 10:45
Member 31967209-Oct-08 10:45 
GeneralRe: How do I detect email with attachment as external file? Pin
Ivar Lumi6-Oct-08 6:22
Ivar Lumi6-Oct-08 6:22 
GeneralRe: How do I detect email with attachment as external file? Pin
Member 319672024-Oct-08 7:21
Member 319672024-Oct-08 7:21 
GeneralGet email from gmail Pin
ngohieutp26-Aug-08 0:18
ngohieutp26-Aug-08 0:18 
GeneralRe: Get email from gmail Pin
Ivar Lumi26-Aug-08 1:00
Ivar Lumi26-Aug-08 1:00 
GeneralRe: Get email from gmail Pin
ngohieutp26-Aug-08 6:56
ngohieutp26-Aug-08 6:56 
GeneralLittle bug in AddressList Pin
VanoXXX11-Aug-08 23:20
VanoXXX11-Aug-08 23:20 
GeneralRe: Little bug in AddressList Pin
Ivar Lumi12-Aug-08 5:42
Ivar Lumi12-Aug-08 5:42 
GeneralRe: Little bug in AddressList Pin
VanoXXX14-Aug-08 3:57
VanoXXX14-Aug-08 3:57 
GeneralRe: Little bug in AddressList Pin
Ivar Lumi14-Aug-08 4:08
Ivar Lumi14-Aug-08 4:08 
GeneralRe: Little bug in AddressList Pin
VanoXXX14-Aug-08 6:56
VanoXXX14-Aug-08 6:56 
GeneralRe: Little bug in AddressList Pin
Ivar Lumi14-Aug-08 20:32
Ivar Lumi14-Aug-08 20:32 

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.