emailgmailgmail-apiapache-commons-email

Apache Commons Email work with "normal gmail", but not "Gmail for work"?


Hi I'm using the following code to send email:

public static void sendEmail(String from, String to, String password) {
    Email email = new SimpleEmail();
    email.setHostName("smtp.googlemail.com");
    email.setSmtpPort(465);
    email.setAuthenticator(new DefaultAuthenticator(from, password));
    email.setSSLOnConnect(true);
    email.setSubject("Plain mail");
    email.setMsg("test");
    email.addTo(to);
    email.send();
}

Now, it works when I'm calling this function with my "normal" gmail address:

sendMail("me@gmail.com","friend@gmail.com", "my-password");

So the above works. But when I'm trying to migrate to Gmail for Business, and create an email address "me@mycompany.com" (which is hooked to Gmail), I get an authentication error:

sendMail("me@mycompany.com","friend@gmail.com", "my-new-password");

Gives me this error:

javax.mail.AuthenticationFailedException:
<https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsb...ZlTLN2wQG4> 
Please log in via your web browser and then try again.

I suspect I need to set something in my Google Apps console, but I'm not even sure where to start looking for the info.

Can anybody help? Thanks.


Solution

  • This answer is coming from a similar SO question here.

    The issue is due to allowing less secure apps to access your account. Click the following link https://www.google.com/settings/security/lesssecureapps and disable security setting. For more information you can read this here.

    Good luck!