Sending Email in Android using JavaMail API without using the default/built-in app
To send an email in Android using the JavaMail API without using the default/built-in app, you can use the following steps:
To send an email in Android using the JavaMail API without using the default/built-in app, you can use the following steps:
- Download the JavaMail API from the following link: https://eclipse-ee4j.github.io/mail/
- Add the
INTERNETpermission to yourAndroidManifest.xmlfile:
<uses-permission android:name="android.permission.INTERNET" />- Add the JavaMail Android dependencies to your app-level
build.gradlefile:
implementation 'com.sun.mail:android-mail:1.6.7'
implementation 'com.sun.mail:android-activation:1.6.7'
- Create a new Java class to send the email and import the necessary classes from the JavaMail API:
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import java.util.Properties;
import java.util.concurrent.Executors;- Use the following code to create a session and send the email:
// Set the email properties
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
// Create a session with the email properties
Session session = Session.getInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("[email protected]", "your_password");
}
});
try {
// Create the email message
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("[email protected]"));
message.setSubject("Email Subject");
message.setText("Email body");
// Send the email on a background thread to prevent NetworkOnMainThreadException
Executors.newSingleThreadExecutor().execute(() -> {
try {
Transport.send(message);
System.out.println("Email sent successfully!");
} catch (MessagingException e) {
e.printStackTrace();
}
});
} catch (MessagingException e) {
e.printStackTrace();
}Important Android Development Notes:
javax.*INTERNETjavax.*packages were removed from the platform, so you must use the Android-specific JavaMail port (added in step 3) to compile on modern SDK versions.- Network operations must run on a background thread to avoid
NetworkOnMainThreadException. - The API does not provide built-in delivery confirmation; verify successful sending by checking the recipient's inbox or the sender's "Sent" folder.
- Gmail requires an App-Specific Password for SMTP authentication. Standard account passwords will be rejected by Google's security policies. Generate one in your Google Account settings under Security > App Passwords.