locked
How do I intercept SMS's from multiple contacts? My code works fine for one contact. RRS feed

  • Question

  • Hi

    I have written the following code to intercept an SMS from a contact, in my code "160by2.com". Code works fine.
    The application launches itself when an SMS arrives from 160by2.com. SMS's from 160by2 don't go the Inbox also.

    But In my project, I need to create a list of contacts (say BlackList), from which I don't intend to receive the SMS. In one MessageCondition we can only give only one contact, or whatever rule. That will contribute to one entry in Registry. The BlackList will be a file stored on device itself.

    How do I check the BlackList for the incoming SMS without cluttering my Registry with as many as enteries as the number of contacts in the list (also u know, each entry in Registry has to be given a unique ID)?

    As far I have analysed, the presence of an SMS filtering condition in Registry is done by Messaging sub-system of Windows Mobile O.S. Can we do something over there to do something about it?

    Also, if needed, we may load the BlackList into RAM for the moment comparision has to be made, and after comparision, unload the list. But where do I write the code to  load the list in RAM?


        public partial class frmMain : Form
        {
            private MessageInterceptor mi;

            public frmMain()
            {
                InitializeComponent();
            }

            private void frmMain_Load(object sender, EventArgs e)
            {
                RegisterApplication();
            }
      
            private void mnuRegisterApp_Click(object sender, EventArgs e)
            {
                RegisterApplication();
            }

            private void SmsInterceptor_MessageReceived_OnThread(object sender, MessageInterceptorEventArgs e)
            {
                this.BringToFront();
      SmsMessage msg = e.Message as SmsMessage;  
                lblSender.Text = msg.From.Address;
                lblBody.Text = "1 Text message blocked";
               
            }

            private void mnuUnregisterApp_Click(object sender, EventArgs e)
            {
                bool appRegistered = MessageInterceptor.IsApplicationLauncherEnabled("miDemo");
                mi.DisableApplicationLauncher();
                mi.MessageReceived -= SmsInterceptor_MessageReceived_OnThread;
                mi.Dispose();
                if (appRegistered == true)
                    lblBody.Text = "Application UNregistered successfully.";
                else
                    lblBody.Text = "Application could not be unregistered";
            }

            private void RegisterApplication()
            {
                ///Wheather the Message Interceptor application launcher with the given identifier has already been enabled
                bool appRegistered = MessageInterceptor.IsApplicationLauncherEnabled("miDemo");
                if (appRegistered == true)
                {
                    mi = new MessageInterceptor("miDemo", true); //Load the existing settings
                    lblBody.Text = "Application is already registered";
                }
                else
                {
                    //Setup rule and register with application id "miDemo"
                    mi = new MessageInterceptor(InterceptionAction.NotifyAndDelete, true);

                    ///Notifies the intercepting application that an intercepting message has arrived. When the application
                    ///has finished processing the message, Pocket Outlook deletes the original message.
                    mi.MessageCondition = new MessageCondition(MessageProperty.Sender,
    MessagePropertyComparisonType.Equal, "@160by2.com", false);

                    string appPath = Assembly.GetExecutingAssembly().GetName().CodeBase;

                    //Persist the rule in registry for later use. Also stores the application path so that when a matching SMS
                    //is received, the system can auromatically start your application and process the message
                    mi.EnableApplicationLauncher("miDemo", appPath);
                    appRegistered = true;
                    if (appRegistered == true)
                        lblBody.Text = "Application registered successfully.";
                }
                mi.MessageReceived += SmsInterceptor_MessageReceived_OnThread;
            }
        }

    Thanks in advance.
    Tuesday, May 20, 2008 4:17 PM

