提问人:dmitrianosov 提问时间:10/22/2023 最后编辑:Mark Rotteveeldmitrianosov 更新时间:10/23/2023 访问量:92
通过 gmail 发送电子邮件的 Python 脚本拒绝接受用户名和应用密码?[复制]
Python script for sending an email via gmail refuses to accept username and app password? [duplicate]
问:
我正在尝试使用 python 学习一些电子邮件自动化。这是我到目前为止的脚本:
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
# Email configuration
sender_email = 'sendingemail@gmail'
sender_password = 'myapppassword'
receiver_email = '[email protected]'
subject = 'Subject of the Email'
message = 'This is the body of the email.'
# Create a MIMEText object to represent the email message
msg = MIMEMultipart()
msg['From'] = sender_email
msg['To'] = receiver_email
msg['Subject'] = subject
msg.attach(MIMEText(message, 'plain'))
# Connect to the SMTP server (in this example, Gmail)
smtp_server = 'smtp.gmail.com'
smtp_port = 587
try:
server = smtplib.SMTP(smtp_server, smtp_port)
server.starttls() # Secure the connection
server.login(sender_email, sender_password)
text = msg.as_string()
server.sendmail(sender_email, receiver_email, text)
print('Email sent successfully')
except Exception as e:
print('Email not sent. An error occurred:', str(e))
finally:
server.quit()
我不断收到此错误:
电子邮件未发送。发生错误:(535, b'5.7.8 不接受用户名和密码。有关详细信息,请参阅\n5.7.8 https://support.google.com/mail/?p=BadCredentials y63-20020a253242000000b00d7497467d36sm2031898yby.45 - gsmtp')
任何想法为什么这不起作用?
想要设置自动化脚本以使用 python 发送电子邮件。我只是想收到提到的电子邮件。
答:
0赞
Suraj Wate
10/23/2023
#1
这里的问题是,由于身份验证问题,Google不接受登录。它通常是在双因素身份验证打开时引起的。您可以登录以创建特定于应用程序的密码。或者更好的解决方案是使用像 Mailgun (https://www.mailgun.com/) 这样的服务,如果你将其用于测试目的。它很容易设置。您每天可以通过它发送有限数量的电子邮件。但出于学习目的,这就足够了。在那里,您将获得用于发送电子邮件的 api 和 smtp 的设置。您可以使用其中任何一个。https://myaccount.google.com/apppasswords
评论
1赞
dmitrianosov
10/23/2023
我已经为此制作了一个特定于应用程序的密码。由于某种原因,它仍然不接受它。
评论
sender_email
电子邮件
文档中的示例重新开始。