始终使用默认电子邮件集发送邮件。 Outlook Add in 工作直到电子邮件被取消

Always send mail using default email set. Outlook Add in works until email message is cancelled

我正在尝试创建一个 outlook 添加,其中我们有 2 个电子邮件帐户,我想将回复邮件设置为默认邮件集。目前,发送电子邮件默认为发送到的任何帐户。我已经在线查看并看到类似的帖子有效,但是一旦我关闭电子邮件,代码就不会再设置我选择的默认电子邮件。你能帮忙吗?下面是我的代码:

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
}

非常感谢!

首先,我建议等待第一个 Activate 事件来访问 Outlook 项目。

无论如何,要保留以编程方式所做的更改,您需要使用 Save 方法保存项目,该方法将 Microsoft Outlook 项目保存到当前文件夹,或者如果这是一个新项目,则保存到 Outlook 默认文件夹项类型的文件夹。