首先,我是 python 的初學者,我正在嘗試學習如何使用 python 發送電子郵件。我在網路上標為紅色的所有教程都解釋了如何使用 Gmail 進行操作。但是,從 2022 年 5 月 30 日起(盡管每個人都可以自由地用他的帳戶做任何他想做的事)谷歌有一個新的政治規定:
為幫助確保您的帳戶安全,自 2022 年 5 月 30 日起,Google 將不再支持使用僅要求您提供用戶名和密碼的第三方應用或設備。登錄您的 Google 帳戶。
來源: https: 
所以我的問題是有沒有其他方法可以使用 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)
如果您知道我可以更改它,或者您看到任何其他錯誤,它將對我有很大幫助!:-)
謝謝您的幫助
uj5u.com熱心網友回復:
通過創建應用程式密碼解決了它。你必須到谷歌帳戶。安全選項卡,激活兩步驗證。在“登錄谷歌”下的這個新選項之后,“應用密碼”選項將被激活。只需創建一個應用密碼并用作驗證密碼
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/487343.html
