简体   繁体   中英

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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM