Outlook 2010插件用于自动电子邮件附件提取

时间:2014-05-09 14:10:05

标签: c# visual-studio-2010 add-in outlook-2010

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

我的问题是我无法在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
    }
}   

1 个答案:

答案 0 :(得分:1)

您是否需要从非默认商店中读取未读邮件?而不是使用outlookNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox),循环遍历Namespace.Stores集合并调用Store.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)