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);
mail.SetContentType("multipart/mixed");
mail.SetBoundary(null);
MimeBody mBody = mail.CreatePart();
mBody.SetText("Hi, there");
mBody = mail.CreatePart();
mBody.SetDescription("enclosed photo",null);
mBody.SetTransferEncoding("base64");
mBody.ReadFromFile(".\\00.jpg");
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");
mBody = mail.CreatePart();
mBody.SetDescription("enclosed message",null);
mBody.SetTransferEncoding("7bit");
mBody.SetMessage(mail2);
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");
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