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

 
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 
Ivar,

I tested the code on a new set of EMLs, some with external attachments. On some of these it worked fine, i.e. had the backslash in the attachment's path (e.g. "Attach\DSC00499.JPG") while on other the backslash was missing. Below I am including one of those bad ones.

NOTE: The attached message contains non-Latin text (Arabic, actually)

OBSERVATION: I am wondering if the problem has something to do with spaces in the attachment's name, e.g. "Attach\Arabic Text2.JPG" below

Thanks!

Delivered-To: mounirsalah@gmail.com
Received: by 10.180.255.7 with SMTP id c7cs213048bki;
        Tue, 14 Oct 2008 06:09:43 -0700 (PDT)
Received: by 10.141.193.1 with SMTP id v1mr4801716rvp.73.1223989772865;
        Tue, 14 Oct 2008 06:09:32 -0700 (PDT)
Received: by 10.140.185.16 with HTTP; Tue, 14 Oct 2008 06:09:32 -0700 (PDT)
Message-ID: <330636b40810140609qa644df7iaa1d684a533801e3@mail.gmail.com>
Date: Tue, 14 Oct 2008 09:09:32 -0400
From: "Samir Ali" <samirali21723@gmail.com>
To: "Mounir Salah" <mounirsalah@gmail.com>
Subject: [Aid4Mail Trial tag #1] Arabic Text2(MS Word+JPG attach)
MIME-Version: 1.0
Content-Type: multipart/mixed; 
	boundary="----=_Part_3306_25190320.1223989772851"
X-Priority: 3 (Normal)
Status: RO
X-Folder: Arabic

------=_Part_3306_25190320.1223989772851
Content-Type: multipart/alternative; 
	boundary="----=_Part_3307_10144069.1223989772851"


------=_Part_3307_10144069.1223989772851
Content-Type: text/plain; charset=WINDOWS-1256
Content-Transfer-Encoding: base64
Content-Disposition: inline

4e3TIM/dx9rHINrkIOTm0e0gx+Hjx+Hf7SDf48cg08jeIObH5CDI7eTKIObh3+TlIOHhx9PdIO3K
2tHWIMfh7CDN4+HJIMrU5e3RCsfayt7PIMfk5ccg2cfh48kg5trH0e3JINrkIMfh1c3JIObK3sre
zyDH4ewgx+Hj1c/H3u3JIObl7SDh7dPKIMzP7c/JIObTyN4K4eHHziDH4drS7dIgzdPt5CDfx9nj
IMfkIMzHweTHIMjR5sfI2CDKyO3kIMrm0dggx83jzyDHyOQgx+Hjx+Hf7SDd7SDe1sftxwrd08fP
IMfO0ewg3e0g49/KyCDmx+HP5b+/vyDa4ewgx+Ha4+bjIMfkIOPVz8fe7ckg5dDHIMfhzsjRINba
7d3JIMzPxyDmx+QKx+Ha5ObH5CDH4ePQ3+bRIO3j3+Qgx+Qg7cPK7SDI5SDH7SDH5NPH5CDt2u3U
IN3tIMjR7djH5O3HIObt49/kIMfkIO3k2OHtINrh7ArH4cja1iDj5MeFLr+/v78K

------=_Part_3307_10144069.1223989772851
Content-Type: text/html; charset=WINDOWS-1256
Content-Transfer-Encoding: base64
Content-Disposition: inline

PGRpdiBkaXI9Imx0ciI+PGRpdj4mbmJzcDs8L2Rpdj4KPGRpdj4mbmJzcDs8L2Rpdj4KPGRpdj4K
PHAgY2xhc3M9Ik1zb05vcm1hbCIgc3R5bGU9Ik1BUkdJTjogMGluIDBpbiAwcHQ7IFRFWFQtQUxJ
R046IHJpZ2h0IiBhbGlnbj0icmlnaHQiPjxzcGFuIGxhbmc9IkFSLUVHIiBkaXI9InJ0bCIgc3R5
bGU9Im1zby1iaWRpLWxhbmd1YWdlOiBBUi1FRyI+PGZvbnQgc2l6ZT0iMyI+PGZvbnQgZmFjZT0i
VGltZXMgTmV3IFJvbWFuIj7h7dMgz93H2scg2uQg5ObR7SDH4ePH4d/tIN/jxyDTyN4g5sfkIMjt
5Mog5uHf5OUg4eHH090g7cra0dYgx+HsIM3j4ckgytTl7dEgx9rK3s8gx+TlxyDZx+HjySDm2sfR
7ckg2uQgx+HVzckg5sreyt7PIMfh7CDH4ePVz8fe7ckg5uXtIOHt08ogzM/tz8kg5tPI3iDh4cfO
IMfh2tLt0iDN0+3kIN/H2eMgx+QgzMfB5McgyNHmx8jYIMrI7eQgyubR2CDHzePPIMfI5CDH4ePH
4d/tIN3tIN7Wx+3HIN3Tx88gx87R7CDd7SDj38rIIObH4c/lv7+/INrh7CDH4drj5uMgx+Qg49XP
x97tySDl0Mcgx+HOyNEg1trt3ckgzM/HIObH5CDH4drk5sfkIMfh49Df5tEg7ePf5CDH5CDtw8rt
IMjlIMftIMfk08fkIO3a7dQg3e0gyNHt2Mfk7ccg5u3j3+Qgx+Qg7eTY4e0g2uHsIMfhyNrWIOPk
x4Uuv7+/vzwvZm9udD48L2ZvbnQ+PC9zcGFuPjwvcD4KPC9kaXY+PC9kaXY+Cg==

------=_Part_3307_10144069.1223989772851--

------=_Part_3306_25190320.1223989772851
Content-Type: message/external-body;
	access-type=local-file;
	name="Attach\Arabic Text2.doc"

Content-Type: application/msword; name="Arabic Text2.doc"
Content-Transfer-Encoding: base64
X-Attachment-Id: f_fmajw0050
Content-Disposition: attachment; filename="Arabic Text2.doc"

------=_Part_3306_25190320.1223989772851
Content-Type: message/external-body;
	access-type=local-file;
	name="Attach\Arabic Text2.JPG"

Content-Type: image/jpeg; name="Arabic Text2.JPG"
Content-Transfer-Encoding: base64
X-Attachment-Id: f_fmajw8vm1
Content-Disposition: attachment; filename="Arabic Text2.JPG"

------=_Part_3306_25190320.1223989772851--

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 
GeneralEmbedded Image in Body, delete the attachments from an eml file Pin
gokul7829-Jul-08 12:47
gokul7829-Jul-08 12:47 
GeneralReference in VBA Pin
lfp200021-Jul-08 9:03
lfp200021-Jul-08 9:03 
GeneralRe: Reference in VBA Pin
Ivar Lumi21-Jul-08 9:08
Ivar Lumi21-Jul-08 9:08 
GeneralRe: Reference in VBA Pin
lfp200021-Jul-08 9:26
lfp200021-Jul-08 9:26 
GeneralRe: Reference in VBA Pin
Ivar Lumi21-Jul-08 10:06
Ivar Lumi21-Jul-08 10:06 
GeneralERR maildrop already locked Pin
ngohieutp20-Jul-08 18:39
ngohieutp20-Jul-08 18:39 
GeneralRe: ERR maildrop already locked Pin
Ivar Lumi20-Jul-08 19:55
Ivar Lumi20-Jul-08 19:55 
GeneralRe: ERR maildrop already locked [modified] Pin
ngohieutp20-Jul-08 21:05
ngohieutp20-Jul-08 21:05 

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.