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:
- Download the JavaMail API from the following link: https://javaee.github.io/javamail/
- Extract the downloaded file and add the
javax.mail.jar
file to your project's classpath. - 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 java.util.Properties;
- 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 javax.mail.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");
//