smtplib 没有发送电子邮件,也没有出现错误
smtplib No email being sent and no errors appear
我正在尝试通过 smtplib 服务器发送消息:
msg = EmailMessage()
msg['Subject'] = 'Product in Stock Alert'
msg['From'] = 'sender'
msg['To'] = 'reciever'
msg.set_content("Hey, {} is now available in stock\n"
"Check it out soon : {}".format('hamza', 'google.com'))
with smtplib.SMTP('smtp.gmail.com', 587) as smtp:
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.login('gmail', 'code')
smtp.sendmail('sender', 'reciever' ,msg)
i 运行 它通过 cmd,但没有出现或发生任何事情,我在我的 gmail 帐户中启用了 lesssecureapps 并关闭了两步验证。
提前致谢
添加 .as_string() 方法到 msg:
smtp.sendmail('sender', 'reciever', msg.as_string())
因此,代码应如下所示:
msg = EmailMessage()
msg['Subject'] = 'Product in Stock Alert'
msg['From'] = 'sender'
msg['To'] = 'reciever'
msg.set_content("Hey, {} is now available in stock\n"
"Check it out soon : {}".format('hamza', 'google.com'))
with smtplib.SMTP('smtp.gmail.com', 587) as smtp:
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.login('gmail', 'code')
smtp.sendmail('sender', 'reciever', msg.as_string())
我正在尝试通过 smtplib 服务器发送消息:
msg = EmailMessage()
msg['Subject'] = 'Product in Stock Alert'
msg['From'] = 'sender'
msg['To'] = 'reciever'
msg.set_content("Hey, {} is now available in stock\n"
"Check it out soon : {}".format('hamza', 'google.com'))
with smtplib.SMTP('smtp.gmail.com', 587) as smtp:
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.login('gmail', 'code')
smtp.sendmail('sender', 'reciever' ,msg)
i 运行 它通过 cmd,但没有出现或发生任何事情,我在我的 gmail 帐户中启用了 lesssecureapps 并关闭了两步验证。 提前致谢
添加 .as_string() 方法到 msg:
smtp.sendmail('sender', 'reciever', msg.as_string())
因此,代码应如下所示:
msg = EmailMessage()
msg['Subject'] = 'Product in Stock Alert'
msg['From'] = 'sender'
msg['To'] = 'reciever'
msg.set_content("Hey, {} is now available in stock\n"
"Check it out soon : {}".format('hamza', 'google.com'))
with smtplib.SMTP('smtp.gmail.com', 587) as smtp:
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.login('gmail', 'code')
smtp.sendmail('sender', 'reciever', msg.as_string())