C#中的Outlook加载项开发

时间:2018-04-24 12:47:58

标签: c# outlook add-in

我目前正在尝试为Outlook创建一个加载项,如果电子邮件来自外部地址(我公司以外),则会显示一个消息框。

我是新手,所以我想尝试使用babysteps,但是我已经撞墙了。在我看来,这应该有效,但没有出现。

public partial class ThisAddIn
{

    Outlook.Inspectors inspectors;

    private string getSenderEmailAddress(Outlook.MailItem mail)
    {
        Outlook.AddressEntry sender = mail.Sender;
        string SenderEmailAddress = "";

        if (sender.AddressEntryUserType == Outlook.OlAddressEntryUserType.olExchangeUserAddressEntry || sender.AddressEntryUserType == Outlook.OlAddressEntryUserType.olExchangeRemoteUserAddressEntry)
        {
            Outlook.ExchangeUser exchUser = sender.GetExchangeUser();
            if (exchUser != null)
            {
                SenderEmailAddress = exchUser.PrimarySmtpAddress;
            }
        }
        else
        {
            SenderEmailAddress = mail.SenderEmailAddress;
        }

        return SenderEmailAddress;
    }

    void Inspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector Inspector)
    {
        Outlook.MailItem mailItem = Inspector.CurrentItem as Outlook.MailItem;
        if (mailItem != null)
        {

            if(getSenderEmailAddress(mailItem).Contains("@gmail.com"))
            {
                MessageBox.Show("From an external email");
            }

        }
    }

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        inspectors = this.Application.Inspectors;
        inspectors.NewInspector +=
        new Microsoft.Office.Interop.Outlook.InspectorsEvents_NewInspectorEventHandler(Inspectors_NewInspector);
    }

    private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
    {

    }

    #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)

您是否尝试调试代码?是否完全调用了NewInspector事件处理程序?

无论如何,NewInspector事件处理程序不是访问Outlook项目的正确位置。可以提供假物品,因为此阶段的检查员未完全装载物品。所以,我建议等待Inspector类的第一个Activate事件然后访问该项。

另外,您可能会发现检查员包装有用。在以下文章中阅读有关它们的更多信息: