【问题标题】:Save Email as .eml with original email.body使用原始 email.body 将电子邮件另存为 .eml
【发布时间】:2019-12-27 13:54:00
【问题描述】:

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

有没有办法将email.Body的原始HTML格式保存为原来的样子?

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

【问题讨论】:

  • 那么它包含HTML呢?这与你的储蓄无关。无论您使用什么打开此文件都不会解释 HTML。
  • 将文件扩展名重命名为.mht并用IE打开。它看起来像你预期的那样吗?
  • 我也将其重命名为.mht。这两种格式都以全文形式显示,而不是带有表格格式和图像的原始格式 .html。对不起,我可能没有清楚地解释自己。让我编辑我的帖子。
  • 你可能想从这个线程中尝试一些东西:stackoverflow.com/questions/6293129/…

标签: c# exchangewebservices mailmessage


【解决方案1】:

您实际上是从几个属性中重新组装消息。为什么不抓取整个 MIME 消息(包括所有标题、附件等)?

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

【讨论】:

    猜你喜欢
    • 2016-05-24
    • 2022-10-06
    • 1970-01-01
    • 1970-01-01
    • 2012-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-23
    相关资源
    最近更新 更多