Apache commons mail not sending through gmail

Apache Commons Mail is a popular library for sending emails in Java, but it can be challenging to configure it to send emails through Gmail. Here are some common issues and solutions to help you troubleshoot:

Issue 1: Authentication failure

Issue 2: SMTP connection failure

Issue 3: Email not being sent

Apache Commons Mail configuration

Here's an example of how you can configure Apache Commons Mail to send emails through Gmail:

import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

public class GmailSender {
    public static void main(String[] args) {
        Email email = new SimpleEmail();
        email.setHostName("smtp.gmail.com");
        email.setSmtpPort(587);
        email.setAuthenticator(new DefaultAuthenticator("[email protected]", "your-password"));
        email.setFrom("[email protected]");
        email.setSubject("Test email");
        email.setMsg("This is a test email");
        try {
            email.send();
        } catch (EmailException e) {
            e.printStackTrace();
        }
    }
}

Additional tips

I hope these tips help you troubleshoot and resolve the issue with Apache Commons Mail not sending emails through Gmail!