Sending Email in Android using JavaMail API without using the default/built-in app
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In the realm of Android development, sending an email programmatically without invoking the built-in email app can be necessary for various reasons, such as automating email transactions or customizing the user experience directly within the app. This can be achieved using the JavaMail API, a robust and mature framework that allows developers to send and receive emails through Java applications.
Prerequisites
Before diving into implementation, ensure the following prerequisites are met:
- Android Studio: Ensure you have the latest version installed.
- JavaMail Library: Include JavaMail and its dependencies in your project.
- Internet Permission: Ensure your
AndroidManifest.xmlincludes the necessary internet permission. - SMTP Server: You should have access to an SMTP server (like Gmail, Outlook, etc.).
Adding JavaMail Library
While JavaMail API is not available on Maven Central, you can include it in your project by downloading the necessary .jar files:
- Download JavaMail: Obtain the
javax.mail.jarlibrary from the official website or a trusted source. - Include in Project: Create a
libsdirectory within yourappmodule and place the.jarfile there. Then, add it to your build script.
Setting Up Internet Permission
Add the following line within the manifest tag in AndroidManifest.xml to allow your app access to the internet, which is required to send emails via an SMTP server:
Implementation
Example: Sending Email
Here's a step-by-step guide on how to programmatically send an email using JavaMail API in Android:
- Email Session Setup: Create a mail session by configuring properties and initializing a
Sessionobject. - Authenticate SMTP: Implement a
Authenticatorsubclass to handle username and password authentication for the SMTP server. - Construct Email Message: Use the
MimeMessageclass to set the email’s attributes, including the recipient, subject, and content. - Send Email: Utilize the
Transportclass to send the email.
Here’s a practical example of how it might look:
Security Considerations
- Secure Connections: Use TLS/SSL to secure communications with the SMTP server.
- Credentials Storage: Avoid hardcoding email credentials in the source code. Externalize configurations and protect them using Android Keystore or encrypted storage.
- Gmail OAuth: Consider implementing OAuth for Gmail to avoid using application-specific passwords.
Error Handling
Handling exceptions that arise from networking or server-side errors is crucial. The use of try-catch blocks around the Transport.send() method ensures adequate error logging and notifications.
Common Exceptions
- MessagingException: This can occur due to authentication issues or incorrect server settings.
- SendFailedException: Indicates failure in sending the message, either due to incorrect recipient addresses or server rejection.
Implement comprehensive logging and user feedback mechanisms to gracefully handle such exceptions.
Table Summary
Below is a summary of key points:
| Aspect | Details |
| JavaMail Library | Download and include the .jar file manually in the project directory. |
| SMTP Server | Access to an SMTP server (e.g., Gmail, Yahoo). |
| Internet Permission | Add to AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET"/>. |
| Setup Session | Configure mail session with properties for SMTP authentication and TLS. |
| Email Message | Use MimeMessage to set sender, recipient, subject, and body. |
| Authenticator | Implement a custom Authenticator subclass for password authentication. |
| SSL/TLS | Enable StartTLS to secure email transport. |
Additional Details
- Handling Attachments: To include attachments, the JavaMail API provides
MimeBodyPartandMultipartclasses, allowing you to add additional content to an email. - Formatting and HTML Content: To send a rich text or HTML email, the content type should be set using
message.setContent()with MIME typetext/html.
By harnessing the JavaMail API in Android applications in this manner, developers can create sophisticated email-related functionalities, customizing how users send and receive emails without the constraints of native or third-party email apps.

