none
Unreaded mail can't be get in NemailEx event in outlook c# RRS feed

  • Question

    • i want delete attachment when new mail receiving.....
    • so i use NewmailEx event.....and also tried Newmail event.....
    • i open the outlook --> send a mail to my outlook mail -->the event get fired -->but the received mail is not shown in my unreaded mail i.e, c# coding did't get the received mail (this happen only for the first mail..after closing and opening of outlook..from the second mail the operation works correctly)
    • i attach my code below
     private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                this.Application.NewMail += new Microsoft.Office.Interop.Outlook
                   .ApplicationEvents_11_NewMailEventHandler(ThisApplication_NewMail);
            }
            private void ThisApplication_NewMail()
            {
                Outlook.MAPIFolder inBox = this.Application.ActiveExplorer()
                    .Session.GetDefaultFolder(Outlook
                    .OlDefaultFolders.olFolderInbox);
                Outlook.Items inBoxItems = inBox.Items;
                Outlook.MailItem newEmail = null;
                inBoxItems = inBoxItems.Restrict("[Unread] = true");
    
                foreach (object collectionItem in inBoxItems)
                {
                    newEmail = collectionItem as Outlook.MailItem;
                    if (newEmail != null)
                    {
                        if (newEmail.Attachments.Count > 0)
                        {
                            for (int i = 1; i <= newEmail
                               .Attachments.Count; i++)
                            {
                                newEmail.Attachments[1].Delete();
    
                            }
                        }
                    }
                }
            }

    • pls help me to read he first mail.....when outlook was open...
    • thank u in advance
    Thursday, May 7, 2015 4:31 PM

Answers

