Google How to make Phone Call, SMS And Email in Xamarin.Forms (C# - Xaml) | SubramanyamRaju Xamarin & Windows App Dev Tutorials

Sunday 18 March 2018

How to make Phone Call, SMS And Email in Xamarin.Forms (C# - Xaml)

Introduction:
We can easily make phone call, sending message, email in Xamarin.forms. In this article we can learn how to implement them in Xamarin.Forms (PCL) project.

Requirements:
  • This article source code is prepared by using Visual Studio Community for Mac (7.4). And it is better to install latest visual studio updates from here.
  • This sample project is Xamarin.Forms PCL project and tested in Android emulator.
  • Used Xam.Plugins.Messaging Nuget Package version is V5.1.0.
Description:
This article can explain you below topics:
1. How to create Xamarin.Forms PCL project with Visual Studio for Mac?
2. How to make phone, SMS, email using Xam.Plugins.Messaging in Xamarin.Forms project?

1. How to create Xamarin.Forms PCL project with Visual studio for Mac?
First we need to create the new Xamarin.Forms project. 
  • Launch Visual Studio for Mac.
  • On the File menu, select New Solution.
  • The New Project dialog appears. The left pane of the dialog lets you select the type of templates to display. In the left pane Multiplatform App Xamarin.Forms > Blank Forms App and click on Next.
  • Enter your App Name (Ex: MessagingPluginSample). Select Target Platforms to Android & iOS and Shared Code to Portable Class Library  after that click on Next button.

  • You can choose your project location like below and Create new project.

And project structure will be.
  • MessagingPluginSample: It is for PCL shared code.
  • MessagingPluginSample.Droid: It is for Android.
  • MessagingPluginSample.iOS: It is for iOS.
2. How to make phone, SMS, email using Xam.Plugins.Messaging in Xamarin.Forms project?
We need to follow below few steps
Step 1:Add Xam.Plugins.Messaging Nuget package to PCL, Android and iOS project
In PCL, Right click on Packages folder => Add Packages.
After that Add Packages windows will open, search for "Xam.Plugins.Messaging" and click on Add Package to install it.


Now your PCL project should include above package in Packages folder. Please add above package to Android and iOS project by following same steps like PCL.
Step 2: Create HomePage which will have UI
Now create Views folder by right click on your PCL project name MessagingPluginSample => Add => New Folder and name it "Views". After that right click on Views folder => Add =>New File => Forms => Forms ContentPage Xaml and name it HomePage like below.

HomePage.xaml
Now open Home page and add below xaml code to create UI for it.
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="MessagingPluginSample.Views.HomePage">  
  3.   <StackLayout Padding="70,20,70,0" Spacing="20">    
  4.             <Label Text="Home" FontSize="25" HorizontalOptions="Center"/>  
  5.           
  6.         <!--Phone dial-->  
  7.             <Entry x:Name="MsgTo" Placeholder="PhoneNumber" HorizontalOptions="FillAndExpand"/>    
  8.             <Entry x:Name="Message" Placeholder="MessageText" HorizontalOptions="FillAndExpand"/>    
  9.             <Button Text="SMS"   Clicked="BtnSend_Click" BackgroundColor="White"/>    
  10.   
  11.         <!--SMS-->  
  12.             <Entry x:Name="PhoneNumber" Placeholder="PhoneNumber" HorizontalOptions="FillAndExpand"/>    
  13.             <Button Text="CALL"  Clicked="BtnCall_Click" BackgroundColor="White"/>  
  14.           
  15.       <!--Email-->  
  16.             <Entry x:Name="EmailTo" Placeholder="EmailTo" HorizontalOptions="FillAndExpand"/>    
  17.             <Entry x:Name="EmailSubject" Placeholder="Subject" HorizontalOptions="FillAndExpand"/>    
  18.             <Entry x:Name="EmailBody" Placeholder="MessageText" HorizontalOptions="FillAndExpand"/>    
  19.             <Button Text="EMAIL" Clicked="BtnEmail_Click" BackgroundColor="White"/>    
  20.      </StackLayout>    
  21. </ContentPage>  
HomePage.xaml.cs
  1. using System;  
  2. using Plugin.Messaging;  
  3. using Xamarin.Forms;  
  4.   
  5. namespace MessagingPluginSample.Views  
  6. {  
  7.     public partial class HomePage : ContentPage  
  8.     {  
  9.         public HomePage()  
  10.         {  
  11.             InitializeComponent();  
  12.         }  
  13.   
  14.         private void BtnSend_Click (object sender, EventArgs e)    
  15.         {  
  16.             
  17.            var SmsTask = CrossMessaging.Current.SmsMessenger;    
  18.     
  19.             if (SmsTask.CanSendSms)    
  20.                 SmsTask.SendSms (MsgTo.Text, Message.Text);    
  21.         }    
  22.     
  23.     
  24.         private void BtnCall_Click (object sender, EventArgs e)    
  25.         {    
  26.             var PhoneCallTask = CrossMessaging.Current.PhoneDialer;    
  27.             if (PhoneCallTask.CanMakePhoneCall)    
  28.                 PhoneCallTask.MakePhoneCall (PhoneNumber.Text);    
  29.         }    
  30.     
  31.     
  32.         private void BtnEmail_Click (object sender, EventArgs e)    
  33.         {    
  34.             var EmailTask = CrossMessaging.Current.EmailMessenger;    
  35.     
  36.             if (EmailTask.CanSendEmail)    
  37.                 EmailTask.SendEmail (EmailTo.Text, EmailSubject.Text, EmailBody.Text);    
  38.         }    
  39.   
  40.     }  
  41. }  
Note: Don’t forgot to enable CALL_PHONE and SEND_SMS capabilities in android project and ID_CAP_PHONEDAILER capabilities on windows phone.

Demo screens from Android:
You can directly work on below sample source code to understand this article. 

CheckBoxSample

FeedBack Note: Please share your thoughts, what you think about this post, Is this post really helpful for you? I always welcome if you drop comments on this post and it would be impressive.

Follow me always at @Subramanyam_B
Have a nice day by  :)

4 comments:

  1. How do you verify that the user actually sent the SMS (rather than cancelling it) or that there was no error while sending the SMS?

    ReplyDelete
    Replies
    1. Hi Primehalo, did you find any solution for this?

      How do you verify that the user actually sent the SMS (rather than cancelling it) or that there was no error while sending the SMS?

      Delete
  2. it work for me but when i tab back button it not get back on old page in my app

    ReplyDelete
  3. hi nice works. can i do it for sms read in xamarin forms pcl projects.

    ReplyDelete

Search Engine Submission - AddMe