Home > Uncategorized > Send an SMS from Windows Phone 7 (WP7/Silverlight)

Send an SMS from Windows Phone 7 (WP7/Silverlight)

This is a simple piece of code that allows you send an SMS from Windows Phone 7 with Silverlight
The XAML is as follows

 
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <TextBlock Height="30" HorizontalAlignment="Left" Margin="29,35,0,0" Name="lblFromName" Text="From (Name)" VerticalAlignment="Top" />
            <TextBox Height="80" HorizontalAlignment="Left" Margin="170,6,0,0" Name="tbFromName" Text="" VerticalAlignment="Top" Width="259" />
            <TextBlock Height="30" HorizontalAlignment="Left" Margin="29,112,0,0" Name="lblFromNumber" Text="From (Number)" VerticalAlignment="Top" />
            <TextBox Height="80" HorizontalAlignment="Left" Margin="170,92,0,0" Name="tbFromNumber" Text="00" VerticalAlignment="Top" Width="259" />
            <TextBlock Height="30" HorizontalAlignment="Left" Margin="29,205,0,0" Name="lblToName" Text="To (Name)" VerticalAlignment="Top" />
            <TextBox Height="80" HorizontalAlignment="Left" Margin="170,178,0,0" Name="tbToName" Text="" VerticalAlignment="Top" Width="259" />
            <TextBlock Height="30" HorizontalAlignment="Left" Margin="33,289,0,0" Name="lblToNumber" Text="To (Number)" VerticalAlignment="Top" />
            <TextBox Height="80" HorizontalAlignment="Left" Margin="170,264,0,0" Name="tbToNumber" Text="00" VerticalAlignment="Top" Width="259" />
            <Button Content="Send SMS" Height="93" HorizontalAlignment="Left" Margin="170,570,0,0" Name="btnSendSMS" VerticalAlignment="Top" Width="239" Click="btnSendSMS_Click" />
            <TextBlock Height="44" HorizontalAlignment="Left" Margin="31,369,0,0" Name="textBlock1" Text="Message" VerticalAlignment="Top" Width="199" />
            <TextBox Height="166" HorizontalAlignment="Left" Margin="33,397,0,0" Name="tbMessage" Text="" VerticalAlignment="Top" Width="374" TextChanged="tbMessage_TextChanged" />
        </Grid>

Then the code behind btnSendSMS_Click is

     private void btnSendSMS_Click(object sender, RoutedEventArgs e)
        {
            var strFromName = tbFromName.Text;
            var strToName = tbToName.Text;
            var strFromNumber = tbFromNumber.Text;
            var strToNumber = tbToNumber.Text;
            var strMessage = tbMessage.Text;
            // Validation
            if (!strToNumber.StartsWith("00") || !strFromNumber.StartsWith("00"))
            {
                MessageBox.Show("Mobile numbers must be written in international format, for example 0044 for the UK, followed by the mobile phone number, without the first zero");
                return;
            }
            if (strFromName.Length  11)
            {
                MessageBox.Show("Use your real name or else the reciepient may not recognize the sender, your name should be between 3 and 11 letters long");
                return;
            }
            if (strFromNumber == strToNumber)
            {
                MessageBox.Show("You cannot send a text message to yourself");
                return;
            }
            if (strMessage.Length < 3)
            {
                MessageBox.Show("Your message is blank, please type a message");
                return;
            }
            sendSmsSoapClient sms = new sendSmsSoapClient();
            sms.SendSmsCompleted +=
                new EventHandler(sms_SendSmsCompleted);
            try
            {
                sms.SendSmsAsync(strFromName, strFromNumber, strToNumber, strMessage, "xx-xx");
            }
            catch (Exception ex)
            {
                MessageBox.Show("SMS failed to send due to: " + ex.Message);
            }
        }

        void sms_SendSmsCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                MessageBox.Show("SMS Sent successfully");
            }
            else
            {
                MessageBox.Show("SMS not sent due to " + e.Error.Message);
            }
        }

Then you need a web service reference to http://www.freebiesms.co.uk/sendsms.asmx called “webservice”

Tip: If you use AffiliateSendSMS API call rather than SendSMS, then you can get paid via your affiliate account on FreebieSMS! 🙂

Advertisement
Categories: Uncategorized
  1. Learner
    July 30, 2011 at 10:44 am

    error CS0246: The type or namespace name ‘sendSmsSoapClient’ could not be found (are you missing a using directive or an assembly reference?)

    when i use above code .

    Like

  2. Learner
    July 30, 2011 at 11:13 am

    Your comment is awaiting moderation.

    error CS0246: The type or namespace name ‘sendSmsSoapClient’ could not be found (are you missing a using directive or an assembly reference?)

    when i use above code .

    Like

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: