Click here to Skip to main content
15,885,365 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 995K   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: What about an eml inside an eml? Pin
BroxCJ11-Aug-09 22:03
BroxCJ11-Aug-09 22:03 
GeneralChange from Singlepart to multipart and preserve attachment Pin
Bman2816-Jul-09 5:12
Bman2816-Jul-09 5:12 
GeneralRe: Change from Singlepart to multipart and preserve attachment Pin
Ivar Lumi16-Jul-09 7:12
Ivar Lumi16-Jul-09 7:12 
GeneralRe: Change from Singlepart to multipart and preserve attachment Pin
Bman2816-Jul-09 8:25
Bman2816-Jul-09 8:25 
GeneralRe: Change from Singlepart to multipart and preserve attachment Pin
Ivar Lumi16-Jul-09 20:29
Ivar Lumi16-Jul-09 20:29 
QuestionCould you please write a simple sample code that reads a eml file and load it on a MailMessage class of .net framework? Pin
evald8021-Jun-09 23:46
evald8021-Jun-09 23:46 
AnswerRe: Could you please write a simple sample code that reads a eml file and load it on a MailMessage class of .net framework? Pin
Ivar Lumi22-Jun-09 3:34
Ivar Lumi22-Jun-09 3:34 
QuestionLumisoft.Net failed to Parse Pin
DharmeshChauhan12-Jun-09 3:21
DharmeshChauhan12-Jun-09 3:21 
Following message store as a 1.email and trying to parse.

But intialization get error INVALID-FIELD MESSAGE-ID, This emails comes from X-user clients

so failed to parse for display on website have any solution. I think problem because space before
Message id how can I handle it.

================ Copy from above content in a file =====================
Message-ID: <200906121804578_11>
From: mmtclimited@hometown.net
To: mmtclimited@hometown.net
Subject: RENOVATION OF SHOP AT BHAWAN
Date: Fri, 12 Jun 2009 10:18:04
Mime-Version: 1.0
Content-Type: text/html; charset=iso-8859-1
Content-Transfer-Encoding: 8bit

<HTML>
<HEAD></HEAD>
<BODY><A href="http://www.mmtclimited.com/tender/tender_details.php?id_pk=1524">http://www.mmtclimited.com/tender/tender_details.php?id_pk=1524</A></BODY>
</HTML>
=================Copy End =====================
AnswerRe: Lumisoft.Net failed to Parse Pin
Ivar Lumi14-Jun-09 19:54
Ivar Lumi14-Jun-09 19:54 
GeneralThe latest code Pin
Sarif Bin Ishak12-Feb-09 17:42
Sarif Bin Ishak12-Feb-09 17:42 
GeneralRe: The latest code Pin
Ivar Lumi12-Feb-09 20:18
Ivar Lumi12-Feb-09 20:18 
GeneralRe: The latest code Pin
Sarif Bin Ishak12-Feb-09 22:41
Sarif Bin Ishak12-Feb-09 22:41 
GeneralRe: The latest code Pin
DuncanWhoCares9-Jun-09 6:54
DuncanWhoCares9-Jun-09 6:54 
QuestionSomething is wrong the way we attach the file Pin
Sarif Bin Ishak11-Feb-09 17:35
Sarif Bin Ishak11-Feb-09 17:35 
QuestionHow to attach JPG file and parse it Pin
Sarif Bin Ishak11-Feb-09 7:09
Sarif Bin Ishak11-Feb-09 7:09 
QuestionHow to convert "Mail_Message" to byte array Pin
Sarif Bin Ishak10-Feb-09 16:11
Sarif Bin Ishak10-Feb-09 16:11 
AnswerRe: How to convert "Mail_Message" to byte array Pin
Ivar Lumi11-Feb-09 0:19
Ivar Lumi11-Feb-09 0:19 
GeneralNew help page - step by step for the new lib Pin
Sarif Bin Ishak9-Feb-09 21:22
Sarif Bin Ishak9-Feb-09 21:22 
GeneralRe: New help page - step by step for the new lib Pin
Ivar Lumi9-Feb-09 21:57
Ivar Lumi9-Feb-09 21:57 
QuestionStatus of new development? Pin
3choBoomer23-Jan-09 9:42
3choBoomer23-Jan-09 9:42 
AnswerRe: Status of new development? Pin
Ivar Lumi23-Jan-09 20:32
Ivar Lumi23-Jan-09 20:32 
GeneralGreat work! Pin
JohanD29-Oct-08 11:03
JohanD29-Oct-08 11:03 
GeneralRe: Great work! Pin
Ivar Lumi30-Oct-08 5:20
Ivar Lumi30-Oct-08 5:20 
QuestionUsing the new or old code? Pin
richardwhitehead20-Oct-08 5:04
richardwhitehead20-Oct-08 5:04 
AnswerRe: Using the new or old code? Pin
Ivar Lumi20-Oct-08 18:58
Ivar Lumi20-Oct-08 18:58 

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.