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.
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.
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.
Step 4
The main.axml file will open in the main page designer.
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.
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").
Step 7
In this step, go to the Main.axml page Source Panel. Note the Button id value.
Main.axml - <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">
- <Button android:id="@+id/myButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/buttonText" />
- </LinearLayout>
Step 8
In this step, open the String.xml page. Go to the Solution Explorer--> Resource--> values--> String.xml.
Step 9
After opening the String.xml file, write the following xml code.
String.xml - <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="buttonText">Send an email</string>
- <string name="app_name">SendEmail</string>
- </resources>
Step 10
Next, go to the MainActivity.cs page. Write the following code in the OnCreate() Method.
MainActivity.cs - protected override void OnCreate(Bundle bundle) {
- base.OnCreate(bundle);
-
- SetContentView(Resource.Layout.Main);
- Button button = FindViewById < Button > (Resource.Id.myButton);
- button.Click += delegate {
- var email = new Intent(Android.Content.Intent.ActionSend);
- email.PutExtra(Android.Content.Intent.ExtraEmail, new string[] {
- "[email protected]",
- "[email protected]"
- });
- email.PutExtra(Android.Content.Intent.ExtraCc, new string[] {
- "[email protected]"
- });
- email.PutExtra(Android.Content.Intent.ExtraSubject, "Hello Xamarin");
- email.PutExtra(Android.Content.Intent.ExtraText, "Hello Xamarin This is My Test Mail...!");
- email.SetType("message/rfc822");
- StartActivity(email);
- };
- }
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.
Output
After a few seconds, the app will start running on your phone.
You can tap the "Send an Email" button now.
Choose "Share with Gmail" and tap "Just once".
It will show To, cc, Subject and body of the message. Fill the details and tap"Send".
Summary So, this was the process of sending an Email using an Intent in Xamarin Android app, using Visual Studio 2015.