2011-04-04 64 views
0

我想将电子邮件正文的内容保存在Outlook的文件中。我能够保存整个邮件.msg,但我只想保存正文的html内容。 例如: 在Outlook电子邮件正文中,我有一张表,我想将该表保存到一个文件中。 对此我工作的脚本:将电子邮件正文的内容保存到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 ); 

回答

2

展望与节能电子邮件的身体,尽可能的文件系统安全的一些问题去。解决方法是使用文件系统对象来保存正文 - 为我工作。

相关问题