Click here to Skip to main content
15,879,051 members
Articles / Programming Languages / C#
Article

A C# Implementation of Mime De/encode

Rate me:
Please Sign up or sign in to vote.
4.00/5 (16 votes)
21 Aug 2005CPOL 150.6K   4.2K   38   23
A C# implementation of Mime de/encode

Introduction

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.:) Here's the example:

Encode a Message

C#
MimeMessage mail = new MimeMessage();
mail.SetDate();
mail.Setversion();
mail.SetFrom("sender@local.com",null);
mail.SetTo("recipient1@server1.com,
   Nick Name <recipient2@server1.com>,
   \"Nick Name\" <recipient3@server2.com>");
mail.SetCC("recipient4@server4.com",null);
mail.SetSubject("test mail",null);
mail.SetFieldValue("X-Priority", "3 (Normal)", null);

// Initialize header
mail.SetContentType("multipart/mixed");
// generate a boundary string automatically
// if the parameter is NULL
mail.SetBoundary(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);
mBody.SetTransferEncoding("base64");
// if Content-Type is not specified, it'll be
// set to "image/jpeg" by ReadFromFile()
mBody.ReadFromFile(".\\00.jpg");

// Generate a simple message
MimeMessage mail2 = new MimeMessage();
mail2.SetFrom("abc@abc.com",null);
mail2.SetTo("abc@abc.com",null);
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);
mBody.SetTransferEncoding("7bit");
// if Content-Type is not specified, it'll be
// set to "message/rfc822" by SetMessage()
mBody.SetMessage(mail2);

// Add an embedded multipart
mBody = mail.CreatePart();
mBody.SetContentType("multipart/alternative");
mBody.SetBoundary("embeded_multipart_boundary");
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();
mail.StoreBody(sb);

Decode a Message

C#
StreamReader sr = new StreamReader(".\\aaa.txt");
string message = sr.ReadToEnd();
MimeMessage aMimeMessage = new MimeMessage();
aMimeMessage.LoadBody(message);
ArrayList bodylist = new ArrayList();
aMimeMessage.GetBodyPartList(bodylist);
for(int i=0;i<bodylist.Count;i++)
{
 MimeBody ab = (MimeBody) bodylist[i];
 if(ab.IsText())
 {
  string m = ab.GetText();
  System.Windows.Forms.MessageBox.Show(m);
 }
 else if(ab.IsAttachment())
 {
  ab.WriteToFile("new"+ab.GetName());
 }
}

Finally...

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

History

  • 21st August, 2005: Initial post

License

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


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

Comments and Discussions

 
QuestionBug report - exception for multiline params Pin
iblesq14-Oct-16 3:35
iblesq14-Oct-16 3:35 
AnswerRe: Bug report - exception for multiline params Pin
iblesq14-Oct-16 20:30
iblesq14-Oct-16 20:30 
QuestionBug report - last field doesn't get loaded in LoadHead Pin
jtrotman1-Jun-15 13:30
jtrotman1-Jun-15 13:30 
GeneralHpw to download attatchment that are inside MIME file Pin
rishi2123-Feb-11 17:48
rishi2123-Feb-11 17:48 
GeneralError decoding image attachments please help Pin
Wajeeh7-Dec-09 20:40
Wajeeh7-Dec-09 20:40 
Hi,

i am testing your code sample for reading MM7 message (Soap Format). Getting SMIL and TEXT attachments out of the message is working fine but when it comes to images it is saving the image with the name present iin the header but the image file cannot be opened to view what it was. windows says that the file is either corrupted or too large. please help.

Regards,
Wajeeh Ullah
GeneralUnsafe code Pin
GWBas1c6-Aug-09 9:38
GWBas1c6-Aug-09 9:38 
GeneralGreat! Thanks. Pin
Bernhard Hiller24-Jul-09 3:29
Bernhard Hiller24-Jul-09 3:29 
Generalgod bless you Pin
bookerman12-Jan-09 5:10
bookerman12-Jan-09 5:10 
GeneralLicense Pin
Member 384073731-Aug-08 4:18
Member 384073731-Aug-08 4:18 
GeneralBug: empty lines in mime header and mime body without children Pin
dB.26-Jun-08 6:17
dB.26-Jun-08 6:17 
GeneralBug: inifinite loop in processing Pin
dB.26-Jun-08 4:15
dB.26-Jun-08 4:15 
GeneralMIME digital signature Pin
kerberos_prozac5-Jul-06 6:53
kerberos_prozac5-Jul-06 6:53 
GeneralRe: MIME digital signature Pin
kerberos_prozac5-Jul-06 7:11
kerberos_prozac5-Jul-06 7:11 
QuestionHow can we use this to send emails? Pin
BohemianDre26-Jun-06 16:51
BohemianDre26-Jun-06 16:51 
Generaldecode from binary Pin
PJ_VIN5-Jan-06 21:48
PJ_VIN5-Jan-06 21:48 
GeneralA Couple Bugs Pin
msailing27-Oct-05 7:52
msailing27-Oct-05 7:52 
QuestionCan someone confirm that this works outside of encoding and decoding using the same classes? Pin
Anonymous22-Sep-05 14:59
Anonymous22-Sep-05 14:59 
AnswerRe: Can someone confirm that this works outside of encoding and decoding using the same classes? Pin
Anonymous23-Sep-05 10:12
Anonymous23-Sep-05 10:12 
AnswerWHOOPS! Looks like I was wrong. Pin
Anonymous23-Sep-05 10:23
Anonymous23-Sep-05 10:23 
GeneralMEDIA VEDIO --&gt; MEDIA VIDEO Pin
feuerfrei22-Aug-05 0:05
feuerfrei22-Aug-05 0:05 
GeneralRe: MEDIA VEDIO --&gt; MEDIA VIDEO Pin
tauyoung22-Aug-05 1:58
tauyoung22-Aug-05 1:58 
GeneralC# conversion Pin
szgt21-Aug-05 22:28
szgt21-Aug-05 22:28 
GeneralRe: C# conversion PinPopular
tauyoung22-Aug-05 1:53
tauyoung22-Aug-05 1:53 

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.