2014-05-09 43 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

相關問題