Click here to Skip to main content
15,888,279 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I have created add-in to read outlook incoming mails.But My issue is Addin working for the first time of outlook opening.

Example: If I close and open outlook , for the first incoming mail its working(showing my custom message), for second mail onwards its not showing custom messge.

I repeated this process. Only first time its working.

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

C#
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
private void InboxFolderItemAdded(object Item)
{
    if (Item is Outlook.MailItem)
    {       
        // New mail item in inbox folder
        MessageBox.Show("you got mail");
    }
}


Please help me.

Thanks in advance.
Posted

1 solution

check below sample from MSDN[^]
C#
Outlook.NameSpace outlookNameSpace;
Outlook.MAPIFolder inbox;
Outlook.Items items;

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    outlookNameSpace = this.Application.GetNamespace("MAPI");
    inbox = outlookNameSpace.GetDefaultFolder(
            Microsoft.Office.Interop.Outlook.
            OlDefaultFolders.olFolderInbox);

    items = inbox.Items;
    items.ItemAdd +=
        new Outlook.ItemsEvents_ItemAddEventHandler(items_ItemAdd);
}

void items_ItemAdd(object Item)
{
    string filter = "USED CARS";
    Outlook.MailItem mail = (Outlook.MailItem)Item;
    if (Item != null)
    {
        if (mail.MessageClass == "IPM.Note" &&
                   mail.Subject.ToUpper().Contains(filter.ToUpper()))
        {
            mail.Move(outlookNameSpace.GetDefaultFolder(
                Microsoft.Office.Interop.Outlook.
                OlDefaultFolders.olFolderJunk));
        }
    }

}


Items object in your case will be garbage collected after leaving that block of code, you can create class level variable(as MSDN sample code) and then hook the event to that object.
 
Share this answer
 
Comments
Prasannala 6-Nov-14 7:10am    
Thank you for giving solution. Its working.
DamithSL 6-Nov-14 8:21am    
you are welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900