Answers

  • Hi KumarKush,

    I don't test your code, base on theory, both registry entry can work.

     

    I tried a sample, which create a MessageInterceptor array, each of the array have a condition to intercept sms. Beolow is code:

     

    Code Snippet

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Text;

    using System.Windows.Forms;

    using Microsoft.WindowsMobile.PocketOutlook.MessageInterception;

    using Microsoft.WindowsMobile.PocketOutlook;

    using System.Net;

     

    namespace InteruptSMS

    {

        public partial class Form1 : Form

        {

            MessageInterceptor[] mi = new MessageInterceptor[2];

     

            public Form1()

            {

                InitializeComponent();

            }

     

            private void Form1_Load(object sender, EventArgs e)

            {

                InterceptSMS();

               

            }

     

            public void InterceptSMS()

            {

                string[] testFilter ={ "111", "222" };

     

                if (MessageInterceptor.IsApplicationLauncherEnabled("Intercept_Sms"))

                {

                    mi[0] = new MessageInterceptor("Intercept_Sms");

                    mi[1] = new MessageInterceptor("Intercept_Sms");

                }

                else

                {

                    for (int i = 0; i < testFilter.Length; i++)

                    {

                        //The first array

                        mi[i] = new MessageInterceptor(InterceptionAction.NotifyAndDelete, true);

                        mi[i].MessageCondition = new MessageCondition(MessageProperty.Body,

                         MessagePropertyComparisonType.StartsWith, testFilter[i], true);

                       

                        mi[i].MessageReceived += new MessageInterceptorEventHandler(_SMSCatcher_MessageReceived);

                    }

                 

                }

     

               

            }

     

            void _SMSCatcher_MessageReceived(object sender, MessageInterceptorEventArgs e)

            {

                MessageBox.Show(((SmsMessage)e.Message).From.Address, "Message");

            }

        }

    }

     

     

    Best regards,

    Guang-Ming Bian - MSFT

    Tuesday, May 27, 2008 2:52 AM
  • Hi KumarKush ,

     

    Firstly, MessageInterceptor.EnableApplicationLauncher ("GUID", appPath)  will enable your current application to be launched if it is not already running when a MessageReceived event is raised. Since just for test, I launch my application, I don't include it.

     

    Please look at the sender list, since sender is not phone number , it is email fomat. MessageInterceptor doesn't support it.

    private string[] blackList = { "", "@way2sms.com", "@160by2.com" };

     

    Please try to change blackList to number, like below:

    string[] blackList = { "88", "555" };

     

    I tried a sample like below, it works fine:

    Code Snippet

     

     

    I tried a sample, it works fine:

    public void InterceptSMS1()

    {

    string appPath = Assembly.GetExecutingAssembly().GetName().CodeBase;

    string[] testFilter = { "88", "555" };

    //{ "testing", "222" };

    if (MessageInterceptor.IsApplicationLauncherEnabled("Intercept_Sms"))

    {

    mi[0] = new MessageInterceptor("Intercept_Sms");

    mi[1] = new MessageInterceptor("Intercept_Sms");

    }

    else

    {

    for (int i = 0; i < testFilter.Length; i++)

    {

    //The first array

    mi[i] = new MessageInterceptor(InterceptionAction.NotifyAndDelete, true);

     

    mi[i].MessageCondition = new MessageCondition(MessageProperty.Sender,MessagePropertyComparisonType.EndsWith, testFilter[i], true);

     

    mi[i].EnableApplicationLauncher("Intercept_Sms"+i, appPath);

    mi[i].MessageReceived += new MessageInterceptorEventHandler(_SMSCatcher_MessageReceived);

    }

    }

    }

     

     

     

    Note: EndsWith property value not only contains the filter criteria, it finishes with it.  Like "88", The send phone number must end with "88", like "2939388", your application can intercept it. if number is "8833",  it will not intercept it.   So you must pay attention to it.

     

    Best regards,

    Guang-Ming Bian - MSFT

    Thursday, May 29, 2008 10:15 AM

