Click here to Skip to main content
14,765,424 members
Articles » Languages » C# » Utilities
Article
Posted 5 Oct 2005

Stats

871.6K views
7.6K downloads
116 bookmarked

Advanced MIME Parser/Creator/Editor

Rate me:
Please Sign up or sign in to vote.
4.91/5 (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

 
GeneralRe: Awesome Pin
Ivar Lumi20-Sep-07 21:35
MemberIvar Lumi20-Sep-07 21:35 
QuestionHow To Use this class with SMTP Pin
stormbill7-Sep-07 0:49
Memberstormbill7-Sep-07 0:49 
AnswerRe: How To Use this class with SMTP Pin
Ivar Lumi7-Sep-07 4:13
MemberIvar Lumi7-Sep-07 4:13 
QuestionHow do you deal with embedded attachments Pin
monsieur_jj23-Aug-07 21:10
Membermonsieur_jj23-Aug-07 21:10 
AnswerRe: How do you deal with embedded attachments Pin
Ivar Lumi23-Aug-07 21:18
MemberIvar Lumi23-Aug-07 21:18 
QuestionUsing Environment.NewLine? Pin
Johann MacDonagh11-Aug-07 15:54
MemberJohann MacDonagh11-Aug-07 15:54 
AnswerRe: Using Environment.NewLine? Pin
Ivar Lumi11-Aug-07 21:03
MemberIvar Lumi11-Aug-07 21:03 
GeneralBug fix: LumiSoft.Net.Core.CanonicalDecode Pin
EngBol3-Aug-07 13:44
MemberEngBol3-Aug-07 13:44 
In a nutshell, this has to do with header encodings in which there are multiple "encoded words" with space in between them.

For example, spaces in between encoded words are to be ignored. Say you have this subject line:
Subject: Hi =?UTF-8?Q?Re: =41?= =?UTF-8?Q?=41?=

... decoded it should read "Hi AA". Notice the space between the two encoded words has been removed. The existing code (without the fix below) results in "Hi A A".

Repaired code:

///
/// Canonical decoding. Decodes all canonical encoding occurences in specified text.
/// Usually mime message header unicode/8bit values are encoded as Canonical.
/// Format: =?charSet?type[Q or B]?encoded_string?= .
/// Defined in RFC 2047.
///

/// <param name="text" />Text to decode.
/// <returns>
public static string CanonicalDecode(string text)
{
/* RFC 2047
Generally, an "encoded-word" is a sequence of printable ASCII
characters that begins with "=?", ends with "?=", and has two "?"s in
between.

Syntax: =?charSet?type[Q or B]?encoded_string?=

Examples:
=?utf-8?q?Buy a Rolex?=
=?iso-8859-1?B?bORs5D8=?=
*/

bool encodedWordPreceeding = false;
StringBuilder retVal = new StringBuilder();
int offset = 0;
while (offset < text.Length)
{
// Search start and end of canonical entry
int iStart = text.IndexOf("=?", offset);
int iEnd = -1;
if (iStart > -1)
{
// End index must be over start index position
iEnd = text.IndexOf("?=", iStart + 2);
}

if (iStart > -1 && iEnd > -1)
{
// Add left side non encoded text of encoded text, if there is any... but:
// see paragraph 3 of section 6.2 of http://www.ietf.org/rfc/rfc2047.txt:
// When displaying a particular header field that contains multiple
// 'encoded-word's, any 'linear-white-space' that separates a pair of
// adjacent 'encoded-word's is ignored. (This is to allow the use of
// multiple 'encoded-word's to represent long strings of unencoded text,
// without having to separate 'encoded-word's where spaces occur in the
// unencoded text.)
if ((iStart - offset) > 0)
{
string append = text.Substring(offset, iStart - offset);

if (encodedWordPreceeding)
{
append = append.Trim();
if (append.Length > 0) // else... don't change encodedWordPreceeding... it's still true
{
encodedWordPreceeding = false;
retVal.Append(append);
}
}
else
{
retVal.Append(append);
encodedWordPreceeding = false; // false because we just appended a non-encoded word
}
}

while (true)
{
// Check if it is encoded entry
string[] charset_type_text = text.Substring(iStart + 2, iEnd - iStart - 2).Split('?');
if (charset_type_text.Length == 3)
{
// Try to parse encoded text
try
{
Encoding enc = Encoding.GetEncoding(charset_type_text[0]);
// QEncoded text
if (charset_type_text[1].ToLower() == "q")
{
retVal.Append(Core.QDecode(enc, charset_type_text[2]));
}
// Base64 encoded text
else
{
retVal.Append(enc.GetString(Core.Base64Decode(Encoding.Default.GetBytes(charset_type_text[2]))));
}
encodedWordPreceeding = true;
}
catch
{
// Parsing failed, just leave text as is.
retVal.Append(text.Substring(iStart, iEnd - iStart + 2));
}

// Move current offset in string
offset = iEnd + 2;
break;
}
// This isn't right end tag, try next
else if (charset_type_text.Length < 3)
{
// Try next end tag
iEnd = text.IndexOf("?=", iEnd + 2);

// No suitable end tag for active start tag, move offset over start tag.
if (iEnd == -1)
{
retVal.Append("=?");
offset = iStart + 2;
break;
}
}
// Illegal start tag or start tag is just in side some text, move offset over start tag.
else
{
retVal.Append("=?");
offset = iStart + 2;
break;
}
}
}
// There are no more entries
else
{
// Add remaining non encoded text, if there is any.
if (text.Length > offset)
{
retVal.Append(text.Substring(offset));
offset = text.Length;
}
}
}

return retVal.ToString();
}

Eric B
GeneralRe: Bug fix: LumiSoft.Net.Core.CanonicalDecode Pin
Ivar Lumi4-Aug-07 22:41
MemberIvar Lumi4-Aug-07 22:41 
GeneralThis code is the real deal Pin
thefullquid1-Aug-07 15:41
Memberthefullquid1-Aug-07 15:41 
QuestionHow to save a .eml attachment Pin
Sravanthik24-Jul-07 21:31
MemberSravanthik24-Jul-07 21:31 
AnswerRe: How to save a .eml attachment Pin
Ivar Lumi24-Jul-07 23:18
MemberIvar Lumi24-Jul-07 23:18 
GeneralRe: How to save a .eml attachment Pin
Sravanthik25-Jul-07 1:54
MemberSravanthik25-Jul-07 1:54 
GeneralRe: How to save a .eml attachment Pin
Ivar Lumi25-Jul-07 2:44
MemberIvar Lumi25-Jul-07 2:44 
GeneralLicensing Agreement Pin
WashDad5-Jul-07 14:16
MemberWashDad5-Jul-07 14:16 
GeneralRe: Licensing Agreement Pin
Ivar Lumi5-Jul-07 20:44
MemberIvar Lumi5-Jul-07 20:44 
GeneralRe: Licensing Agreement Pin
nelsonlam17-Nov-09 21:40
Membernelsonlam17-Nov-09 21:40 
QuestionHow can I send an a web archive file in the mail body ? Pin
Anupama Matta27-Jun-07 18:45
MemberAnupama Matta27-Jun-07 18:45 
AnswerRe: How can I send an a web archive file in the mail body ? Pin
Ivar Lumi27-Jun-07 20:49
MemberIvar Lumi27-Jun-07 20:49 
GeneralRe: How can I send an a web archive file in the mail body ? Pin
Anupama Matta2-Jul-07 19:49
MemberAnupama Matta2-Jul-07 19:49 
GeneralRe: How can I send an a web archive file in the mail body ? Pin
Ivar Lumi2-Jul-07 20:53
MemberIvar Lumi2-Jul-07 20:53 
GeneralAttachments... Pin
retowittwer20-Jun-07 7:15
Memberretowittwer20-Jun-07 7:15 
GeneralRe: Attachments... Pin
Ivar Lumi20-Jun-07 9:15
MemberIvar Lumi20-Jun-07 9:15 
QuestionLicence Pin
Cendro16-May-07 0:53
MemberCendro16-May-07 0:53 
AnswerRe: Licence Pin
Ivar Lumi16-May-07 0:56
MemberIvar Lumi16-May-07 0:56 

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.