首先,我是python的初学者,我正在try 学习如何使用python发送邮箱.我在网上看到的所有教程都解释了如何使用Gmail.

为了确保您的帐户安全,从2022年5月30日起,谷歌将不再支持使用只要求您输入用户名和密码的第三方应用程序或设备.登录您的Google帐户.

来源:https://support.google.com/accounts/answer/6010255

And we get: enter image description here

那么我的问题是,有没有其他方法可以使用python发送邮箱(包括发送到其他公司的邮箱帐户)?

顺便说一句,我的功能是发送邮箱:

def send_email_fct(filename, filepath, fromaddr, mdpfrom, toaddr):
"""" filename: file name to be sent with extension
     filepath: file path of the file to be sent
     fromaddr: sender email address
     mdpfrom: password of sender email address
     toaddr: receiver email address"""

msg = MIMEMultipart()  # instance of MIMEMultipart
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "data file"

body_email = "Body_of_the_mail"
msg.attach(MIMEText(body_email, 'plain'))

attachment = open(filepath, 'rb')  # open the file to be sent

p = MIMEBase('application', 'octet-stream')  # instance of MIMEBase
p.set_payload(attachment.read())
encoders.encode_base64(p)
p.add_header('Content-Disposition', "attachment; filename= %s" % filename)

msg.attach(p)  # attach the instance 'p' to instance 'msg'

s = smtplib.SMTP('smtp.gmail.com', 587)  # SMTP
s.starttls()
s.login(fromaddr, mdpfrom)

text = msg.as_string()

s.sendmail(from_email_addr, toaddr, text)  # sending the email

s.quit()  # terminating the session

我得到这个错误:

smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials c12-20020aa7d60c000000b0042be14040c1sm2612116edr.86 - gsmtp')

为了解决这个问题,我认为唯一需要更改的行是:

s = smtplib.SMTP('smtp.gmail.com', 587)

如果您知道我可以更改它,或者您看到任何其他错误,这将对我有很大帮助!:-)

谢谢你的帮助

推荐答案

这里有一个更精确的答案,包括所有主要步骤.我希望它能帮助其他人.

1/登录您的邮箱帐户:https://myaccount.google.com

2/然后转到安全部分

enter image description here

确保已打开两步验证并单击"应用程序密码"

enter image description here

3/ Select 邮箱和相应设备后

enter image description here

4/它将生成一个如下所示的密码,您必须在python脚本中使用该密码.

enter image description here

5/此密码将出现在此处和此处,您可以 Select 将其删除(因此连接到您的邮箱帐户将不再有用).

enter image description here

希望它能帮助其他人

Python相关问答推荐

配置Sweetviz以分析对象类型列,而无需转换

如何在msgraph.GraphServiceClient上进行身份验证?

Pandas实际上如何对基于自定义的索引(integer和非integer)执行索引

为什么我的Python代码在if-else声明中的行之前执行if-else声明中的行?

Pytest两个具有无限循环和await命令的Deliverc函数

为什么这个带有List输入的简单numba函数这么慢

对所有子图应用相同的轴格式

numpy卷积与有效

avxspan与pandas period_range

所有列的滚动标准差,忽略NaN

迭代嵌套字典的值

提取相关行的最快方法—pandas

如何从列表框中 Select 而不出错?

matplotlib + python foor loop

处理具有多个独立头的CSV文件

pandas:在操作pandora之后将pandora列转换为int

Python Mercury离线安装

有没有办法在不先将文件写入内存的情况下做到这一点?

我怎么才能用拉夫分拣呢?

类型对象';敌人';没有属性';损害';