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,
- Windows 10 (Recommended)
- Visual Studio 2017
- 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
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)
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.
Step 4
Add the Label and Button controls in Mainpage.Xaml for displaying title and send, subject, and content buttons.
- <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"/>
- <Entry x:Name="txtTo" Placeholder="[email protected]"></Entry>
- <Entry x:Name="txtSub" Placeholder="Enter Your Subject"></Entry>
- <Editor x:Name="txtContent" HeightRequest="50" ></Editor>
- <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,
- Xamarin.Essentials.Platform.Init(this, savedInstanceState);
Also add the below method,
- publicoverridevoid OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
- {
- Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
- base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
- }
Step 6
Add the following namespace and code in Mainpage.Xaml.cs
- using Xamarin.Essentials;
- protectedoverridevoid OnAppearing() {
- base.OnAppearing();
- }
- asyncvoid btnSend_Click(object sender, System.EventArgs e) {
- List < string > toAddress = new List < string > ();
- toAddress.Add(txtTo.Text);
- await SendEmail(txtSub.Text, txtContent.Text, toAddress);
- }
- publicasync Task SendEmail(string subject, string body, List < string > recipients) {
- try {
- var message = new EmailMessage {
- Subject = subject,
- Body = body,
- To = recipients,
- };
- await Email.ComposeAsync(message);
- } catch (FeatureNotSupportedException fbsEx) {
-
- } catch (Exception ex) {
-
- }
- }
Note
Automatically the following code will be generated in XAML code view, while we are done in the design view.
- <?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:XamFormMail" x:Class="XamFormMail.MainPage">
- <StackLayout>
- <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" />
- <Entry x:Name="txtTo" Placeholder="[email protected]"></Entry>
- <Entry x:Name="txtSub" Placeholder="Enter Your Subject"></Entry>
- <Editor x:Name="txtContent" HeightRequest="50"></Editor>
- <Button x:Name="btnSend" Text="Send Mail" Clicked="btnSend_Click" />
- </StackLayout>
- </ContentPage>
Step 7
Deploy your App in UWP and Android. The output of the XamFormEmail App is,
After entering the email details,
After Clicking SEND MAIL button,
After Choosing the from Email ID
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.