始终使用默认电子邮件集发送邮件。 Outlook添加有效,直到电子邮件被取消

时间:2017-05-09 18:24:44

标签: outlook outlook-addin

我正在尝试创建一个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
}

非常感谢!

1 个答案:

答案 0 :(得分:0)

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

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