Accessing Call, SMS And Email Service In Xamarin.Forms

Adding phone call, SMS and email feature is easy in Xamarin.Forms. Thanks to Carel Lotz for developing the plugin. His plugin provides three services: Call, SMS and Email. In this post let’s see how to implement all the three services in Xamarin.Forms (PCL) project.

First let’s create simple UI to enter phone number, message and email. Here is my simple UI.

simple UI

Don’t worry about the code, I uploaded it on Github.

First two entries and button is for sending SMS, second two for phone call and remaining are for email purpose.

Let’s add Nuget package. Add this nuget package in your projects (pcl, android, iOS, winphone).

Now let’s implement the services.

For Sending SMS:

In SendSms click event, add the following code:

  1. SendSms.Clicked += (sender, e) =>   
  2. {  
  3.    var SmsTask = MessagingPlugin.SmsMessenger;  
  4.   
  5.    if (SmsTask.CanSendSms)  
  6.    SmsTask.SendSms(MsgTo.Text, Message.Text);  
  7.   
  8. };  
For making Call:

In CallNo click event add the following code.
  1. CallNo.Clicked += (sender, e) =>  
  2. {  
  3.    //Don't forgot to enable ID_CAP_PHONEDAILER on manifest file  
  4.    var PhoneCallTask = MessagingPlugin.PhoneDialer;  
  5.    if (PhoneCallTask.CanMakePhoneCall)  
  6.    PhoneCallTask.MakePhoneCall(PhoneNumber.Text);  
  7. };  
For Sending Email:

In SendEmail click event add the following code.
  1. SendEmail.Clicked += (sender, e) =>   
  2.  {  
  3.        var EmailTask = MessagingPlugin.EmailMessenger;  
  4.   
  5.        if (EmailTask.CanSendEmail)  
  6.             EmailTask.SendEmail(EmailTo.Text, EmailSubject.Text, EmailBody.Text);  
  7.  };  
Here I am applying simple email structure but you can also include complex email structure.  Here'e the sample code.

Tip: Don’t forgot to enable CALL_PHONE and SEND_SMS capabilities in android project and ID_CAP_PHONEDAILER capabilities on windows phone.

Now only this much for code. Let’s build and see the result;

Here we go on my windows phone.

windows phone

It works fine. Complete code is here.

Happy Coding.

 


Similar Articles