All replies

  • Hi

    Would it be wise on my part to make multiple enteries in registry, having mobile number in the blacklist appended at the end of the applicationLaunchID I have already created, and keeping the appPath same for all enteries?

    Thanks
    Tuesday, May 20, 2008 4:20 PM
  • Hi,

     

    There are limitations in the MessageIntercepter class in the .NET Compact Framework as you can see. To answer the question in the title of this thread (i.e. to avoid using multiple objects of the MessageIntercepter class), you can create a Message Rule Client (that implements the IMailRuleClient interface), as explained in the following article:

     

     Receiving SMS Messages Inside a Managed Application

     

     

    Best Regards

    Chunsheng Tang

     

     

    Friday, May 23, 2008 2:56 AM
  • Hi Chunsheng Tang

    I read the link ("Receiving SMS Messages Inside a Managed Application") you posted me. It seems promising. I am using C# 2005 and the code sample is in C++. I will try to use the code in C#.

    Well, I tried to create two registry values with different applicationLaunchID's. I created different rules for them, but the path of the application being the same. After a lot of effort, I was able to create two different keys under Rules in registry. I could see both of them in Remote Registry. Both of them having different senders to filter. Now, the problem is that SMS's from the sender specified in the second entry in the registry under Rules are blocked while SMS's from the first one are still not being intercepted.
    Is it so that only one registry entry is functional?

    Anyways, here is the code, just for testing. Please comment upon it
    private void RegisterApplication()
            {
                ///Wheather the Message Interceptor application launcher with the given identifier has already been enabled
                bool appRegistered = MessageInterceptor.IsApplicationLauncherEnabled("miDemo");
                if (appRegistered == true)
                {
                    mi = new MessageInterceptor("miDemo"); //Load the existing settings
                    lblBody.Text = "Application is already registered";
                }
                else
                {
                    //Setup rule and register with application id "miDemo"
                    mi = new MessageInterceptor(InterceptionAction.NotifyAndDelete, true);


                     mi.MessageCondition = new MessageCondition(MessageProperty.Sender,
    MessagePropertyComparisonType.EndsWith, "@160by2.com", true);

                    string appPath = Assembly.GetExecutingAssembly().GetName().CodeBase;

                    //Persist the rule in registry for later use. Also stores the application path so that when a matching SMS
                    //is received, the system can auromatically start your application and process the message
                    mi.EnableApplicationLauncher("miDemo", appPath);
                    appRegistered = true;
                    if (appRegistered == true)
                        lblBody.Text = "Application registered successfully.";
                }

              
                appRegistered = MessageInterceptor.IsApplicationLauncherEnabled("miDemo1");
                if (appRegistered == true)
                {
                    mi = new MessageInterceptor("miDemo1"); //Load the existing settings
                    lblBody.Text = "Application is already registered";
                }
                else
                {
                   
                    mi = new MessageInterceptor(InterceptionAction.NotifyAndDelete, true);
                    mi.MessageCondition = new MessageCondition(MessageProperty.Sender,
    MessagePropertyComparisonType.EndsWith, "@way2sms.com", true);

                    string appPath = Assembly.GetExecutingAssembly().GetName().CodeBase;

                    //Persist the rule in registry for later use. Also stores the application path so that when a matching SMS
                    //is received, the system can auromatically start your application and process the message
                    mi.EnableApplicationLauncher("miDemo1", appPath);
                    appRegistered = true;
                    if (appRegistered == true)
                        lblBody.Text = "Application registered successfully.";
                }

    Infact I tried phone numbers in place of web-sites.

    Thanks and regards.
    Friday, May 23, 2008 5:34 PM
  • Hi KumarKush,

    I don't test your code, base on theory, both registry entry can work.

     

    I tried a sample, which create a MessageInterceptor array, each of the array have a condition to intercept sms. Beolow is code:

     

    Code Snippet

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Text;

    using System.Windows.Forms;

    using Microsoft.WindowsMobile.PocketOutlook.MessageInterception;

    using Microsoft.WindowsMobile.PocketOutlook;

    using System.Net;

     

    namespace InteruptSMS

    {

        public partial class Form1 : Form

        {

            MessageInterceptor[] mi = new MessageInterceptor[2];

     

            public Form1()

            {

                InitializeComponent();

            }

     

            private void Form1_Load(object sender, EventArgs e)

            {

                InterceptSMS();

               

            }

     

            public void InterceptSMS()

            {

                string[] testFilter ={ "111", "222" };

     

                if (MessageInterceptor.IsApplicationLauncherEnabled("Intercept_Sms"))

                {

                    mi[0] = new MessageInterceptor("Intercept_Sms");

                    mi[1] = new MessageInterceptor("Intercept_Sms");

                }

                else

                {

                    for (int i = 0; i < testFilter.Length; i++)

                    {

                        //The first array

                        mi[i] = new MessageInterceptor(InterceptionAction.NotifyAndDelete, true);

                        mi[i].MessageCondition = new MessageCondition(MessageProperty.Body,

                         MessagePropertyComparisonType.StartsWith, testFilter[i], true);

                       

                        mi[i].MessageReceived += new MessageInterceptorEventHandler(_SMSCatcher_MessageReceived);

                    }

                 

                }

     

               

            }

     

            void _SMSCatcher_MessageReceived(object sender, MessageInterceptorEventArgs e)

            {

                MessageBox.Show(((SmsMessage)e.Message).From.Address, "Message");

            }

        }

    }

     

     

    Best regards,

    Guang-Ming Bian - MSFT

    Tuesday, May 27, 2008 2:52 AM
  • Hi Guang-Ming Bian

    Using your code, I was able to create rules in Inbox\Rules with different GUID's successfully. But I don't understand that though your code does not have MessageInterceptor.EnableApplicationLauncher ("GUID", appPath), still the rules are being registered in Regsitry. But when I used the technique used in ur code in my code (which has the EnableApplicationLaucher), no entries in the Registry are created. Here is the code:

    private string[] blackList = { "<ActualPhoneNumber>", "@way2sms.com", "@160by2.com" };
    ---------------------------------------------------------------------------------------------
    private void RegisterApplication ()
    {
                bool appRegistered = MessageInterceptor.IsApplicationLauncherEnabled("miDemo");

                if (appRegistered == true)
                {
                    lblBody.Text = "Application is already registered";
                    for (int i = 0; i < blackList.Length; i++)
                        miIdea = new MessageInterceptor("miDemo"); //Load the existing settings
                }
                else
                {
                    string appPath = Assembly.GetExecutingAssembly().GetName().CodeBase;
                    for (int i = 0; i < blackList.Length; i++)
                    {
                        miIdea = new MessageInterceptor(InterceptionAction.NotifyAndDelete, true);
                        miIdea.MessageCondition = new MessageCondition(MessageProperty.Sender,
    MessagePropertyComparisonType.EndsWith, blackListIdea, true);
                       
                        miIdea.EnableApplicationLauncher("miDemo", appPath);
                        miIdea.MessageReceived += SmsInterceptor_MessageReceived_OnThread;
                    }
    }

    One more thing, the SMS's are not intercepted by using this code, though entries are there in registry. When I run your code, the entries created by my code are also visible. Can you explain me what's happening?

    Thanks and regards.
    Wednesday, May 28, 2008 2:28 PM
  • Hi Guang-Ming Bian

    The bulbs in front of mi are actually array indices [ i ]. I don't know why are they are being displayed as bulbs.
    Wednesday, May 28, 2008 2:38 PM
  • Hi KumarKush ,

     

    Firstly, MessageInterceptor.EnableApplicationLauncher ("GUID", appPath)  will enable your current application to be launched if it is not already running when a MessageReceived event is raised. Since just for test, I launch my application, I don't include it.

     

    Please look at the sender list, since sender is not phone number , it is email fomat. MessageInterceptor doesn't support it.

    private string[] blackList = { "", "@way2sms.com", "@160by2.com" };

     

    Please try to change blackList to number, like below:

    string[] blackList = { "88", "555" };

     

    I tried a sample like below, it works fine:

    Code Snippet

     

     

    I tried a sample, it works fine:

    public void InterceptSMS1()

    {

    string appPath = Assembly.GetExecutingAssembly().GetName().CodeBase;

    string[] testFilter = { "88", "555" };

    //{ "testing", "222" };

    if (MessageInterceptor.IsApplicationLauncherEnabled("Intercept_Sms"))

    {

    mi[0] = new MessageInterceptor("Intercept_Sms");

    mi[1] = new MessageInterceptor("Intercept_Sms");

    }

    else

    {

    for (int i = 0; i < testFilter.Length; i++)

    {

    //The first array

    mi[i] = new MessageInterceptor(InterceptionAction.NotifyAndDelete, true);

     

    mi[i].MessageCondition = new MessageCondition(MessageProperty.Sender,MessagePropertyComparisonType.EndsWith, testFilter[i], true);

     

    mi[i].EnableApplicationLauncher("Intercept_Sms"+i, appPath);

    mi[i].MessageReceived += new MessageInterceptorEventHandler(_SMSCatcher_MessageReceived);

    }

    }

    }

     

     

     

    Note: EndsWith property value not only contains the filter criteria, it finishes with it.  Like "88", The send phone number must end with "88", like "2939388", your application can intercept it. if number is "8833",  it will not intercept it.   So you must pay attention to it.

     

    Best regards,

    Guang-Ming Bian - MSFT

    Thursday, May 29, 2008 10:15 AM