Outlook ItemAdd事件未持续触发

时间:2019-04-22 15:15:59

标签: c# outlook vsto outlook-addin

我正在尝试确定传入邮件是否为垃圾邮件。 Itemadd事件仅工作一次。它不会持续触发。

如何解决此问题?

我照做:

public partial class ThisAddIn
{

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        Outlook.MAPIFolder inbox = Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
        inbox.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(InboxFolderItemAdded);
    }

    private void InboxFolderItemAdded(object Item)
    {
        if (Item is Outlook.MailItem)
        {
            Debug.WriteLine("I'm in!");
            Outlook.MailItem mail = (Outlook.MailItem)Item;
            int[] answer = Predict(mail);

            if(answer[0] == 0) // Not spam
            {
                Outlook.MAPIFolder inboxFolder = ((Outlook.MAPIFolder)this.Application.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox));
                mail.Move(inboxFolder);
            }

            else if(answer[0] == 1) // Spam
            {
                Outlook.MAPIFolder junkFolder = ((Outlook.MAPIFolder)this.Application.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderJunk));
                mail.Move(junkFolder);
            }
        }
    }

    private int[] Predict(Outlook.MailItem mailBody)
    {
        Debug.WriteLine("I'm inside in predict function");
        double[]feature = featureExtraction.findFeatureIncomingMail(mailBody.Body);
        int[] answer = tree.Decide(feature);
        return answer;
    }
}

当我向自己发送邮件时,“我在”和“我在预测功能中”在调试控制台中仅显示一次。

2 个答案:

答案 0 :(得分:1)

首先,您的代码将无法编译-ItemAdd对象上没有MAPIFolder事件。它由Items对象公开(从MAPIFolder.Items属性返回)。

第二,引发事件(Items)的对象必须保持活动状态-在类级别上声明它,而不是将其声明为局部变量,该变量在ThisAddIn_Startup方法退出时由垃圾收集器释放。

答案 1 :(得分:0)

工作代码会像

 public partial class ThisAddIn
 {
    Outlook.Items items=null;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
     Outlook.MAPIFolder inbox = 
    Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
    items=inbox.Items;
    items.ItemAdd += new 
    Outlook.ItemsEvents_ItemAddEventHandler(InboxFolderItemAdded);
}

private void InboxFolderItemAdded(object Item)
{
    if (Item is Outlook.MailItem)
    {
        Debug.WriteLine("I'm in!");
        Outlook.MailItem mail = (Outlook.MailItem)Item;
        int[] answer = Predict(mail);

        if(answer[0] == 0) // Not spam
        {
            Outlook.MAPIFolder inboxFolder = ((Outlook.MAPIFolder)this.Application.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox));
            mail.Move(inboxFolder);
        }

        else if(answer[0] == 1) // Spam
        {
            Outlook.MAPIFolder junkFolder = ((Outlook.MAPIFolder)this.Application.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderJunk));
            mail.Move(junkFolder);
        }
    }
}

private int[] Predict(Outlook.MailItem mailBody)
{
    Debug.WriteLine("I'm inside in predict function");
    double[]feature = featureExtraction.findFeatureIncomingMail(mailBody.Body);
    int[] answer = tree.Decide(feature);
    return answer;
 }
}