2014-05-09 42 views
0

Outlook 2010添加用于从Outlook 2010上配置的电子邮件ID自动提取附件。对于每个配置的电子邮件ID,将会有一个单独的文件夹,其中的附件将自动保存在其中。我不想单击任何按钮或重新加载它。如果邮件到达收件箱文件夹,如果它未读,它的附件将被提取并保存在其各自的文件夹中。用于自动电子邮件附件提取的Outlook 2010插件

我的问题是,我还没有能够提取Outlook 2010上的非默认电子邮件ID附件,而且我的过程不会自动提取附件。

如何使未读邮件的附件提取并自动为Outlook 2010上的多个配置的电子邮件ID保存?在这里,我附上了我已经尝试过的代码....

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Xml.Linq; 
using Outlook = Microsoft.Office.Interop.Outlook; 
using Office = Microsoft.Office.Core; 
using System.Windows.Forms; 
using System.IO; 

namespace ITAPOutlookAddIn 
{ 
    public partial class ThisAddIn 
    { 
     public void ThisAddIn_Startup(object sender, System.EventArgs e) 
     { 
      this.Application.NewMail += new Microsoft.Office.Interop.Outlook 
       .ApplicationEvents_11_NewMailEventHandler(ThisApplication_NewMail); 

      this.Application.NewMail += new Microsoft.Office.Interop.Outlook 
       .ApplicationEvents_11_NewMailEventHandler(ThisApplication_NewMailStatus); 
     } 


     public void ThisApplication_NewMail() 
     { 
      const string destinationDirectory = @"C:\TestFileSave"; 
      const string destinationDirectory2 = @"C:\TestFileForm"; 
      if (!Directory.Exists(destinationDirectory)) 
      { 
       Directory.CreateDirectory(destinationDirectory); 
      } 
      if (!Directory.Exists(destinationDirectory2)) 
      { 
       Directory.CreateDirectory(destinationDirectory2); 
      } 
      Outlook.MAPIFolder inBox = this.Application.ActiveExplorer() 
       .Session.GetDefaultFolder(Outlook 
       .OlDefaultFolders.olFolderInbox); 
      Outlook.Items inBoxItems = inBox.Items; 

      Outlook.MailItem newEmail = null; 
      inBoxItems = inBoxItems.Restrict("[Unread] = true"); 

      try 
      { 
       foreach (object collectionItem in inBoxItems) 
       { 
        newEmail = collectionItem as Outlook.MailItem; 
        if (newEmail == null) 
         continue; 
        if (newEmail != null) 

        { 

         if (newEmail.ReceivedByName == "Sumit Ray") 
         { 
          if (newEmail.Attachments.Count > 0) 
          { 
           for (int i = 1; i <= newEmail 
           .Attachments.Count; i++) 
           { 
            string filepath = Path.Combine(destinationDirectory, newEmail.Attachments[i].FileName); 
            string Sname = newEmail.SentOnBehalfOfName; 
            string timestamp = newEmail.ReceivedTime.ToString("MMddyyyy.HHmmss"); 
            string result = filepath + Sname + timestamp + ".docx"; 
            newEmail.Attachments[i].SaveAsFile(result); 
            // newEmail.Attachments[i].SaveAsFile 
            //  (@"C:\TestFileSave\" + 
            //  newEmail.Attachments[i].FileName); 
           } 
          } 
         } //end of inner-if 
        } //end of outer-if 
       } //end of for-each 
       }//end of try 
      catch (Exception ex) 
      { 
       Console.WriteLine(ex); 
       string errorInfo = (string)ex.Message 
        .Substring(0, 11); 
       if (errorInfo == "Cannot save" && newEmail.SenderName == "Sumit Ray") 
       { 
        MessageBox.Show(@"Create Folder C:\TestFileSave"); 
       } 
      } //end of catch                  void ThisApplication_NewMailStatus() 
     { 
      Outlook.NameSpace outlookNameSpace = this.Application.GetNamespace("MAPI"); 

      Outlook.MAPIFolder inbox = outlookNameSpace.GetDefaultFolder 
       (Outlook.OlDefaultFolders.olFolderInbox); 

      // Mark each unread message from Jeff Hay with a yellow flag icon. 
      Outlook.Items unreadMailItems = 
       inbox.Items.Restrict("[Unread]= true"); 
     // if (Convert.ToBoolean(unreadMailItems) 
      if(unreadMailItems.Equals(true)) 
      { 
       ThisApplication_NewMail(); 
      } 
     } 

     private void ThisAddIn_Shutdown(object sender, System.EventArgs e) 
     { 
     } 
private void InternalStartup() 
     { 
      this.Startup += new System.EventHandler(ThisAddIn_Startup); 
      this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown); 
     } 

     #endregion 
    } 
} 
+0

@http://stackoverflow.com/users/332059/dmitry-streblechenko我希望这次能够做得更准确...谢谢 – Sumit

回答

1

您的意思是您需要阅读来自非默认商店的未读消息吗?而不是使用outlookNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox),循环访问Namespace.Stores集合并调用Store.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)

+0

您的建议解决方案很有效。感谢您的帮助。 – Sumit

+0

如果这解决了你的问题,请将答案标记为已解决。 – Cory

相关问题