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 Application without any Intents. For this, we can 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.

Coding Part:

Steps:

I have split this article into 3 steps as in the following.
Step 1: Creating 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 new Xamarin.Android Projects

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

fig1

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 and do search for Mailkit and click install to add the library or Paste the following in Package Manager Console to install the Nuget plugin.

Install-Package MailKit
fig2

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.
    using MimeKit;
    using MailKit.Net.Smtp;
    
  • Then add/create an Async task class named as “MailAsyncTask” and add implement the override functions.
    • OnPreExecute
    • OnPostExecute
    • DoInBackground
  • Then add/paste the following code in DoInBackground method.
    var message = new MimeMessage();
    message.From.Add(new MailboxAddress("From", mainActivity.edtFrom.Text));
    message.To.Add(new MailboxAddress("To", mainActivity.edtTo.Text));
    message.Subject = mainActivity.edtSubject.Text;
    
    message.Body = new TextPart("plain")
    {
     Text = mainActivity.edtMessage.Text
    };
    
    using (var client = new SmtpClient())
    {
     // For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS)
     client.ServerCertificateValidationCallback = (s, c, h, e) => true;
    
     client.Connect(host, port, false);
    
     // Note: only needed if the SMTP server requires authentication
     client.Authenticate(username, password);
     client.Send(message);
     client.Disconnect(true);
    }
  • Here, we should provide host address, port and username & password if the SMTP server needs authentication.
  • The Mail Connections needed to be run with separate thread. So, I had 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.

class MailAsyncTask : AsyncTask
{
 string username = "mail-id or username", password = "password", host = "smtp.gmail.com";
 int port = 25;
 MainActivity mainActivity;
 ProgressDialog progressDialog;

 public MailAsyncTask(MainActivity activity)
 {
  mainActivity = activity;
  progressDialog = new ProgressDialog(mainActivity);
  progressDialog.SetMessage("Sending...");
  progressDialog.SetCancelable(false);
 }

 protected override void OnPreExecute()
 {
  base.OnPreExecute();
  progressDialog.Show();
 }

 protected override Java.Lang.Object DoInBackground(params Java.Lang.Object[] @params)
 {
  try
  {
   var message = new MimeMessage();
   message.From.Add(new MailboxAddress("From", mainActivity.edtFrom.Text));
   message.To.Add(new MailboxAddress("To", mainActivity.edtTo.Text));
   message.Subject = mainActivity.edtSubject.Text;

   message.Body = new TextPart("plain")
   {
    Text = mainActivity.edtMessage.Text
   };

   using (var client = new SmtpClient())
   {
    // For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS)
    client.ServerCertificateValidationCallback = (s, c, h, e) => true;

    client.Connect(host, port, false);

    // Note: only needed if the SMTP server requires authentication
    client.Authenticate(username, password);
    client.Send(message);
    client.Disconnect(true);
   }
   return "Successfully Sent";
  }
  catch (System.Exception ex)
  {
   return ex.Message;
  }
 }

 protected override void OnPostExecute(Java.Lang.Object result)
 {
  base.OnPostExecute(result);
  progressDialog.Dismiss();
  mainActivity.edtFrom.Text = null;
  mainActivity.edtTo.Text = null;
  mainActivity.edtSubject.Text = null;
  mainActivity.edtMessage.Text = null;
  Toast.MakeText(mainActivity, "Email Succesfully Sent...", ToastLength.Short).Show();
 }
}

The Mail class can be executed by the following code

new MailAsyncTask(this).Execute();

Download Code:

You can download the full source code of the article in GitHub. If you like this article, do star the repo in GitHub. Hit like the article.

Share this

Related Posts

Previous
Next Post »

1 comments:

comments
13 October 2018 at 18:00 delete

Nice article…
Thank you for sharing !!

Recharge Designs is a creative agency that’s into content, designing, branding and IT services. We have been in the industry from over a decade and the kind of clients we have handled has given us the required expertise. Recently, we have made inroads into IT industry and just in a short span of time we have garnered the best in the industry.

For more details visit :- http://www.rechargedesigns.com/
contact :- sales@rechargedesigns.com / 9285022333

Reply
avatar

Please Comment about the Posts and Blog