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

时间:2011-04-04 17:04:50

标签: c# ssis outlook task

我想将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   );

1 个答案:

答案 0 :(得分:2)

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