将电子邮件另存为 .eml 并包含原始 email.body

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


有没有办法保存 的原始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.");

            }



守着星空守着你
浏览 62回答 1
1回答

宝慕林4294392

您实际上是从一些属性重新组装消息。为什么不获取整个 MIME 消息(包含所有标头、附件等)?
打开App,查看更多内容
随时随地看视频慕课网APP