Ivan Grishaev
Ivan Grishaev

Reputation: 1681

Can't connect to Gmail SMTP secured with 2FA

For some reason, I cannot send an email from my application using Google SMTP protocol. I receive a message that looks like that:

1. Unhandled javax.mail.AuthenticationFailedException
   534-5.7.14
   <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbt0
   534-5.7.14
   joZVenRyJq1oMRdChfymgOOgcAmikBwqT-_rFkNsHtvsquevNauUwW34Ksg5n8kXxPTALF
   534-5.7.14
   SYbdpH4O4GEE2TXaQmHScXAY15r_00D-lmyVzoJs54ba6RrOYb-yVcFNpsZbxxonQckTgd
   534-5.7.14
   dxdnp69p7t3_xj5523el2LWvh_HPE2lKTgOr8yLkeXjrFKYhvJT1jRhb5-Sy3Rs3G7OjuS
   534-5.7.14 GdcriEyrrStn9Re4Vq7U46c5f8SwM> Please log in via your
   web browser and 534-5.7.14 then try again.  534-5.7.14 Learn more
   at 534 5.7.14 https://support.google.com/mail/answer/78754
   c12-v6sm1390727lji.59 - gsmtp

Some facts:

Code (I use Clojure):

(def cred {:host "imap.gmail.com" 
           :user "user@my_domain.net" 
           :pass "application_password" 
           :ssl true})
(send-message cred {:from "[email protected]" 
                    :to "[email protected]" 
                    :subject "hello" 
                    :body "test"})

UPD: I also tried to follow the link from the response. It prompts me for 2FA and then redirects me to my account settings. Sending an email still doesn't work.

Upvotes: 1

Views: 4991

Answers (2)

kdub
kdub

Reputation: 61

Not exactly sure which options the answer Ivan provided is referring to. They don't appear in my non-gapps account perhaps due to a UI update.

I had a similar problem with a nas cage configured to use a Gmail SMTP to send alert emails. Once I enabled 2FA on the Gmail account the emails would fail.

The solution was to create an 'App Password' under the google account security tab. Essentially these are passwords created on a per-app basis for programs/devices that do not support 2FA. Once you create the password, you use it in place of the original Gmail password for that specific app only. Here's a direct link to the Google Security App Password Page

Upvotes: 4

Ivan Grishaev
Ivan Grishaev

Reputation: 1681

After 8 hours of trying this and that I found an option in Google Apps. Go to Security -> Advanced -> Check "Allow users to manage their security settings" (it was disabled). Now it works, oh gosh.

Upvotes: 1

Related Questions