首先,我是python的初学者,我正在try 学习如何使用python发送邮箱.我在网上看到的所有教程都解释了如何使用Gmail.
为了确保您的帐户安全,从2022年5月30日起,谷歌将不再支持使用只要求您输入用户名和密码的第三方应用程序或设备.登录您的Google帐户.
来源:https://support.google.com/accounts/answer/6010255
那么我的问题是,有没有其他方法可以使用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)
如果您知道我可以更改它,或者您看到任何其他错误,这将对我有很大帮助!:-)
谢谢你的帮助