Click here to Skip to main content
15,924,982 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
foreach (MailMessage cm in mm)
{
Console.WriteLine("Message No." + i + " " + cm.From);
string curpath = Directory.GetCurrentDirectory();
string mailbox = String.Format("{0}\\inbox", curpath);

if (!Directory.Exists(mailbox))
{
Directory.CreateDirectory(mailbox);
}
System.DateTime d = System.DateTime.Now;
System.Globalization.CultureInfo cur = new
System.Globalization.CultureInfo("en-US");
string sdate = d.ToString("MMyyyyddHHmmss", cur);
string fileName1 = String.Format("{0}\\{1}{2}{3}.eml", mailbox, sdate, d.Millisecond.ToString("d3"), i); //mailbox, sdate, d.Millisecond.ToString("d3"), i,
StreamWriter sw = new StreamWriter(fileName1);
cm.Save(sw);
i++
}


Here the eml file is been created but the attachments are shown as text when opened with thunderbird. Somebody help me to get the attachment too saved in eml in correct formats. Thanks.
Posted

1 solution

Considering no email server will send you an email as a file your question doesn't really make any sense.

You have to download the email as any IMAP or SMTP client would and then take that information and build an EML file yourself.

The code you have doesn't build a real EML file. You have to take the information in the MailMessage object and write the RFC 822 file format yourself. You can read up on the format here[^].
 
Share this answer
 
Comments
Ajay Vamu 7-Apr-15 11:43am    
Ae.Net.Mail is an Imap library. I'm using it to save the mail as eml... It happens but the attachments are in Base64 encoded type. So when I open the eml file attachments are seen like a very long text in the body of the mail. Thanks for your reply Dave Kreskowiak. Will be happy if you provide me a solution for this regarding how to save this file format by myself... Thanks.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900