C# Outlook加载项在C中的开发#

C# Outlook加载项在C中的开发#,c#,outlook,add-in,C#,Outlook,Add In,我目前正在尝试为outlook编写一个加载项,如果电子邮件来自外部地址(我的公司之外),它将显示一个消息框 我是新来的,所以我试图采取婴儿步,但我撞到了墙。在我看来,这应该是可行的,但什么也没有出现 public partial class ThisAddIn { Outlook.Inspectors inspectors; private string getSenderEmailAddress(Outlook.MailItem mail) { Ou

我目前正在尝试为outlook编写一个加载项,如果电子邮件来自外部地址(我的公司之外),它将显示一个消息框

我是新来的,所以我试图采取婴儿步,但我撞到了墙。在我看来,这应该是可行的,但什么也没有出现

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
}
public分部类ThisAddIn
{
展望.视察员;
私有字符串getSenderEmailAddress(Outlook.MailItem邮件)
{
Outlook.AddressEntry sender=mail.sender;
字符串SenderEmailAddress=“”;
if(sender.AddressEntryUserType==Outlook.oladdresentryUserType.olExchangeUserAddressEntry | | sender.AddressEntryUserType==Outlook.oladdresentryUserType.olExchangeRemoteUserAddressEntry)
{
Outlook.ExchangeUser exchUser=sender.GetExchangeUser();
if(exchUser!=null)
{
SenderEmailAddress=exchUser.PrimarySmtpAddress;
}
}
其他的
{
SenderEmailAddress=mail.SenderEmailAddress;
}
返回SenderEmailAddress;
}
无效检查器\u新检查器(Microsoft.Office.Interop.Outlook.Inspector)
{
Outlook.MailItem MailItem=Inspector.CurrentItem作为Outlook.MailItem;
if(mailItem!=null)
{
如果(getSenderEmailAddress(mailItem).Contains(“@gmail.com”))
{
MessageBox.Show(“来自外部电子邮件”);
}
}
}
私有void ThisAddIn_启动(对象发送方,System.EventArgs e)
{
检查员=this.Application.inspectors;
检查员,新检查员+=
新的Microsoft.Office.Interop.Outlook.InspectorEvents\u NewInspector或Venthandler(Inspectors\u NewInspector);
}
私有void ThisAddIn_关闭(对象发送方,System.EventArgs e)
{
}
#区域VSTO生成的代码
/// 
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
/// 
私有void InternalStartup()
{
this.Startup+=new System.EventHandler(ThisAddIn\u启动);
this.Shutdown+=new System.EventHandler(ThisAddIn\u Shutdown);
}
#端区
}

这似乎毫无作用。我需要它来检查我的屏幕上任何打开和显示的电子邮件。任何帮助都将不胜感激

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

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

此外,您可能会发现inspector包装器很有用。请在以下文章中阅读更多关于它们的信息:


在调试过程中,我收到一条通知,说它无法预览。然而,代码目前是如何运行的;如果我双击电子邮件并以这种方式打开它,消息框将显示出来。当您只需单击邮件并在outlook主页内阅读邮件时,代码是否有办法读取邮件的发件人信息,您需要处理
Explorer
类的
SelectionChange
事件,并跟踪
选择
对象,根据该对象您可以获取发件人信息。有关更多信息,请参阅。使用Explorer类取得了巨大的成功!我现在得到的效果接近预期!一个后续的问题是,你知道一种方法,我可以在邮件预告的下方显示一个警告文本而不标记电子邮件吗?你可以考虑为它创建一个毗邻的表单区域。