Click here to Skip to main content
Click here to Skip to main content

A C# Implementation of Mime De/encode

, 21 Aug 2005 CPOL
Rate this:
Please Sign up or sign in to vote.
A C# implementation of Mime de/encode


Several days ago, when I needed a MIME lib to encode/decode some MIME type messages in C#, I did not find any suitable classes. What I only found is a C++ implementation written by nickadams. I read his code and then, I translated it into C# code. And it works well for me.

How To Use

It is as easy to use as nickadams' code.Smile | :) Here's the example:

Encode a Message

   MimeMessage mail = new MimeMessage();
      Nick Name <>, 
      \"Nick Name\" <>");
   mail.SetSubject("test mail",null);
   mail.SetFieldValue("X-Priority", "3 (Normal)", null);

   // Initialize header
   // generate a boundary string automatically
   // if the parameter is NULL
   // Add a text body part
   // default Content-Type is "text/plain"
   // default Content-Transfer-Encoding is "7bit"
   MimeBody mBody = mail.CreatePart();
   mBody.SetText("Hi, there");  // set the content of the body part

   // Add a file attachment body part
   mBody = mail.CreatePart();
   mBody.SetDescription("enclosed photo",null);
   // if Content-Type is not specified, it'll be
   // set to "image/jpeg" by ReadFromFile()

   // Generate a simple message
   MimeMessage mail2 = new MimeMessage();
   mail2.SetSubject("This is an attached message",null);
   mail2.SetText("Content of attached message.\r\n");

   // Attach the message
   mBody = mail.CreatePart();
   mBody.SetDescription("enclosed message",null);
   // if Content-Type is not specified, it'll be
   // set to "message/rfc822" by SetMessage()

   // Add an embedded multipart
   mBody = mail.CreatePart();
   MimeBody mBodyChild = mBody.CreatePart();
   mBodyChild.SetText("Content of Part 1\r\n");
   mBodyChild = mBody.CreatePart();
   mBodyChild.SetText("Content of Part 2\r\n");

   //store content to a string buffer
   StringBuilder sb = new StringBuilder();

Decode a Message

   StreamReader sr = new StreamReader(".\\aaa.txt");
   string message = sr.ReadToEnd();
   MimeMessage aMimeMessage = new MimeMessage();
   ArrayList bodylist = new ArrayList();
   for(int i=0;i<bodylist.Count;i++)
    MimeBody ab = (MimeBody) bodylist[i];
     string m = ab.GetText();
    else if(ab.IsAttachment())


Many thanks to nickadams. Your great work helps me a lot.


  • 21st August, 2005: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
China China
I think I have to start coding my life now...Smile | :)

Comments and Discussions

GeneralHpw to download attatchment that are inside MIME file Pinmemberrishi2123-Feb-11 18:48 
GeneralError decoding image attachments please help PinmemberWajeeh7-Dec-09 21:40 
GeneralUnsafe code PinmemberGWBas1c6-Aug-09 10:38 
Does this need unsafe code? MIME handling isn't special enough to warrant the elevated privileges needed to work with pointers.
GeneralGreat! Thanks. Pinmemberbhiller24-Jul-09 4:29 
Generalgod bless you Pinmemberbookerman12-Jan-09 6:10 
GeneralLicense PinmemberMember 384073731-Aug-08 5:18 
GeneralBug: empty lines in mime header and mime body without children PinmemberdB.26-Jun-08 7:17 
GeneralBug: inifinite loop in processing PinmemberdB.26-Jun-08 5:15 
GeneralMIME digital signature Pinmemberkerberos_prozac5-Jul-06 7:53 
GeneralRe: MIME digital signature Pinmemberkerberos_prozac5-Jul-06 8:11 
QuestionHow can we use this to send emails? PinmemberBohemianDre26-Jun-06 17:51 
Generaldecode from binary Pinmemberjsli@ukr.net5-Jan-06 22:48 
GeneralA Couple Bugs Pinmembermsailing27-Oct-05 8:52 
QuestionCan someone confirm that this works outside of encoding and decoding using the same classes? PinsussAnonymous22-Sep-05 15:59 
AnswerRe: Can someone confirm that this works outside of encoding and decoding using the same classes? PinsussAnonymous23-Sep-05 11:12 
AnswerWHOOPS! Looks like I was wrong. PinsussAnonymous23-Sep-05 11:23 
GeneralMEDIA VEDIO --&gt; MEDIA VIDEO Pinmemberfeuerfrei22-Aug-05 1:05 
GeneralRe: MEDIA VEDIO --&gt; MEDIA VIDEO Pinmemberlionwind22-Aug-05 2:58 
GeneralC# conversion Pinmemberszgt21-Aug-05 23:28 
GeneralRe: C# conversion Pinmemberlionwind22-Aug-05 2:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150414.1 | Last Updated 22 Aug 2005
Article Copyright 2005 by tauyoung
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid