Mailgun Android Example: Sending Emails with Mailgun

Have you tried creating an app that can send emails? I have posted a tutorial earlier explaining how to send emails using Javamail API. But the problem with Javamail API is an SMTP server is …

Have you tried creating an app that can send emails? I have posted a tutorial earlier explaining how to send emails using Javamail API. But the problem with Javamail API is an SMTP server is required and in the tutorial we were using Google SMTP. So in this Mailgun Android Example, we will learn sending emails using Mailgun.

What is Mailgun?

Mailgun is an email service provider for developers. It gives transactional email API Service, so you do not need to worry about creating your own SMTP server. Mailgun gives us RESTful API to perform email operations.

Mailgun Android Example

So now let’s create our email sender app. As always we will open Android Studio and we will create a new project. But before going further, just make sure that you have created an account in Mailgun.

Creating a new Android Studio Project

  • For this example, I have created a project name My Email Sender.
  • Once the project is loaded we will add the required dependencies in our project.

Adding Dependencies

Here I am going to use Retrofit Library. Now retrofit has nothing to do with Mailgun, it is a network library which makes networking easier in our application. Mailgun provides RESTful API for sending emails, and to make the API call we are going to use Retrofit. Though it is optional you can use other libraries as well, or if you don’t want to use any library you can do it without a library as well.

I have already posted tutorials about http calls from android with and without retrofit. You can go through the below links to learn in detail.

But for this post I am going to use Retrofit as it is the best library now. 

  • To add retrofit into your project, open app level build.gradle file and add the following dependencies.

  • After adding the above three lines, sync your project.

Designing User Interface

  • As this is only an example, I am not going to design a very cool or awesome UI. For the simplicity let’s not worry about the look of the app. But yes, you can play with the design as much as you want.
  • For designing a simple UI like me, copy the below xml code to your activity_main.xml file.

  • The above code will generate the following user interface.
Mailgun Android Example
Mailgun Android Example
  • Now let’s code the basics in MainActivity.java.

  • The above written code is very simple, and I don’t think there is a need to explain it. We simply initialised the view objects and we added input validations.

Sending Email using Mailgun

  • Now open your Mailgun account, and click on domains. Here you will see a sandbox domain. If you own a domain you can add it here. But for this example I am going to use the sandbox domain.

Android Mailgun Example

  • If you will use Sandbox domain, you need to add authorized recipients and only those recipients who are authorized will receive the email using sandbox domain. So make sure you add an authorized recipient for testing. 

Mailgun Example Android

  • As you can see above, I have added an authorized recipient.
  • If you want to send emails to any email you need to add your domain here.

Testing Mailgun API using Postman

  • As I already told you Mailgun provides us RESTful Web Service to do operations. And here our task is to send email. So let’s first understand how the Mailgun API works.
Base URL
  • Below is the base URL of our Mailgun API.

  • As we are using the sandbox domain. So the actual API in my case is

  • Remember it is my Mailgun API, for your case you have to use your own custom or sandbox domain. 
Authentication
  • Mailgun API uses HTTP Basic Authentication, where user name is api and password is your api key.
  • To find your API key, click on the domain and you will see the API Key.

Android Mailgun Example

Sending Email
  • Now let’s try sending an email using POSTMAN. (For those who don’t know what is POSTMAN, it is a REST API Development Tool). It is free, if you don’t have search on google and download.
  • For sending an email we will send a POST Request to /messages (We need to add the base URL before).
Endpoint Method Parameters
messages POST to, from, subject, text
  • You can send more parameters, for the details go through the official documentation.
  • Now open POSTMAN and put the URL, required parameters, Basic Auth values and send an HTTP Post request.

Sending Email Mailgun

  • You can see it is working fine.

Using the Mailgun API in Android

  • I hope you got the idea that what we are going to do. It is a simple POST request that we need to make from our Android App and here I am using Retrofit for this.
  • So we need an interface for the API call.

Creating API interface

  • Create an interface named Api and write the following code.

  • We have now defined the API call.
  • Now we need a Retrofit Client to make the call.

Creating a Singleton Retrofit Client

  • Create a new class named Retrofit Client and write the following code.

  • And we have the Retrofit Client as well. Now we only need to make the call to send email.
  • And to do this we will use the following code.

  • So the final code for our MainActivity.java will be.

  • Lastly add internet permission to your AndroidManifest.xml file.

  • Now you can try running the application. But make sure you are sending the email to an authorized recipient or else you will not receive the email. 
Mailgun Android Example
Mailgun Android Example
  • As you can see it is working fine. If you did not receive the email, check your spam folder.

Mailgun Android Example – Source Code

So guys, if you are having any trouble building the email sender, you can clone my source code. The link to the repository is given below.

[sociallocker id=1372] Mailgun Android Example Source Code [/sociallocker]

So that is all for this Mailgun Android Example friends. If you have any question please let me know in the comments. And if you found this post helpful share it with your friends. Thank You 🙂

Hi, my name is Belal Khan and I am a Google Developers Expert (GDE) for Android. The passion of teaching made me create this blog. If you are an Android Developer, or you are learning about Android Development, then I can help you a lot with Simplified Coding.

Expand Your Knowledge: Next Tutorial Picks

0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x