![](/img/trans.png)
[英]How can I send an email from Gmail with Python + Selenium?
[英]How can I send email from python with 2 Factor Authentication?
我创建了 python 文件,该文件可以使用打开的安全性较低的应用程序发送 email,但我需要将其关闭。 如何发送带有 2FA 的 email?
# import simple mail transfer protocol library
import smtplib
# import EmailMessage method
from email.message import EmailMessage
contacts = ['<email@email.com>', '<email2@email.com>']
EMAIL_ADDRESS = '<my_gmail>'
EMAIL_PASSWORD = '<my_gmail_password>'
# Create empty Email Message object
msg = EmailMessage()
msg['Subject'] = 'Automated python email sender 5'
msg['From'] = EMAIL_ADDRESS
msg['To'] = contacts
msg.set_content('<sample_content>')
# contact manager we will make sure our connection is closed automatically without us doing it manually
# 465 is the port number for plain SMTP_SSL
# SMTP_SSL means you do not have to use ehlo() ans starttls()
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
smtp.send_message(msg)
我应该添加什么才能使 2FA 正常工作?
尝试为您的 Gmail 设置应用密码。 并使用该应用程序密码进行 smtp 登录。
这将有助于设置 APP-Password
在安全性下,您必须为您的应用程序生成密码:
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login('demo@gmail.com', 'password generated by google')
server.sendmail('demo@gmail.com', 'demo@icloud.com', 'Mail sent from program')
print('Mail Sent')
如您所见,您输入了 email 但不是输入您自己的密码,而是输入谷歌为您的应用程序生成的密码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.