All replies

    1. I am trying to develop a addin for outook in that i want remove attachment when new mail is received....
    2. so i call my function on NewMailEx + event .....it working fine.....
    3. in that function i try to find unreaded mails in outlook inbox....from that i remove the attach ments
    4. but my problem is......when i open my outlook the first mail i received is not showing in inbox(not in outlook in c# code)...so i can't remove the attachment from that mail......from the second mail(after first mail)...i can got the receiving mail.....so ican remove the attachments.....
    5. whenever i close and reopen my outlook the first mail i received give this problrm......
     private void Application_NewMailEx(object Item)
            {
                string senderEmailid = string.Empty;
                outlookNameSpace = this.Application.GetNamespace("MAPI");
                Outlook.Application myApp = new Outlook.Application();
                Outlook.NameSpace mapiNameSpace = myApp.GetNamespace("MAPI");
                Outlook.MAPIFolder myInbox = mapiNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
                Outlook.Attachments attachments;
                int test = myInbox.Items.Count;
                
                Microsoft.Office.Interop.Outlook.Recipients recipients = ((Outlook.MailItem)myInbox.Items[test]).Recipients;
                Outlook.Items unreadItems = myInbox.Items.Restrict("[Unread]=true");
     if (unreadItems.Count > 0)
                {
                    
                    foreach (Outlook.MailItem mail in unreadItems)
    
                    {
      Outlook.Recipient recip;
                        Outlook.ExchangeUser exUser;
                        string sAddress;
                        
                        if (mail.SenderEmailType.ToLower() == "ex")
                        {
                            recip = Globals.ThisAddIn.Application.GetNamespace("MAPI").CreateRecipient(mail.SenderEmailAddress);
                            exUser = recip.AddressEntry.GetExchangeUser();
                            sAddress = exUser.PrimarySmtpAddress;
                        }
                        else
                        {
                            sAddress = mail.SenderEmailAddress.Replace("'", "");
                        }
     string[] emailAddressPart = sAddress.Split('@');
                        string strSenderDomain = emailAddressPart[1];
                        if (lstDomain.Any(item => item.Contains(strSenderDomain)))
                        {
    
    
                            attachments = mail.Attachments;
                            int nAttachmentCount = mail.Attachments.Count;
                            if (nAttachmentCount > 0)
                            {
                                int j = nAttachmentCount;
    for (int i = 1; i <= nAttachmentCount; i++)
                                {
     mail.Attachments[j].Delete();
      j--;
                                }
    
                            }
                        }
    
                    }
                }           
            }

    6. at the first mail received there is no unreaded mail.

    pls help.....

    thanks in advance.....


    Tuesday, May 5, 2015 7:56 AM
  • The NewMailEx event of the Application class is not the best place for searching unread emails. This event fires once for every received item that is processed by Microsoft Outlook. The item can be one of several different item types, for example, MailItem, MeetingItem, or SharingItem. The EntryIDsCollection string contains the Entry ID that corresponds to that item. The alternative way is to handle the ItemAdd event of the Items class.

    Instead, you can wait until Outlook finishes synchronizing items and run your code for searching unread emails. The SyncEnd event of the SyncObject class is fired immediately after Microsoft Outlook finishes synchronizing a user’s folders using the specified Send/Receive group.

    Also you may consider handling the Startup or MAPILogonComplete events. But they are fired before the synchronization is completed. Consider using a timer to run the procedure a bit after Outlook is started.

    You can read about possible ways of handling incoming emails in the following series of articles:

    Outlook NewMail event unleashed: the challenge (NewMail, NewMailEx, ItemAdd)

    Outlook NewMail event: solution options

    Outlook NewMail event and Extended MAPI: C# example

    Outlook NewMail unleashed: writing a working solution (C# example)


    Tuesday, May 5, 2015 11:16 AM
  • Also I'd recommend breaking the chain of calls and delaclaring each property or method call on a separate line of code. Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it. This is particularly important if your add-in attempts to enumerate more than 256 Outlook items in a collection that is stored on a Microsoft Exchange Server. If you do not release these objects in a timely manner, you can reach the limit imposed by Exchange on the maximum number of items opened at any one time. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. Read more about that in the Systematically Releasing Objects article in MSDN.
    Tuesday, May 5, 2015 12:16 PM
  • i try the syncend event ...but till my problemis not solved.....pls help...
    Wednesday, May 6, 2015 5:34 AM
  • What code do you use now? What problem are you talking about? Could you be more specific?
    Wednesday, May 6, 2015 6:14 AM
  • hello,

        In startup i use below code......

      mysync.SyncEnd += new Microsoft.Office.Interop.Outlook.SyncObjectEvents_SyncEndEventHandler(mysync_SyncEnd);           
                mysync.SyncStart += new Microsoft.Office.Interop.Outlook.SyncObjectEvents_SyncStartEventHandler(mysync_SyncEnd);

    in mysync_end function i use the code as follows..........

            private void mysync_SyncEnd()
            {
                string senderEmailid = string.Empty;
                outlookNameSpace = this.Application.GetNamespace("MAPI");
                Outlook.Application myApp = new Outlook.Application();
                Outlook.NameSpace mapiNameSpace = myApp.GetNamespace("MAPI");
                Outlook.MAPIFolder myInbox = mapiNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
                Microsoft.Office.Interop.Outlook.Items inboxItems = myInbox.Items;
                int count = inboxItems.Count;
                Outlook.Attachments attachments;
                int test = myInbox.Items.Count;
                senderEmailid = ((Outlook.MailItem)myInbox.Items[test]).SenderEmailAddress;
                Microsoft.Office.Interop.Outlook.Recipients recipients = ((Outlook.MailItem)myInbox.Items[test]).Recipients;
                Outlook.Items unreadItems = myInbox.Items.Restrict("[Unread]=true");
               
                if (unreadItems.Count > 0)
                {
    
                    foreach (Outlook.MailItem mail in unreadItems)
                    {
                       
                        Outlook.Recipient recip;
                        Outlook.ExchangeUser exUser;
                        string sAddress;
                       
                        if (mail.SenderEmailType.ToLower() == "ex")
                        {
                            recip = Globals.ThisAddIn.Application.GetNamespace("MAPI").CreateRecipient(mail.SenderEmailAddress);
                            exUser = recip.AddressEntry.GetExchangeUser();
                            sAddress = exUser.PrimarySmtpAddress;
                        }
                        else
                        {
                            sAddress = mail.SenderEmailAddress.Replace("'", "");
                        }
                        
                        string[] emailAddressPart = sAddress.Split('@');
                        string strSenderDomain = emailAddressPart[1];
                        if (lstDomain.Any(item => item.Contains(strSenderDomain)))
                        {
    
    
                            attachments = mail.Attachments;
                            int nAttachmentCount = mail.Attachments.Count;
                            if (nAttachmentCount > 0)
                            {
                                int j = nAttachmentCount;
                                
                              
                                for (int i = 1; i <= nAttachmentCount; i++)
                                {
                                   
                                    mail.Attachments[j].Delete();
                                   
    
                                    j--;
                                }
    
                            }
                        }
    
                    }
                }
            }

    i open outlook and there is no unread mails(but have some read mails).....i send a mail to my outlook .......send/receive was performed.....it go to my mysync_SyncEnd() function...but in my inbox the mail send by me was not found. so my attachment not get deleted(only for the first mail after that the function found the mails and remove the attachments).....

    pls help......

    Wednesday, May 6, 2015 6:26 AM
  • What code do you use now? What problem are you talking about? Could you be more specific?

    hello,

        In startup i use below code......

    mysync.SyncEnd += new Microsoft.Office.Interop.Outlook.SyncObjectEvents_SyncEndEventHandler(mysync_SyncEnd);           
                mysync.SyncStart += new Microsoft.Office.Interop.Outlook.SyncObjectEvents_SyncStartEventHandler(mysync_SyncEnd);

    in mysync_end function i use the code as follows..........
     private void mysync_SyncEnd()
            {
                string senderEmailid = string.Empty;
                outlookNameSpace = this.Application.GetNamespace("MAPI");
                Outlook.Application myApp = new Outlook.Application();
                Outlook.NameSpace mapiNameSpace = myApp.GetNamespace("MAPI");
                Outlook.MAPIFolder myInbox = mapiNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
                Microsoft.Office.Interop.Outlook.Items inboxItems = myInbox.Items;
                int count = inboxItems.Count;
                Outlook.Attachments attachments;
                int test = myInbox.Items.Count;
                senderEmailid = ((Outlook.MailItem)myInbox.Items[test]).SenderEmailAddress;
                Microsoft.Office.Interop.Outlook.Recipients recipients = ((Outlook.MailItem)myInbox.Items[test]).Recipients;
                Outlook.Items unreadItems = myInbox.Items.Restrict("[Unread]=true");
               
                if (unreadItems.Count > 0)
                {
    
                    foreach (Outlook.MailItem mail in unreadItems)
                    {
                       
                        Outlook.Recipient recip;
                        Outlook.ExchangeUser exUser;
                        string sAddress;
                       
                        if (mail.SenderEmailType.ToLower() == "ex")
                        {
                            recip = Globals.ThisAddIn.Application.GetNamespace("MAPI").CreateRecipient(mail.SenderEmailAddress);
                            exUser = recip.AddressEntry.GetExchangeUser();
                            sAddress = exUser.PrimarySmtpAddress;
                        }
                        else
                        {
                            sAddress = mail.SenderEmailAddress.Replace("'", "");
                        }
                        
                        string[] emailAddressPart = sAddress.Split('@');
                        string strSenderDomain = emailAddressPart[1];
                        if (lstDomain.Any(item => item.Contains(strSenderDomain)))
                        {
    
    
                            attachments = mail.Attachments;
                            int nAttachmentCount = mail.Attachments.Count;
                            if (nAttachmentCount > 0)
                            {
                                int j = nAttachmentCount;
                                
                              
                                for (int i = 1; i <= nAttachmentCount; i++)
                                {
                                   
                                    mail.Attachments[j].Delete();
                                   
    
                                    j--;
                                }
    
                            }
                        }
    
                    }
                }
            }

    i open outlook and there is no unread mails(but have some read mails).....i send a mail to my outlook .......send/receive was performed.....it go to my mysync_SyncEnd() function...but in my inbox the mail send by me was not found. so my attachment not get deleted(only for the first mail after that the function found the mails and remove the attachments).....

    pls help......



    Wednesday, May 6, 2015 7:38 AM
  • Why do you need to create a new Outlook Application instance in the SyncEnd event handler?

    Instead, you need to use the Application property of the add-in class.

    Did you try to debug the code and see what happens under the hood?

    Also I'd suggest reading articles I mentioned in my previous post there.

    Wednesday, May 6, 2015 7:49 AM
  • till my problem was not get cleared.........

    below i attach my full code.....

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;
    using Outlook = Microsoft.Office.Interop.Outlook;
    using Office = Microsoft.Office.Core;
    using System.Windows.Forms;
    using System.Xml;
    using System.Collections;
    
    
    
    
    namespace Secova_Email_Add_in
    {
        public partial class ThisAddIn
        {
            
            Outlook.NameSpace outlookNameSpace;
            
            private Microsoft.Office.Interop.Outlook.SyncObject mysync;
    
            List<string> lstDomain;
           
            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
    
                string path;
                path = new Uri(System.IO.Path.GetDirectoryName(
                   System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)).LocalPath;
               
                lstDomain = new List<string>();
    
                XmlDocument xmlDoc = new XmlDocument();
              
                
                if (!System.IO.File.Exists(path+"\\DomainList.xml"))
                {
                    
                    MessageBox.Show("Unable to load DomainList.xml.\n Please check your Administrator.\n Add-in Aborted" + path,"Secova SharePoin Add-in",MessageBoxButtons.OK,MessageBoxIcon.Error);
                    return;
                }
                xmlDoc.Load(path + "\\DomainList.xml");
                XmlNodeList nodeList = xmlDoc.DocumentElement.SelectNodes("/Domains/Domain");
                string proName = "";
    
                if (nodeList.Count==0)
                {
                    MessageBox.Show("No Domain name identified in DomainList.xml.\n Please check your Administrator.\n" + path);
                    return;
                }
    
                foreach (XmlNode node in nodeList)
                {
    
                    proName = node.SelectSingleNode("Name").InnerText;
                    lstDomain.Add(proName);
                }
                Outlook.MAPIFolder inbox = Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
               
                inbox.Items.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(Application_NewMailEx);
                Application.NewMail += new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_NewMailEventHandler(mysync_SyncEnd);
                Application.NewMailEx += new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_NewMailExEventHandler(mysync_SyncEnd);
                mysync = Application.Session.SyncObjects[1];
                mysync.SyncEnd += new Microsoft.Office.Interop.Outlook.SyncObjectEvents_SyncEndEventHandler(mysync_SyncEnd);
                mysync.SyncStart += new Microsoft.Office.Interop.Outlook.SyncObjectEvents_SyncStartEventHandler(mysync_SyncEnd);
               
            }
    
    
            
            private void InternalStartup()
            {
                this.Startup += new System.EventHandler(ThisAddIn_Startup);
                this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
            }
    
          
            private string GetSenderSMTPAddress(Outlook.MailItem mail)
            {
                string PR_SMTP_ADDRESS =@"http://schemas.microsoft.com/mapi/proptag/0x39FE001E";
                if (mail == null)
                {
                    throw new ArgumentNullException();
                }
                if (mail.SenderEmailType == "EX")
                {
                    Outlook.AddressEntry sender =
                        mail.Sender;
                    if (sender != null)
                    {
                        
                            Outlook.OlAddressEntryUserType.
                            olExchangeUserAddressEntry
                            || sender.AddressEntryUserType ==
                            Outlook.OlAddressEntryUserType.
                            olExchangeRemoteUserAddressEntry)
                        {
                            
                            Outlook.ExchangeUser exchUser =
                                sender.GetExchangeUser();
                            if (exchUser != null)
                            {
                                return exchUser.PrimarySmtpAddress;
                            }
                            else
                            {
                                return null;
                            }
                        }
                        else
                        {
                            return sender.PropertyAccessor.GetProperty(
                                PR_SMTP_ADDRESS) as string;
                        }
                    }
                    else
                    {
                        return null;
                    }
                }
                else
                {
                    return mail.SenderEmailAddress;
                }
            }
           
            private void mysync_SyncEnd()
            {
                string senderEmailid = string.Empty;
                //outlookNameSpace = this.Application.GetNamespace("MAPI");
                //Outlook.Application myApp = new Outlook.Application();
                //Outlook.NameSpace mapiNameSpace = myApp.GetNamespace("MAPI");
                //Outlook.MAPIFolder myInbox = mapiNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
                Outlook.MAPIFolder myInbox = Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
                Microsoft.Office.Interop.Outlook.Items inboxItems = myInbox.Items;
                int count = inboxItems.Count;
                Outlook.Attachments attachments;
                int test = myInbox.Items.Count;
                senderEmailid = ((Outlook.MailItem)myInbox.Items[test]).SenderEmailAddress;
                Microsoft.Office.Interop.Outlook.Recipients recipients = ((Outlook.MailItem)myInbox.Items[test]).Recipients;
                Outlook.Items unreadItems = myInbox.Items.Restrict("[Unread]=true");          
                if (unreadItems.Count > 0)
                {
    
                    foreach (Outlook.MailItem mail in unreadItems)
                    {
                        Outlook.Recipient recip;
                        Outlook.ExchangeUser exUser;
                        string sAddress;                    
                        if (mail.SenderEmailType.ToLower() == "ex")
                        {
                            recip = Globals.ThisAddIn.Application.GetNamespace("MAPI").CreateRecipient(mail.SenderEmailAddress);
                            exUser = recip.AddressEntry.GetExchangeUser();
                            sAddress = exUser.PrimarySmtpAddress;
                        }
                        else
                        {
                            sAddress = mail.SenderEmailAddress.Replace("'", "");
                        }
                       
                        string[] emailAddressPart = sAddress.Split('@');
                        string strSenderDomain = emailAddressPart[1];
                        if (lstDomain.Any(item => item.Contains(strSenderDomain)))
                        {
    
    
                            attachments = mail.Attachments;
                            int nAttachmentCount = mail.Attachments.Count;
                            if (nAttachmentCount > 0)
                            {
                                int j = nAttachmentCount;
                               
                                for (int i = 1; i <= nAttachmentCount; i++)
                                {
                                   
                                    mail.Attachments[j].Delete();
                                    
                                    j--;
                                }
    
                            }
                        }
    
                    }
                }
            }
         
          
        }
    }
    

    pls help me......

    Wednesday, May 6, 2015 9:51 AM
  • till my problem was not get cleared.........

    below i attach my full code.....

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;
    using Outlook = Microsoft.Office.Interop.Outlook;
    using Office = Microsoft.Office.Core;
    using System.Windows.Forms;
    using System.Xml;
    using System.Collections;
    
    
    
    
    namespace Secova_Email_Add_in
    {
        public partial class ThisAddIn
        {
            
            Outlook.NameSpace outlookNameSpace;
            
            private Microsoft.Office.Interop.Outlook.SyncObject mysync;
    
            List<string> lstDomain;
           
            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
    
                string path;
                path = new Uri(System.IO.Path.GetDirectoryName(
                   System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)).LocalPath;
               
                lstDomain = new List<string>();
    
                XmlDocument xmlDoc = new XmlDocument();
              
                
                if (!System.IO.File.Exists(path+"\\DomainList.xml"))
                {
                    
                    MessageBox.Show("Unable to load DomainList.xml.\n Please check your Administrator.\n Add-in Aborted" + path,"Secova SharePoin Add-in",MessageBoxButtons.OK,MessageBoxIcon.Error);
                    return;
                }
                xmlDoc.Load(path + "\\DomainList.xml");
                XmlNodeList nodeList = xmlDoc.DocumentElement.SelectNodes("/Domains/Domain");
                string proName = "";
    
                if (nodeList.Count==0)
                {
                    MessageBox.Show("No Domain name identified in DomainList.xml.\n Please check your Administrator.\n" + path);
                    return;
                }
    
                foreach (XmlNode node in nodeList)
                {
    
                    proName = node.SelectSingleNode("Name").InnerText;
                    lstDomain.Add(proName);
                }
                Outlook.MAPIFolder inbox = Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
               
                inbox.Items.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(Application_NewMailEx);
                Application.NewMail += new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_NewMailEventHandler(mysync_SyncEnd);
                Application.NewMailEx += new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_NewMailExEventHandler(mysync_SyncEnd);
                mysync = Application.Session.SyncObjects[1];
                mysync.SyncEnd += new Microsoft.Office.Interop.Outlook.SyncObjectEvents_SyncEndEventHandler(mysync_SyncEnd);
                mysync.SyncStart += new Microsoft.Office.Interop.Outlook.SyncObjectEvents_SyncStartEventHandler(mysync_SyncEnd);
               
            }
    
    
            
            private void InternalStartup()
            {
                this.Startup += new System.EventHandler(ThisAddIn_Startup);
                this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
            }
    
          
            private string GetSenderSMTPAddress(Outlook.MailItem mail)
            {
                string PR_SMTP_ADDRESS =@"http://schemas.microsoft.com/mapi/proptag/0x39FE001E";
                if (mail == null)
                {
                    throw new ArgumentNullException();
                }
                if (mail.SenderEmailType == "EX")
                {
                    Outlook.AddressEntry sender =
                        mail.Sender;
                    if (sender != null)
                    {
                        
                            Outlook.OlAddressEntryUserType.
                            olExchangeUserAddressEntry
                            || sender.AddressEntryUserType ==
                            Outlook.OlAddressEntryUserType.
                            olExchangeRemoteUserAddressEntry)
                        {
                            
                            Outlook.ExchangeUser exchUser =
                                sender.GetExchangeUser();
                            if (exchUser != null)
                            {
                                return exchUser.PrimarySmtpAddress;
                            }
                            else
                            {
                                return null;
                            }
                        }
                        else
                        {
                            return sender.PropertyAccessor.GetProperty(
                                PR_SMTP_ADDRESS) as string;
                        }
                    }
                    else
                    {
                        return null;
                    }
                }
                else
                {
                    return mail.SenderEmailAddress;
                }
            }
           
            private void mysync_SyncEnd()
            {
                string senderEmailid = string.Empty;
                //outlookNameSpace = this.Application.GetNamespace("MAPI");
                //Outlook.Application myApp = new Outlook.Application();
                //Outlook.NameSpace mapiNameSpace = myApp.GetNamespace("MAPI");
                //Outlook.MAPIFolder myInbox = mapiNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
                Outlook.MAPIFolder myInbox = Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
                Microsoft.Office.Interop.Outlook.Items inboxItems = myInbox.Items;
                int count = inboxItems.Count;
                Outlook.Attachments attachments;
                int test = myInbox.Items.Count;
                senderEmailid = ((Outlook.MailItem)myInbox.Items[test]).SenderEmailAddress;
                Microsoft.Office.Interop.Outlook.Recipients recipients = ((Outlook.MailItem)myInbox.Items[test]).Recipients;
                Outlook.Items unreadItems = myInbox.Items.Restrict("[Unread]=true");          
                if (unreadItems.Count > 0)
                {
    
                    foreach (Outlook.MailItem mail in unreadItems)
                    {
                        Outlook.Recipient recip;
                        Outlook.ExchangeUser exUser;
                        string sAddress;                    
                        if (mail.SenderEmailType.ToLower() == "ex")
                        {
                            recip = Globals.ThisAddIn.Application.GetNamespace("MAPI").CreateRecipient(mail.SenderEmailAddress);
                            exUser = recip.AddressEntry.GetExchangeUser();
                            sAddress = exUser.PrimarySmtpAddress;
                        }
                        else
                        {
                            sAddress = mail.SenderEmailAddress.Replace("'", "");
                        }
                       
                        string[] emailAddressPart = sAddress.Split('@');
                        string strSenderDomain = emailAddressPart[1];
                        if (lstDomain.Any(item => item.Contains(strSenderDomain)))
                        {
    
    
                            attachments = mail.Attachments;
                            int nAttachmentCount = mail.Attachments.Count;
                            if (nAttachmentCount > 0)
                            {
                                int j = nAttachmentCount;
                               
                                for (int i = 1; i <= nAttachmentCount; i++)
                                {
                                   
                                    mail.Attachments[j].Delete();
                                    
                                    j--;
                                }
    
                            }
                        }
    
                    }
                }
            }
         
          
        }
    }
    Pls help.......
    Wednesday, May 6, 2015 10:39 AM
  • It looks like you use the NewMail event, but not the NewMailEx one.

    The NewMailEx event fires when a new message arrives in the Inbox and before client rule processing occurs. You can use the Entry ID returned in the EntryIDCollection array to call the NameSpace.GetItemFromID method and process the item. Use this method with caution to minimize the impact on Outlook performance. However, depending on the setup on the client computer, after a new message arrives in the Inbox, processes like spam filtering and client rules that move the new message from the Inbox to another folder can occur asynchronously. You should not assume that after these events fire, you will always get a one-item increase in the number of items in the Inbox.

    You may also find the following series of articles helpful:

    • Marked as answer by RSBrothers Friday, May 8, 2015 11:48 AM
    Thursday, May 7, 2015 4:38 PM