2014-05-09 2 views
0

Outlook 2010 ajoute l'extraction automatique de la pièce jointe à partir des identifiants de courrier électronique configurés sur Outlook 2010. Pour chaque ID de messagerie configuré, un dossier distinct dans lequel les pièces jointes seront enregistrées automatiquement. Je ne veux pas cliquer sur un bouton ou le recharger tout le temps. Si un courrier arrive dans le dossier de la boîte de réception, s'il n'est pas lu, ses pièces jointes seront extraites et enregistrées dans son dossier respectif.Outlook 2010 addins pour l'extraction automatique de pièces jointes électroniques

Mon problème est que je n'ai pas été en mesure d'extraire les pièces jointes des e-mails non-défaut sur Outlook 2010 et mon processus n'extrait pas automatiquement la pièce jointe.

Comment puis-je effectuer l'extraction des pièces jointes non lues et l'enregistrement automatique de plusieurs identifiants d'e-mail configurés sur Outlook 2010? Ici, j'attacher le code que j'ai essayé ....

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 J'espère être précis cette fois-ci ... Merci – Sumit

Répondre

1

Voulez-vous dire que vous devez lire les messages non lus à partir d'un magasin non par défaut? Au lieu d'utiliser outlookNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox), parcourez la collection Namespace.Stores et appelez le Store.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox).

+0

Votre solution proposée a travaillé.Merci une tonne pour votre aide. – Sumit

+0

Marquer la réponse comme résolue si cela l'a corrigé pour vous. – Cory

Questions connexes