In this thread lets look at several options, libraries and examples for sending email in android.
You can send email just via intents. You need to chose an action based on whether an attachment is part of the email or not.
- ACTION_SENDTO (for no attachment) or
- ACTION_SEND (for one attachment) or
- ACTION_SEND_MULTIPLE (for multiple attachments)
The MIME Type can be:
"text/plain"
- "*/*"
The Extras can be:
- Intent.EXTRA_EMAIL – A string array of all "To" recipient email addresses.
- Intent.EXTRA_CC – A string array of all "CC" recipient email addresses.
- Intent.EXTRA_BCC – A string array of all "BCC" recipient email addresses.
- Intent.EXTRA_SUBJECT – A string with the email subject.
- Intent.EXTRA_TEXT – A string with the body of the email.
- Intent.EXTRA_STREAM – A Uri pointing to the attachment. If using the ACTION_SEND_MULTIPLE – action, this should instead be an ArrayList containing multiple Uri objects.
Here is a Kotlin example to send email:
fun composeEmail(addresses: Array<String>, subject: String, attachment: Uri) {
val intent = Intent(Intent.ACTION\_SEND).apply {
type = "\*/\*"
putExtra(Intent.EXTRA\_EMAIL, addresses)
putExtra(Intent.EXTRA\_SUBJECT, subject)
putExtra(Intent.EXTRA\_STREAM, attachment)
}
if (intent.resolveActivity(packageManager) != null) {
startActivity(intent)
}
}
Here is a Java Example to send email in android:
public void composeEmail(String\[\] addresses, String subject, Uri attachment) {
Intent intent = new Intent(Intent.ACTION\_SEND);
intent.setType("\*/\*");
intent.putExtra(Intent.EXTRA\_EMAIL, addresses);
intent.putExtra(Intent.EXTRA\_SUBJECT, subject);
intent.putExtra(Intent.EXTRA\_STREAM, attachment);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
Proceed below to see more options for sending email. Feel free to contribute more examples.
(a). EmailIntentBuilder
This an Android Library for the creation of SendTo Intents with mailto: URI.
Step 1 – Installation
Install the library by adding the following statement in your build.gradle file:
implementation ‘de.cketti.mailto:email-intent-builder:2.0.0’
Step 2
Then set the appropriate fields to be sent:
EmailIntentBuilder.from(activity)
.to("[email protected]")
.cc("[email protected]")
.bcc("[email protected]")
.subject("Message from an app")
.body("Some text here")
.start();
FULL EXAMPLE
Here is a full example to use this library to send email:
(a). MainActivity.java
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.material.snackbar.Snackbar;
import de.cketti.mailto.EmailIntentBuilder;
public class MainActivity extends AppCompatActivity {
private View mainContent;
private EditText emailTo;
private EditText emailCc;
private EditText emailBcc;
private EditText emailSubject;
private EditText emailBody;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\_main);
mainContent = findViewById(R.id.main\_content);
emailTo = findViewById(R.id.email\_to);
emailCc = findViewById(R.id.email\_cc);
emailBcc = findViewById(R.id.email\_bcc);
emailSubject = findViewById(R.id.email\_subject);
emailBody = findViewById(R.id.email\_body);
findViewById(R.id.button\_send\_email).setOnClickListener(v -> sendEmail());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity\_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.menu\_feedback) {
sendFeedback();
}
return true;
}
private void sendFeedback() {
boolean success = EmailIntentBuilder.from(this)
.to("[email protected]")
.subject(getString(R.string.feedback\_subject))
.body(getString(R.string.feedback\_body))
.start();
if (!success) {
Snackbar.make(mainContent, R.string.error\_no\_email\_app, Snackbar.LENGTH\_LONG).show();
}
}
void sendEmail() {
String to = emailTo.getText().toString();
String cc = emailCc.getText().toString();
String bcc = emailBcc.getText().toString();
String subject = emailSubject.getText().toString();
String body = emailBody.getText().toString();
EmailIntentBuilder builder = EmailIntentBuilder.from(this);
try {
if (!TextUtils.isEmpty(to)) {
builder.to(to);
}
if (!TextUtils.isEmpty(cc)) {
builder.cc(cc);
}
if (!TextUtils.isEmpty(bcc)) {
builder.bcc(bcc);
}
if (!TextUtils.isEmpty(subject)) {
builder.subject(subject);
}
if (!TextUtils.isEmpty(body)) {
builder.body(body);
}
boolean success = builder.start();
if (!success) {
Snackbar.make(mainContent, R.string.error\_no\_email\_app, Snackbar.LENGTH\_LONG).show();
}
} catch (IllegalArgumentException e) {
String errorMessage = getString(R.string.argument\_error, e.getMessage());
Snackbar.make(mainContent, errorMessage, Snackbar.LENGTH\_LONG).show();
}
}
}
Download
Here are the links:
(b). Maildroid
MMaildroid is a small robust android library for sending emails using SMTP server.
It uses Oracle Java Mail API to handle connections and sending emails.
Key Features
- Sending emails using SMTP protocol
- Compatible with all smtp providers
- Sending HTML/CSS styled emails
- Library is using Java Mail API that is well known as best library for sending emails
Step 1: Installation
This library is hosted in jitpack. So start by adding jitpack in your root level build.gradle:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Then you add the dependency:
dependencies {
implementation 'com.github.nedimf:maildroid:v0.0.4-relase'
}
Step 2: Code
You can use builder pattern:
MaildroidX.Builder()
.smtp("")
.smtpUsername("")
.smtpPassword("")
.port("")
.type(MaildroidXType.HTML)
.to("")
.from("")
.subject("")
.body("")
.attachment("")
.isJavascriptDisabled()
//or
.attachments() //List
.onCompleteCallback(object : MaildroidX.onCompleteCallback{
override val timeout: Long = 3000
override fun onSuccess() {
Log.d("MaildroidX", "SUCCESS")
}
override fun onFail(errorMessage: String) {
Log.d("MaildroidX", "FAIL")
}
})
.mail()
Or DSL implementation
sendEmail {
smtp("smtp.mailtrap.io")
smtpUsername("username")
smtpPassword("password")
smtpAuthentication(true)
port("2525")
type(MaildroidXType.HTML)
to("[email protected]")
from("[email protected]")
subject("Hello!")
body("email body")
attachment("path_to_file/file.txt")
//or
attachments() //List
callback {
timeOut(3000)
onSuccess {
Log.d("MaildroidX", "SUCCESS")
}
onFail {
Log.d("MaildroidX", "FAIL")
}
}
}
Step 3: Download
Here are the links:
(c). Sending Emails using Shelly
Shelly is a Fluent API for common Intent use-cases in Android.
This library wraps Intents with a clean and simple to understand interface for a number of specific use-cases. You can use it to send emails via Intent.
Step 1: Installation
Install Shell from jcenter:
implementation 'au.com.jtribe.shelly:shelly:[email protected]'
Step 2: Send Email
To send email:
Shelly.email(context)
.to("[email protected]")
.subject("Subject Text")
.body("Email Body")
.send();
To send an email to multiple addresses:
Shelly.email(context)
.to("[email protected]", "[email protected]", "[email protected]")
.subject("Subject Text")
.body("Email Body")
.send();
To send an email with a cc:
Shelly.email(context)
.to("[email protected]", "[email protected]", "[email protected]")
.cc("[email protected]", "[email protected]")
.subject("Subject Text")
.body("Email Body")
.send();
To send an email with bcc:
Shelly.email(context)
.to("[email protected]", "[email protected]", "[email protected]")
.bcc("[email protected]", "[email protected]")
.subject("Subject Text")
.body("Email Body")
.send();
Links
- Download the code here.