繁体   English   中英

使用原始email.body将电子邮件另存为.eml

[英]Save Email as .eml with original email.body

我使用以下代码使用EWS api保存我的Email 但是,当我打开保存的.eml并以.mht格式打开时,它是全文本,格式为<tags>

有没有一种方法可以保存email.Body的原始HTML格式, email.Body具有原始外观?

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();
        }
    }

本质上,您是从一些属性中重组消息。 为什么不截取整个MIME消息(包括所有标题,附件等)?

请参阅https://docs.microsoft.com/zh-cn/exchange/client-developer/exchange-web-services/how-to-export-items-by-using-ews-in-exchange

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM