Click here to Skip to main content
6,822,123 members and growing! (17,641 online)
Email Password   helpLost your password?
Languages » C# » General     Intermediate License: The Code Project Open License (CPOL)

A C# Implementation of Mime De/encode

By lionwind

A C# implementation of Mime de/encode
C#, VC7.1, Windows, .NET1.1VS.NET2003, Dev
Posted:21 Aug 2005
Views:39,849
Bookmarked:29 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
11 votes for this article.
Popularity: 4.00 Rating: 3.84 out of 5

1
2 votes, 18.2%
2

3
4 votes, 36.4%
4
5 votes, 45.5%
5

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

   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

   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)

About the Author

lionwind


Member
I think I have to start coding my life now...Smile
Occupation: Web Developer
Location: China China

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 19 of 19 (Total in Forum: 19) (Refresh)FirstPrevNext
GeneralError decoding image attachments please help PinmemberWajeeh21:40 7 Dec '09  
GeneralUnsafe code PinmemberGWBas1c10:38 6 Aug '09  
GeneralGreat! Thanks. Pinmemberbhiller4:29 24 Jul '09  
Generalgod bless you Pinmemberbookerman6:10 12 Jan '09  
GeneralLicense PinmemberMember 38407375:18 31 Aug '08  
GeneralBug: empty lines in mime header and mime body without children PinmemberdB.7:17 26 Jun '08  
GeneralBug: inifinite loop in processing PinmemberdB.5:15 26 Jun '08  
GeneralMIME digital signature Pinmemberkerberos_prozac7:53 5 Jul '06  
GeneralRe: MIME digital signature Pinmemberkerberos_prozac8:11 5 Jul '06  
QuestionHow can we use this to send emails? PinmemberBohemianDre17:51 26 Jun '06  
Generaldecode from binary Pinmemberjsli@ukr.net22:48 5 Jan '06  
GeneralA Couple Bugs Pinmembermsailing8:52 27 Oct '05  
GeneralCan someone confirm that this works outside of encoding and decoding using the same classes? PinsussAnonymous15:59 22 Sep '05  
GeneralRe: Can someone confirm that this works outside of encoding and decoding using the same classes? PinsussAnonymous11:12 23 Sep '05  
GeneralWHOOPS! Looks like I was wrong. PinsussAnonymous11:23 23 Sep '05  
GeneralMEDIA VEDIO --> MEDIA VIDEO Pinmemberfeuerfrei1:05 22 Aug '05  
GeneralRe: MEDIA VEDIO --> MEDIA VIDEO Pinmemberlionwind2:58 22 Aug '05  
GeneralC# conversion Pinmemberszgt23:28 21 Aug '05  
GeneralRe: C# conversion Pinmemberlionwind2:53 22 Aug '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 21 Aug 2005
Editor: Deeksha Shenoy
Copyright 2005 by lionwind
Everything else Copyright © CodeProject, 1999-2010
Web20 | Advertise on the Code Project