2017-05-09 56 views
0

我想創建一個Outlook添加,其中我們有2個電子郵件帳戶,我想將回復郵件設置爲默認郵件集。目前,發送電子郵件默認爲發送給它的任何帳戶。我在網上查了一下,看到了類似的帖子,但是一旦我關閉電子郵件,代碼就不會再設置我選擇的默認電子郵件了。你能幫忙嗎?以下是我的代碼:始終使用默認電子郵件發送郵件。 Outlook添加工作,直到電子郵件被取消

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.DirectoryServices.AccountManagement; 

using System.Windows.Forms; 

using System.Runtime.InteropServices; 

    namespace OutlookAddIn1{ 
     public partial class ThisAddIn 
     { 
      private Outlook.Inspectors inspectors; 
      private Outlook.Accounts accounts; 
      private Outlook.MailItem mailItem; 
      private void ThisAddIn_Startup(object sender, System.EventArgs e) 
      { 
       inspectors = this.Application.Inspectors; 
       inspectors.NewInspector += new Microsoft.Office.Interop.Outlook.InspectorsEvents_NewInspectorEventHandler(Inspectors_NewInspector); 
      } 


    void Inspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector Inspector) 
      { 
      mailItem =Inspector.CurrentItem; 

       accounts = Application.Session.Accounts; 
      if (mailItem != null) 
       { 
         if (mailItem.EntryID == null) 
         { 
          foreach (Outlook.Account account in accounts) 
       { 
           String strname = "xxxx"; 

           if (account.SmtpAddress.ToString().IndexOf(strname)>0) 
           { 
            mailItem.SendUsingAccount =account; 

           } 
          } 
          } 
      } 

     } 
     } 
    } 

    #region VSTO generated code 

     /// <summary> 
     /// Required method for Designer support - do not modify 
     /// the contents of this method with the code editor. 
     /// </summary> 
     private void InternalStartup() 
     { 


      this.Startup += new System.EventHandler(ThisAddIn_Startup); 
      this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown); 


     } 

     #endregion 
} 

非常感謝!

回答

0

首先,我建議等待第一個Activate事件訪問Outlook項目。

無論如何,要保存以編程方式進行的更改,您需要使用Save方法保存項目,該方法將Microsoft Outlook項目保存到當前文件夾,或者如果這是一個新項目,則保存到項目類型的Outlook默認文件夾。

+0

你好尤金,感謝您的迴應。你能幫助分享它的代碼嗎? – Derrick

+0

你好尤金,它工作!我的參考是從https://www.pcreview.co.uk/threads/inspector-activate-event-under-vs2005-c.3206710/...Thanks很多! – Derrick

相關問題