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.
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:
- SendSms.Clicked += (sender, e) =>
- {
- var SmsTask = MessagingPlugin.SmsMessenger;
-
- if (SmsTask.CanSendSms)
- SmsTask.SendSms(MsgTo.Text, Message.Text);
-
- };
For making Call:
In CallNo click event add the following code.
- CallNo.Clicked += (sender, e) =>
- {
-
- var PhoneCallTask = MessagingPlugin.PhoneDialer;
- if (PhoneCallTask.CanMakePhoneCall)
- PhoneCallTask.MakePhoneCall(PhoneNumber.Text);
- };
For Sending Email:
In SendEmail click event add the following code.
- SendEmail.Clicked += (sender, e) =>
- {
- var EmailTask = MessagingPlugin.EmailMessenger;
-
- if (EmailTask.CanSendEmail)
- EmailTask.SendEmail(EmailTo.Text, EmailSubject.Text, EmailBody.Text);
- };
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.
It works fine. Complete code is
here.
Happy Coding.