How To Send Mail Directly From Android Applications In Xamarin.Android

In this article, we will learn how to send Mail directly from Xamarin Android Application without any Intents. For this, we can MailKit to send mail directly.

How To Send Mail Directly From Android Application In Xamarin.Android

Introduction

In this article, we will learn how to send mail directly from Xamarin.Android applications without any intents. We can use MailKit to send mail directly.

MailKit

MailKit is a cross-platform mail client library built on top of MimeKit. MailKit is a personal open source project that I have put thousands of hours into perfecting with the goal of making it the very best email framework for .NET.

Steps

I have split this article into 3 steps as in the following.

  • Step 1 - Creating a new Xamarin.Android Projects.
  • Step 2 - Setting up the plugin for Xamarin.Android Application.
  • Step 3 - Implementing Mail functionalities in Xamarin.Android Application.

Step 1 - Creating a new Xamarin.Android Projects

Create New Project by Selecting New - Project - Select Android App and Click OK.

How To Send Mail Directly From Android Application In Xamarin.Android

Step 2 - Setting up the plugin for Xamarin.Android Application

In this step, we will include the Mailkit plugin for Xamarin.Android Project. Open NuGet Package Manager against the project, search for Mailkit, and click Install to add the library or paste the following in Package Manager Console to install the NuGet plugin.

  1. Install-Package MailKit  
How To Send Mail Directly From Android Application In Xamarin.Android

Step 3 - Implementing Mail Functionalities in Xamarin.Android Application

In this part, we will see how to implement Mail functions to send mail.

  • Open your MainActivity.cs and Import the Following Packages.
    1. using MimeKit;  
    2. using MailKit.Net.Smtp;  
  • Then add/create an Async task class named as MailAsyncTaskand add implement the override functions.

    • OnPreExecute
    • OnPostExecute
    • DoInBackground

  • Then add/paste the following code in DoInBackground method.
    1. var message = new MimeMessage();  
    2. message.From.Add(new MailboxAddress("From", mainActivity.edtFrom.Text));  
    3. message.To.Add(new MailboxAddress("To", mainActivity.edtTo.Text));  
    4. message.Subject = mainActivity.edtSubject.Text;  
    5. message.Body = new TextPart("plain") {  
    6.     Text = mainActivity.edtMessage.Text  
    7. };  
    8. using(var client = new SmtpClient()) {  
    9.     // For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS)  
    10.     client.ServerCertificateValidationCallback = (s, c, h, e) => true;  
    11.     client.Connect(host, port, false);  
    12.     // Note: only needed if the SMTP server requires authentication  
    13.     client.Authenticate(username, password);  
    14.     client.Send(message);  
    15.     client.Disconnect(true);  
    16. }  
  • Here, we should provide host address, port and username & password if the SMTP server needs authentication.

  • The Mail Connections need to be run with a separate thread. So, I have done the call with AsyncTask. You can find the full code below.

Full code of the MailAsyncClass

The following code shows how to implement Direct Mail functions in Xamarin.Android with AsyncTask.

  1. class MailAsyncTask: AsyncTask {  
  2.     string username = "mail-id or username", password = "password", host = "smtp.gmail.com";  
  3.     int port = 25;  
  4.     MainActivity mainActivity;  
  5.     ProgressDialog progressDialog;  
  6.     public MailAsyncTask(MainActivity activity) {  
  7.         mainActivity = activity;  
  8.         progressDialog = new ProgressDialog(mainActivity);  
  9.         progressDialog.SetMessage("Sending...");  
  10.         progressDialog.SetCancelable(false);  
  11.     }  
  12.     protected override void OnPreExecute() {  
  13.         base.OnPreExecute();  
  14.         progressDialog.Show();  
  15.     }  
  16.     protected override Java.Lang.Object DoInBackground(params Java.Lang.Object[] @params) {  
  17.         try {  
  18.             var message = new MimeMessage();  
  19.             message.From.Add(new MailboxAddress("From", mainActivity.edtFrom.Text));  
  20.             message.To.Add(new MailboxAddress("To", mainActivity.edtTo.Text));  
  21.             message.Subject = mainActivity.edtSubject.Text;  
  22.             message.Body = new TextPart("plain") {  
  23.                 Text = mainActivity.edtMessage.Text  
  24.             };  
  25.             using(var client = new SmtpClient()) {  
  26.                 // For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS)  
  27.                 client.ServerCertificateValidationCallback = (s, c, h, e) => true;  
  28.                 client.Connect(host, port, false);  
  29.                 // Note: only needed if the SMTP server requires authentication  
  30.                 client.Authenticate(username, password);  
  31.                 client.Send(message);  
  32.                 client.Disconnect(true);  
  33.             }  
  34.             return "Successfully Sent";  
  35.         } catch (System.Exception ex) {  
  36.             return ex.Message;  
  37.         }  
  38.     }  
  39.     protected override void OnPostExecute(Java.Lang.Object result) {  
  40.         base.OnPostExecute(result);  
  41.         progressDialog.Dismiss();  
  42.         mainActivity.edtFrom.Text = null;  
  43.         mainActivity.edtTo.Text = null;  
  44.         mainActivity.edtSubject.Text = null;  
  45.         mainActivity.edtMessage.Text = null;  
  46.         Toast.MakeText(mainActivity, "Email Succesfully Sent...", ToastLength.Short).Show();  
  47.     }  
  48. }  
The Mail class can be executed by the following code.
  1. new MailAsyncTask(this).Execute();  

Download Code

You can download the full source code from GitHub. If you like the post, do like and share the article and star the repo on GitHub.