IntroductionIn 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.
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.
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()) {
-
- client.ServerCertificateValidationCallback = (s, c, h, e) => true;
- client.Connect(host, port, false);
-
- 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 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.
- 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()) {
-
- client.ServerCertificateValidationCallback = (s, c, h, e) => true;
- client.Connect(host, port, false);
-
- 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 from GitHub. If you like the post, do like and share the article and star the repo on GitHub.