Reputation: 4623
I am saving my Email
using EWS
api using the following code. However, when I open the saved .eml
and also in .mht
format, it is in full text with formatting <tags>
.
Is there a way to save the original HTML
format of the email.Body
, with the original look of it?
private static void saveEmailAsEML(EmailMessage email)
{
{
string to = "[email protected]";
string from = "[email protected]";
string subject = "test subject";
string body = email.Body.();
string emailDir = @"C:\\Temp\\Email";
string msgName = @"email.eml";
Console.WriteLine("Saving e-mail...");
using (var client = new SmtpClient())
{
MailMessage msg = new MailMessage(from, to, subject, body);
client.UseDefaultCredentials = true;
client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
client.PickupDirectoryLocation = emailDir;
try
{
client.Send(msg);
}
catch (Exception ex)
{
Console.WriteLine("Exception caught: {0}",
ex.ToString());
Console.ReadLine();
System.Environment.Exit(-1);
}
}
var defaultMsgPath = new DirectoryInfo(emailDir).GetFiles()
.OrderByDescending(f => f.LastWriteTime)
.First();
var realMsgPath = Path.Combine(emailDir, msgName);
try
{
File.Move(defaultMsgPath.FullName, realMsgPath);
Console.WriteLine("Message saved.");
}
catch (System.IO.IOException e)
{
Console.WriteLine("File already exists. Overwrite it ? Y / N");
var test = Console.ReadLine();
if (test == "y" || test == "Y")
{
Console.WriteLine("Overwriting existing file...");
File.Delete(realMsgPath);
File.Move(defaultMsgPath.FullName, realMsgPath);
Console.WriteLine("Message saved.");
}
else
{
Console.WriteLine("Exiting Program without saving file.");
}
}
Console.WriteLine("Press any key to exit.");
Console.ReadLine();
}
}
Upvotes: 0
Views: 1187
Reputation: 66245
You are essentially reassembling the message from a few properties. Why not grab the whole MIME message (with all the headers, attachments etc.)?
Upvotes: 1