Devrim Akıncı
Devrim Akıncı

Reputation: 61

Outlook ItemAdd event not triggered consistently

I'm trying to decide whether the incoming mail is spam or not. Itemadd event works only once. It does not trigger consistently.

How can I fix this problem?

I do following:

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;
    }
}

When i send myself mail, "I'm in" and "I'm inside in predict function" shows only once in debug console.

Upvotes: 0

Views: 785

Answers (2)

Shyam sundar shah
Shyam sundar shah

Reputation: 2523

Working code will be like

 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;
 }
}

Upvotes: 1

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66215

Firstly, your code won't compile - there is no ItemAdd event on the MAPIFolder object. It is exposed by the Items object (returned from the MAPIFolder.Items property).

Secondly, the object raising the events (Items) must remain alive - declare it on the class level, not as a local variable, which gets released by the Garbage Collector when the ThisAddIn_Startup method exits.

Upvotes: 3

Related Questions