Introduction
This article demonstrates how to make it possible to make a phone call, send an SMS, or send an e-mail using the default messaging applications in cross-platform API. These features are not listed by default in Xamarin.Forms. So, we need to install a plug-in for this.
NuGet package - search for "Xam.Plugins.Messaging"
Messaging plug-in details
Make a phone call, send an SMS or send an e-mail using the default messaging applications on different mobile platforms.
API usage
The Messaging Plug-in makes use of IEmailTask, ISmsTask, and IPhoneCalTask abstractions to send an e-mail, send an SMS, and for making a call respectively. These abstractions are defined within the Plugin.Messaging.Abstractions PCL library.
Send SMS
Phone Dialer
Send e-mail
Step 1
You can create a Xamarin.Forms app by going to File >> New >>Visual C#>>Cross Platform >> Cross platform App (Xamarin.Native or Xamarin.Forms) and click OK.
(Project name: MessageApp)
Step 2
Next, add the following NuGet packages for your project.
For that, open Solution Explorer and select solution, right click and select "Manage NuGet Packages for the Solution". In the popup window, select the following NuGet package followed by selecting your projects; then, install it.
Step 3
After installing this plug-in, design your MainPage.xaml. For that, go to Solution Explorer >>MessageApp(PCL) >>MainPage.xaml and click to open MainPage.Xaml. Here is the code.
Used toolbox items
- Label - Display a text
- Label - Display a text
- Button - Send SMS with clicked event
- Button - Phone dialer with clicked event
- Button - Send Email with clicked event
XAML code
- <?xml version="1.0" encoding="utf-8" ?>
- <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
- xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
- xmlns:local="clr-namespace:MessageApp"
- x:Class="MessageApp.MainPage">
- <StackLayout Orientation="Vertical">
- <Label Text="Xamarin.Forms"
- TextColor="Navy"
- FontSize="40"/>
- <Label Text="Messaging Application"
- BackgroundColor="Azure"
- TextColor="CadetBlue"
- FontSize="28"/>
-
- <Button Text="Send SMS"
- FontSize="25"
- Clicked="OnSMSButtonClicked"/>
- <Button Text="Phone Dial"
- Clicked="OnPhoneButtonClicked"
- FontSize="25"/>
- <Button Text="Send Email"
- Clicked="OnEmailButtonClicked"
- FontSize="25"/>
- </StackLayout>
- </ContentPage>
Step 4
Afterwards, open Solution Explorer >>MessageApp(PCL)>>MainPage.xaml.cs and double-click to get its design view. Here is code for this page.
CS code
- using Plugin.Messaging;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Xamarin.Forms;
-
- namespace MessageApp
- {
- public partial class MainPage : ContentPage
- {
- public MainPage()
- {
- InitializeComponent();
- }
-
- private void OnSMSButtonClicked(object sender , EventArgs e)
- {
- var smsMessanger = CrossMessaging.Current.SmsMessenger;
-
- if (smsMessanger.CanSendSms)
- {
- smsMessanger.SendSms("+91 7200606860", "Welcome to Xamarin.Forms");
- }
- }
-
- private void OnPhoneButtonClicked(object sender, EventArgs e)
- {
- var phoneDial = CrossMessaging.Current.PhoneDialer;
-
- if (phoneDial.CanMakePhoneCall)
- {
- phoneDial.MakePhoneCall("+91 7200606860", "Logesh Palani");
- }
- }
-
- private void OnEmailButtonClicked(object sender, EventArgs e)
- {
- var sendEmail = CrossMessaging.Current.EmailMessenger;
-
- if (sendEmail.CanSendEmail)
- {
- sendEmail.SendEmail("[email protected]", "Welcome to Xamarin", "New Updates on you xamarin forms application");
- }
- // Construct HTML email (iOS and Android only)
- //var email = new EmailMessageBuilder()
- //.To("[email protected]")
- // .Subject("Xamarin Messaging Plugin")
- // .BodyAsHtml("Well hello there from <a>Xam.Messaging.Plugin</a>")
- // .Build();
-
- }
- }
- }
Step 5
Now, go to the Build menu and click "Configure Manager". Here, you can configure your startup projects. Click F5 or start "Build and Run" your application. After running this project, click buttons to send SMS, e-mail, or make a phone call.
The results are given below.
Send SMS
Phone Dialer
Send e-mail
Finally, we have successfully created Xamarin.Forms Message application.