-->

Save content of Email body in outlook to a file

2020-08-09 04:29发布

问题:

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.