0

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 = "test@mail.com";
        string from = "test@mail.com";
        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();
        }
    }
gymcode
  • 4,431
  • 15
  • 72
  • 128
  • So it contains the HTML then? It's nothing to do with what your saving. Whatever your opening this file with just using isn't interpreting the HTML. – Liam Aug 22 '19 at 09:26
  • Rename the file extension to .mht and open with IE. Does it look like you expected? – LocEngineer Aug 22 '19 at 09:29
  • I have renamed it to `.mht` as well. Both formats are displaying in full text form instead of the original formatted `.html` with table formatting and images. My apologies, I may not have explained myself clearly. Let me edit my post. – gymcode Aug 22 '19 at 09:33
  • You might want to try something from this thread: https://stackoverflow.com/questions/6293129/save-mail-to-msg-file-using-ews-api – LocEngineer Aug 22 '19 at 09:56

1 Answers1

1

You are essentially reassembling the message from a few properties. Why not grab the whole MIME message (with all the headers, attachments etc.)?

See https://learn.microsoft.com/en-us/exchange/client-developer/exchange-web-services/how-to-export-items-by-using-ews-in-exchange

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78