How To Send Content With Default Email Application In Xamarin Forms Application Using Xamarin Essentials For Android And UWP

The Email class is available at Xamarin.Essentials API. It is used to enable an application to open the default email application with specified information including subject, body, and recipients (TO, CC, BCC). Android, iOS, and UWP offer unique operating system and platform APIs that developers have access to all in C# leveraging Xamarin. Xamarin.Essentials provides a single cross-platform API that works with any Xamarin.Forms, Android, iOS, or UWP application that can be accessed from shared code no matter how the user interface is created.

Reading this article, you can learn how to send content with the default mail application in Xamarin Forms application using Xamarin Essentials for Android and Universal Windows Platform with XAML and Visual C# in the cross-platform application development.

The following important tools are required for developing Xamarin Forms App,

  1. Windows 10 (Recommended)
  2. Visual Studio 2017
  3. Android API 19 or higher and UWP 10.0.16299.0 or higher.

Now, we can discuss step by step App development.

Step 1

Open Visual Studio 2017 -> Start -> New Project-> Select Cross-Platform (under Visual C#-> Mobile App (Xamarin.Forms)-> Give the Suitable Name for your App (XamFormEmail) ->OK

How To Send A Content With Default Email Application In Xamarin Forms Application Using Xamarin Essentials For Android And UWP 

Step 2

Select the Cross-Platform template as the Blank APP ->Set Platform as Android and UWP and code sharing strategy as .NET standar. Afterwards, Visual Studio creates 3 projects (Portable, Android, UWP)

How To Send A Content With Default Email Application In Xamarin Forms Application Using Xamarin Essentials For Android And UWP 

Step 3

For adding a reference,

RightClick Your solution (XamFormEmail) and Select Manage NuGet Packages.

For adding Xamarin.Essentials Reference, Choose Browse and Search Xamarin.Essentials, select the package and select all the projects (portable, Android, UWP) and install it.

How To Send A Content With Default Email Application In Xamarin Forms Application Using Xamarin Essentials For Android And UWP 

Step 4

Add the Label and Button controls in Mainpage.Xaml for displaying title and send, subject, and content buttons. 

  1. <Label FontAttributes="Bold" Text=" How to Send a content with default email application in Xamarin Forms application using Xamarin Essentials for Android and UWP" VerticalOptions="Center" HorizontalOptions="Center"/>  
  2. <Entry x:Name="txtTo" Placeholder="[email protected]"></Entry>  
  3. <Entry x:Name="txtSub" Placeholder="Enter Your Subject"></Entry>  
  4. <Editor x:Name="txtContent" HeightRequest="50" ></Editor>  
  5. <Button x:Name="btnSend" Text="Send Mail" Clicked="btnSend_Click"/> 

Step 5

Add the following code in the MainActivity.cs in XamFormEmail.Android project,

In onCreate() method,

  1. Xamarin.Essentials.Platform.Init(this, savedInstanceState); 

Also add the below method,

  1. publicoverridevoid OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)  
  2. {  
  3.    Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);  
  4.    base.OnRequestPermissionsResult(requestCode, permissions, grantResults);  
  5. }  

Step 6

Add the following namespace and code in Mainpage.Xaml.cs

  1. using Xamarin.Essentials;  
  2. protectedoverridevoid OnAppearing() {  
  3.     base.OnAppearing();  
  4. }  
  5. asyncvoid btnSend_Click(object sender, System.EventArgs e) {  
  6.     List < string > toAddress = new List < string > ();  
  7.     toAddress.Add(txtTo.Text);  
  8.     await SendEmail(txtSub.Text, txtContent.Text, toAddress);  
  9. }  
  10. publicasync Task SendEmail(string subject, string body, List < string > recipients) {  
  11.     try {  
  12.         var message = new EmailMessage {  
  13.             Subject = subject,  
  14.                 Body = body,  
  15.                 To = recipients,  
  16.         };  
  17.         await Email.ComposeAsync(message);  
  18.     } catch (FeatureNotSupportedException fbsEx) {  
  19.         // Email is not supported on this device  
  20.     } catch (Exception ex) {  
  21.         // Some other exception occurred  
  22.     }  

Note
Automatically the following code will be generated in XAML code view, while we are done in the design view.

  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" xmlns:local="clr-namespace:XamFormMail" x:Class="XamFormMail.MainPage">  
  3.     <StackLayout>  
  4.         <Label FontAttributes="Bold" Text=" How to Send a content with default email application in Xamarin Forms application using Xamarin Essentials for Android and UWP" VerticalOptions="Center" HorizontalOptions="Center" />  
  5.         <Entry x:Name="txtTo" Placeholder="[email protected]"></Entry>  
  6.         <Entry x:Name="txtSub" Placeholder="Enter Your Subject"></Entry>  
  7.         <Editor x:Name="txtContent" HeightRequest="50"></Editor>  
  8.         <Button x:Name="btnSend" Text="Send Mail" Clicked="btnSend_Click" />  
  9.     </StackLayout>  
  10. </ContentPage>  

Step 7

Deploy your App in UWP and Android. The output of the XamFormEmail App is,

How To Send A Content With Default Email Application In Xamarin Forms Application Using Xamarin Essentials For Android And UWP 
 
How To Send A Content With Default Email Application In Xamarin Forms Application Using Xamarin Essentials For Android And UWP 

After entering the email details,

How To Send A Content With Default Email Application In Xamarin Forms Application Using Xamarin Essentials For Android And UWP 
 
How To Send A Content With Default Email Application In Xamarin Forms Application Using Xamarin Essentials For Android And UWP 

After Clicking SEND MAIL button,

How To Send A Content With Default Email Application In Xamarin Forms Application Using Xamarin Essentials For Android And UWP 
 
How To Send A Content With Default Email Application In Xamarin Forms Application Using Xamarin Essentials For Android And UWP 

After Choosing the from Email ID

How To Send A Content With Default Email Application In Xamarin Forms Application Using Xamarin Essentials For Android And UWP 
 
How To Send A Content With Default Email Application In Xamarin Forms Application Using Xamarin Essentials For Android And UWP 

Summary

Now, you have successfully sent content with default mail application in Xamarin Forms application using Xamarin Essentials for Android and Universal Windows Platform using Visual C# and Xamarin.


Similar Articles