Ask Your Question
0

How can JavaMail API be utilized on Android?

asked 2023-07-12 13:53:20 +0000

ladyg gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
3

answered 2023-07-12 14:14:02 +0000

bukephalos gravatar image

The JavaMail API can be utilized on Android using the following steps:

  1. Add JavaMail library in the build.gradle file of your Android project.
dependencies {
   compile 'com.sun.mail:android-mail:1.5.5'
   compile 'com.sun.mail:android-activation:1.5.5’
}
  1. Create a JavaMail session object by setting the mail properties such as host, port, authentication, and protocol.
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");
  }
});
  1. Create a MimeMessage object and set the message content, recipient, subject, and sender.
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!");
  1. Use the Transport class to send the MimeMessage.
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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-07-12 13:53:20 +0000

Seen: 9 times

Last updated: Jul 12 '23