Xamarin.Forms - Messaging App

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.
  • Xam.Plugins.Messaging
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.
  • Xam.Plugins.Messaging 
 
 
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

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  4.              xmlns:local="clr-namespace:MessageApp"  
  5.              x:Class="MessageApp.MainPage">  
  6. <StackLayout Orientation="Vertical">  
  7.     <Label Text="Xamarin.Forms"  
  8.         TextColor="Navy"  
  9.         FontSize="40"/>  
  10.         <Label Text="Messaging Application"  
  11.            BackgroundColor="Azure"  
  12.            TextColor="CadetBlue"  
  13.                FontSize="28"/>  
  14.   
  15.         <Button Text="Send SMS"  
  16.             FontSize="25"  
  17.             Clicked="OnSMSButtonClicked"/>  
  18.     <Button Text="Phone Dial"  
  19.             Clicked="OnPhoneButtonClicked"  
  20.             FontSize="25"/>  
  21.     <Button Text="Send Email"  
  22.             Clicked="OnEmailButtonClicked"  
  23.             FontSize="25"/>  
  24.     </StackLayout>  
  25. </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
  1. using Plugin.Messaging;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using System.Threading.Tasks;  
  7. using Xamarin.Forms;  
  8.   
  9. namespace MessageApp  
  10. {  
  11.     public partial class MainPage : ContentPage  
  12.     {  
  13.         public MainPage()  
  14.         {  
  15.             InitializeComponent();  
  16.         }  
  17.   
  18.         private void OnSMSButtonClicked(object sender , EventArgs e)  
  19.         {  
  20.             var smsMessanger = CrossMessaging.Current.SmsMessenger;  
  21.   
  22.             if (smsMessanger.CanSendSms)   
  23.             {  
  24.                 smsMessanger.SendSms("+91 7200606860", "Welcome to Xamarin.Forms");  
  25.             }  
  26.         }  
  27.   
  28.         private void OnPhoneButtonClicked(object sender, EventArgs e)  
  29.         {  
  30.             var phoneDial = CrossMessaging.Current.PhoneDialer;  
  31.   
  32.             if (phoneDial.CanMakePhoneCall)  
  33.             {  
  34.                 phoneDial.MakePhoneCall("+91 7200606860", "Logesh Palani");  
  35.             }  
  36.         }  
  37.   
  38.         private void OnEmailButtonClicked(object sender, EventArgs e)  
  39.         {  
  40.             var sendEmail = CrossMessaging.Current.EmailMessenger;  
  41.   
  42.             if (sendEmail.CanSendEmail)  
  43.             {  
  44.                 sendEmail.SendEmail("[email protected]", "Welcome to Xamarin", "New Updates on you xamarin forms application");  
  45.             }  
  46.                   // Construct HTML email (iOS and Android only)  
  47.                   //var email = new EmailMessageBuilder()  
  48.                   //.To("[email protected]")  
  49.                   //  .Subject("Xamarin Messaging Plugin")  
  50.                   //  .BodyAsHtml("Well hello there from <a>Xam.Messaging.Plugin</a>")  
  51.                   //  .Build();  
  52.   
  53.         }  
  54.     }  
  55. }   
 
 
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.


Similar Articles