将 Outlook 中的电子邮件正文内容保存到文件

发布于 2024-10-29 15:21:59 字数 2681 浏览 0 评论 0原文

我想将 Outlook 中的电子邮件正文内容保存到文件中。我可以保存整个消息 .msg,但我只想保存正文的 html 内容。 例如: 在 Outlook 电子邮件正文中,我有一个表,我想将该表保存到文件中。 我正在处理的脚本:

public void GetAttachments()
    {
       Microsoft.Office.Interop.Outlook.Application myolApp = default(Microsoft.Office.Interop.Outlook.Application);
       Microsoft.Office.Interop.Outlook.NameSpace ns = default(NameSpace);
       MAPIFolder Inbox = default(MAPIFolder);
       object Item = null;
       Attachment Atmt = default(Attachment);
       string FileName = null;
       string subject = null;
       string AttachmentName = null;
       string Body = null;
       string SenderName = null;
       string SenderEmailAddress = null;
       string CreationTime = null;
       int i = 0;
       int j = 0;
       try
       {

        myolApp = (Microsoft.Office.Interop.Outlook.Application)Interaction.CreateObject("Outlook.Application","");
        ns = myolApp.GetNamespace("MAPI");
        ns.Logon("", "", false, true);
        Inbox = ns.Folders["Mailbox - Name"].Folders["Inbox"];
        i = 0;
        j = 1;

        //Scan for attachments
        foreach (object Item_loopVariable in Inbox.Items) 
        {
           Item = Item_loopVariable;
           System.Windows.Forms.Application.DoEvents();

           if ((Item as MailItem) != null ? ((MailItem)Item).UnRead : false) 
           {
             Body = ((Microsoft.Office.Interop.Outlook.MailItem)Item).Body;
             ((Microsoft.Office.Interop.Outlook.MailItem)Item).HTMLBody = Body;
             ((Microsoft.Office.Interop.Outlook.MailItem)Item).SaveAs(@"\\path\"+"filename",                                       Microsoft.Office.Interop.Outlook.OlSaveAsType.olHTML   );

                  j = j + 1;

           }
        }



    //Clear Memory
    Atmt = null;
    Item = null;
    ns = null;

 }
      catch (System.Exception ex)
      {
            MessageBox.Show("An unexpected error has occurred." 
             + "\r\n" + "Please note and report the following information."
             + "\r\n" + "Script Name: GetAttachments"
             + "\r\n" + "Error Description: " + ex.Message
             + "\r\n" + "Error StackTrace: " + ex.StackTrace
                , "Error!");
            Atmt = null;
            Item = null;
            ns = null;
      }
}

我需要更改这些代码:

Body = ((Microsoft.Office.Interop.Outlook.MailItem)Item).Body;
             ((Microsoft.Office.Interop.Outlook.MailItem)Item).HTMLBody = Body;
             ((Microsoft.Office.Interop.Outlook.MailItem)Item).SaveAs(@"\\path\"+"filename",                                       Microsoft.Office.Interop.Outlook.OlSaveAsType.olHTML   );

i want to save the contents of the Email Body in outlook to a file. I am able to save the entire message .msg but i want to save only the html content of the body.
for example:
In the outlook email body i have a table i want to save that table to a file.
the script which i am working on:

public void GetAttachments()
    {
       Microsoft.Office.Interop.Outlook.Application myolApp = default(Microsoft.Office.Interop.Outlook.Application);
       Microsoft.Office.Interop.Outlook.NameSpace ns = default(NameSpace);
       MAPIFolder Inbox = default(MAPIFolder);
       object Item = null;
       Attachment Atmt = default(Attachment);
       string FileName = null;
       string subject = null;
       string AttachmentName = null;
       string Body = null;
       string SenderName = null;
       string SenderEmailAddress = null;
       string CreationTime = null;
       int i = 0;
       int j = 0;
       try
       {

        myolApp = (Microsoft.Office.Interop.Outlook.Application)Interaction.CreateObject("Outlook.Application","");
        ns = myolApp.GetNamespace("MAPI");
        ns.Logon("", "", false, true);
        Inbox = ns.Folders["Mailbox - Name"].Folders["Inbox"];
        i = 0;
        j = 1;

        //Scan for attachments
        foreach (object Item_loopVariable in Inbox.Items) 
        {
           Item = Item_loopVariable;
           System.Windows.Forms.Application.DoEvents();

           if ((Item as MailItem) != null ? ((MailItem)Item).UnRead : false) 
           {
             Body = ((Microsoft.Office.Interop.Outlook.MailItem)Item).Body;
             ((Microsoft.Office.Interop.Outlook.MailItem)Item).HTMLBody = Body;
             ((Microsoft.Office.Interop.Outlook.MailItem)Item).SaveAs(@"\\path\"+"filename",                                       Microsoft.Office.Interop.Outlook.OlSaveAsType.olHTML   );

                  j = j + 1;

           }
        }



    //Clear Memory
    Atmt = null;
    Item = null;
    ns = null;

 }
      catch (System.Exception ex)
      {
            MessageBox.Show("An unexpected error has occurred." 
             + "\r\n" + "Please note and report the following information."
             + "\r\n" + "Script Name: GetAttachments"
             + "\r\n" + "Error Description: " + ex.Message
             + "\r\n" + "Error StackTrace: " + ex.StackTrace
                , "Error!");
            Atmt = null;
            Item = null;
            ns = null;
      }
}

I need changes in these piece of code:

Body = ((Microsoft.Office.Interop.Outlook.MailItem)Item).Body;
             ((Microsoft.Office.Interop.Outlook.MailItem)Item).HTMLBody = Body;
             ((Microsoft.Office.Interop.Outlook.MailItem)Item).SaveAs(@"\\path\"+"filename",                                       Microsoft.Office.Interop.Outlook.OlSaveAsType.olHTML   );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千と千尋 2024-11-05 15:22:00

就文件系统安全性而言,Outlook 在保存电子邮件正文方面存在一些问题。解决方法是使用文件系统对象来保存正文 - 对我有用。

Outlook has some issues with saving the body of an email, as far as the filesystem security goes. A workaround is to use the file system objects to save the body - worked for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文