Sending Message, E-mails and Making Calls

We put light upon the task of making your Xamarin app capable of sending SMS, E-Mails and making calls or copying a number to the dialer pad. This is a feature in Xamarin through which we can open other application on the device through an app.

 

Sending an SMS

There are two ways of sending an SMS via App, Using the default application and the other being sending it in the background.

Sending an SMS in Android

To begin, first, add the SEND_SMS permission to the Android manifest.
For this we use a Uri with smsto: containing the Contact no. of the recipient.
Then we use an intent with ActionSendto along with the URI.
The Message Body is included using the PutExtra method.
public void SendMessage()
        {
            SmsManager.Default.SendTextMessage("1234567890",
                null, "Hello from Xamarin.Android", null, null);
            var smsUri = Android.Net.Uri.Parse("smsto:1234567890");
            var smsIntent = new Intent(Intent.ActionSendto, smsUri);
            smsIntent.PutExtra("sms_body", "Hello from Xamarin.Android");
            (Xamarin.Forms.Forms.Context).StartActivity(smsIntent);
        }
Sending an SMS in iOS 
NSUrl is used which contains Contact number of the recipient.
 
"OpenUrl" is called to make the switch to the SMS app.
public void SendMessage()
        {
            var smsTo = NSUrl.FromString("sms:18015551234");
            UIApplication.SharedApplication.OpenUrl(smsTo);
        }
 

Sending an Email

Following are the steps to make your app capable of sending emails.
 

Sending an Email in Android

public void SendEmail()
        {
            var email = new Intent(Android.Content.Intent.ActionSend);
            email.PutExtra(Android.Content.Intent.ExtraEmail,
            new string[] { "person1@xamarin.com", "person2@xamrin.com" });
            email.PutExtra(Android.Content.Intent.ExtraCc,
            new string[] { "person3@xamarin.com" });
            email.PutExtra(Android.Content.Intent.ExtraSubject, "Hello Email");
            email.PutExtra(Android.Content.Intent.ExtraText,
            "Hello from Xamarin.Android");
            email.SetType("message/rfc822");
            (Xamarin.Forms.Forms.Context).StartActivity(email);
        }
 

Sending an Email in iOS

public void SendEmail()
        {
            if (MFMailComposeViewController.CanSendMail)
            {
                mailController = new MFMailComposeViewController();
                mailController.SetToRecipients(new string[] { "john@doe.com" });
                mailController.SetSubject("mail test");
                mailController.SetMessageBody("this is a test", false);
                GetController().PresentViewController(mailController, true, null);
                mailController.Finished += (object s, MFComposeResultEventArgs args) =>
                {
                    Console.WriteLine(args.Result.ToString());
                    args.Controller.DismissViewController(true, null);

                };
            }
        }

Making a Phone-Call
 
Following are the steps to make your app capable of sending emails.
 

Phone-Call in Android

public void MakeCall()
{
   Intent intent = new Intent(Intent.ActionDial, Android.Net.Uri.Parse("tel:" + "Your Phone_number"));
   (Xamarin.Forms.Forms.Context).StartActivity(intent);
}
 
The Intent “ActionDial” is the backbone of this function. A new intent is created using the ActionDial by associating it with the telephone number the call is to be made.
Start Activity then Starts the activity which is responsible for making the switch to the
Dialer app which is default in the mobile set.

Phone-Call in iOS

public void MakeCall()
{
    var url = new NSUrl("tel:" + "9811521033");
    UIApplication.SharedApplication.OpenUrl (url);
}

Similarly, In iOS, like the ActionDial Intent in Android, the NSUrl plays that part of making the mobile number in a URI. 

The URI is passed through UIApplication.SharedApplication to make a move to the Dialer app to make the phone-call.

Find the sample for the same here.