在 C# 中为 Outlook 开发电子邮件跟踪插件

本文关键字:电子邮件 跟踪 插件 开发 Outlook 中为 | 更新日期: 2023-09-27 18:33:25

我想在C#中为Outlook开发电子邮件跟踪插件,我能够创建插件,但不知道如何使用C#跟踪从我的Outlook发送的电子邮件,请分享任何参考博客或代码。 我已经为Outlook创建了简单的Hello World插件

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;

namespace MyAddin
{
public partial class ThisAddIn
{
    private Office.CommandBar _objMenuBar;
    private Office.CommandBarPopup _objNewMenuBar;
    //private Outlook.Inspector Inspectors;
    private Office.CommandBarButton _objButton;
    private string menuTag = "MyMenu";
    Outlook.Inspectors inspectors;
    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        this.MyMenuBar();
        //Inspectors = this.Application.Inspectors;
        inspectors = this.Application.Inspectors;
        inspectors.NewInspector +=
        new Microsoft.Office.Interop.Outlook.InspectorsEvents_NewInspectorEventHandler(Inspectors_NewInspector);
    }
    private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
    {
    }
    void Inspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector Inspector)
    {
        Outlook.MailItem mailItem = Inspector.CurrentItem as Outlook.MailItem;
        if (mailItem != null)
        {
            if (mailItem.EntryID == null)
            {
                mailItem.Subject = "This text was added by using code";
                mailItem.Body = "This text was added by using code";
            }
        }
    }
    private void MyMenuBar()
    {
        this.ErsMyMenuBar();
        try
        {
            _objMenuBar = this.Application.ActiveExplorer().CommandBars.ActiveMenuBar;
            _objNewMenuBar = (Office.CommandBarPopup)
                             _objMenuBar.Controls.Add(Office.MsoControlType.msoControlPopup
                                                    , missing
                                                    , missing
                                                    , missing
                                                    , false);
            if (_objNewMenuBar != null)
            {
                _objNewMenuBar.Caption = "my Plugin";
                _objNewMenuBar.Tag = menuTag;
                _objButton = (Office.CommandBarButton)_objNewMenuBar.Controls.
                Add(Office.MsoControlType.msoControlButton, missing,
                    missing, 1, true);
                _objButton.Style = Office.MsoButtonStyle.
                    msoButtonIconAndCaption;
                _objButton.Caption = "my Plugin";
                //Icon 
                _objButton.FaceId = 500;
                _objButton.Tag = "ItemTag";
                //EventHandler
                _objButton.Click += new Office._CommandBarButtonEvents_ClickEventHandler(_objButton_Click);
                _objNewMenuBar.Visible = true;
            }
        }
        catch (System.Exception ex)
        {
            System.Windows.Forms.MessageBox.Show("Error: " + ex.Message.ToString()
                                               , "Error Message");
        }
    }

    #region VSTO generated code
    private void InternalStartup()
    {
        this.Startup += new System.EventHandler(ThisAddIn_Startup);
        this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
    }
    #endregion
    #region "Event Handler"
    #region "Menu Button"
    private void _objButton_Click(Office.CommandBarButton ctrl, ref bool cancel)
    {
        try
        {
            System.Windows.Forms.MessageBox.Show("Hello World");
        }
        catch (System.Exception ex)
        {
            System.Windows.Forms.MessageBox.Show("Error " + ex.Message.ToString());
        }
    }
    #endregion
    #region "Remove Existing"
    private void ErsMyMenuBar()
    {
        // If the menu already exists, remove it.
        try
        {
            Office.CommandBarPopup _objIsMenueExist = (Office.CommandBarPopup)
                this.Application.ActiveExplorer().CommandBars.ActiveMenuBar.
                FindControl(Office.MsoControlType.msoControlPopup
                          , missing
                          , menuTag
                          , true
                          , true);
            if (_objIsMenueExist != null)
            {
                _objIsMenueExist.Delete(true);
            }
        }
        catch (System.Exception ex)
        {
        System.Windows.Forms.MessageBox.Show("Error: " + ex.Message.ToString(), "Error Message");
        }
    }
    #endregion
    #endregion
}

}

在 C# 中为 Outlook 开发电子邮件跟踪插件

我看到您在代码中使用了命令栏。从 Office 2007 开始,将使用新的 UI。命令栏自 Office 2010 起已弃用。您需要改用流畅 UI(也称为功能区 UI)。可以在以下系列文章中阅读有关新 UI 的详细信息:

  • 为开发人员自定义 2007 Office Fluent 功能区(第 1 部分,共 3 部分)
  • 为开发人员自定义 2007 Office Fluent 功能区(第 2 部分,共 3 部分)
  • 为开发人员自定义 2007 Office Fluent 功能区(第 3 部分,共 3 部分)

跟踪电子邮件有不同的方法:

  1. 添加自定义属性。这不是最好的方法,因为财产可能会消失。
  2. 使用 Outlook 项目的类似对话 (*Id + *索引) 属性。Outlook 使用这些属性对对话中的相关项目进行分组。
  3. 将您的 ID 显式添加到主题行。
选择

哪种方式取决于您...