Send Email Using An Intent In Xamarin Android App, Using Visual Studio 2015

Introduction

Xamarin is a platform to develop cross-platform and multi-platform apps (Ex. Windows phone, Android, iOS). The code-sharing concept is used in Xamarin. The Xamarin Studio is available in the Visual Studio also.

How to compose and send email using Intent

Prerequisites

  • Visual Studio 2015 update 3

The following steps are needed to be followed in order to send the Email using Intent in Xamarin Android app.

Step 1

Click File-->  New--> Project, or click (Ctrl+Shift+N). This will open the list of all types of projects available in Visual Studio.

Project

Step 2

Select Installed --> Templates --> Visual C# --> Android --> choose the Blank App (Android).

Next, give your Android app a name (Ex:sample) and assign the path of your project. Click OK.

Project

Step 3

Next, go to the Solution Explorer and select Resource -->Layout. Double click to open main.axml page. Select either the Source tab or Designer tab depending on your expertise of designing the application.

Project

Step 4

The main.axml file will open in the main page designer.

Project

Delete the Linear Layout and default "hello world" button by removing the coding from source panel.

Now, delete the C# button action code from MainActivity.cs page.

Step 5

Next, go to the toolbox window and scroll down until you see all the tools and controls.

Drag and drop a Button to your application.

Project

Step 6

Next, go to the properties window and edit the Button id value and Text value.

(Ex:android:id="@+id/myButton" android:text="@string/buttonText").

Project

Step 7

In this step, go to the Main.axml page Source Panel. Note the Button id value.

Project

Main.axml
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">  
  2.     <Button android:id="@+id/myButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/buttonText" />  
  3. </LinearLayout>  
Step 8

In this step, open the String.xml page. Go to the Solution Explorer--> Resource--> values--> String.xml.

Project

Step 9

After opening the String.xml file, write the following xml code.

String.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="buttonText">Send an email</string>  
  4.     <string name="app_name">SendEmail</string>  
  5. </resources>  
Project

Step 10

Next, go to the MainActivity.cs page. Write the following code in the OnCreate() Method.

MainActivity.cs
  1. protected override void OnCreate(Bundle bundle) {  
  2.     base.OnCreate(bundle);  
  3.     // Set our view from the "main" layout resource  
  4.     SetContentView(Resource.Layout.Main);  
  5.     Button button = FindViewById < Button > (Resource.Id.myButton);  
  6.     button.Click += delegate {  
  7.         var email = new Intent(Android.Content.Intent.ActionSend);  
  8.         email.PutExtra(Android.Content.Intent.ExtraEmail, new string[] {  
  9.             "[email protected]",  
  10.             "[email protected]"  
  11.         });  
  12.         email.PutExtra(Android.Content.Intent.ExtraCc, new string[] {  
  13.             "[email protected]"  
  14.         });  
  15.         email.PutExtra(Android.Content.Intent.ExtraSubject, "Hello Xamarin");  
  16.         email.PutExtra(Android.Content.Intent.ExtraText, "Hello Xamarin This is My Test Mail...!");  
  17.         email.SetType("message/rfc822");  
  18.         StartActivity(email);  
  19.     };  
  20. }  
Project

Step 11

If you have Android Virtual device, run the app on it. Else, connect your Android phone and run the app in that.

Simply, connect your phone and go to Visual Studio. The connected phone will show up in the Run menu (Ex:LENOVO A6020a40(Android 5.1-API 22)). Click the Run option.

Project

Output

After a few seconds, the app will start running on your phone.

You can tap the "Send an Email" button now.

Project

Choose "Share with Gmail" and tap "Just once".

Project

It will show To, cc, Subject and body of the message. Fill the details and tap"Send".

Project


Project

Summary

So, this was the process of sending an Email using an Intent in Xamarin Android app, using Visual Studio 2015.

Up Next
    Ebook Download
    View all
    Learn
    View all