The JavaMail API can be utilized on Android using the following steps:
dependencies {
compile 'com.sun.mail:android-mail:1.5.5'
compile 'com.sun.mail:android-activation:1.5.5’
}
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("your_email@gmail.com", "your_password");
}
});
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("your_email@gmail.com"));
message.addRecipient(Message.RecipientType.TO, new InternetAddress("recipient_email@example.com"));
message.setSubject("Test email from Android");
message.setText("Hello, this is a test email from Android!");
Transport.send(message);
Note: Make sure to perform network operations (such as sending emails) on a separate thread or using AsyncTask/Handler to avoid blocking the UI thread.
Asked: 2023-07-12 13:53:20 +0000
Seen: 9 times
Last updated: Jul 12 '23