2

I am attempting to send an email, but I run into this error: smtplib.SMTPAuthenticationError: (534, b'5.7.9 Application-specific password required. Learn more at\n5.7.9 https://support.google.com/mail/?p=InvalidSecondFactor d2sm13023190qkl.98 - gsmtp')

In the web URL i dont see anything super useful, would anyone have any tips? For SO purposes I left the email account passwords as test versus sharing my person info..

import smtplib
import ssl


# User configuration
sender_email = '[email protected]'
receiver_email = '[email protected]'
password = 'test'



# Email text
email_body = '''
    This is a test email sent by Python. Isn't that cool?
'''

# Creating a SMTP session | use 587 with TLS, 465 SSL and 25
server = smtplib.SMTP('smtp.gmail.com', 587)
# Encrypts the email
context = ssl.create_default_context()
server.starttls(context=context)
# We log in into our Google account
server.login(sender_email, password)
# Sending email from sender, to receiver with the email body
server.sendmail(sender_email, receiver_email, email_body)
print('Email sent!')

print('Closing the server...')
server.quit()
4
  • "Application-specific password required" – do you have one? Commented Mar 31, 2020 at 17:44
  • 1
    You should consider using the formal gmail API Commented Mar 31, 2020 at 17:45
  • i runned the example in this page in my gmail account and it worked: julien.danjou.info/… Commented Mar 31, 2020 at 17:47
  • currently Gmail is more restricted then other mail servers and every application needs own password to access mails. Commented Mar 31, 2020 at 18:29

2 Answers 2

7

I tried my best... I think this should work!

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

email = "[email protected]" # the email where you sent the email
password = "yourPassword"
send_to_email = "[email protected]" # for whom
subject = "Gmail"
message = "This is a test email sent by Python. Isn't that cool?!"

msg = MIMEMultipart()
msg["From"] = email
msg["To"] = send_to_email
msg["Subject"] = subject

msg.attach(MIMEText(message, 'plain'))

server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login(email, password)
text = msg.as_string()
server.sendmail(email, send_to_email, text)
server.quit()
Sign up to request clarification or add additional context in comments.

Comments

1

You must allow the "Less secure apps" in Google configurations.

Here is the link of another thread about it : Link

1 Comment

This feature seems to be deprecated now. Google wont allow less secure apps.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.