Save content of Email body in outlook to a file

谁说我不能喝 提交于 2019-12-06 17:20:21

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/5541848/save-content-of-email-body-in-outlook-to-a-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!