繁体   English   中英

如何发送 email 与 python

[英]How to send email with python

import smtplib, ssl

port = 587  # For starttls
smtp_server = "smtp.gmail.com"
sender_email = "jppythons@gmail.com"
receiver_email = "jppythons@gmail.com"
password = input("pass")
message = """\
Subject: Email Test1

First eamil ever using Python."""

context = ssl.create_default_context()
with smtplib.SMTP(smtp_server, port) as server:
    server.ehlo()  # Can be omitted
    server.starttls(context=context)
    server.ehlo()  # Can be omitted
    server.login(sender_email, password)
    server.sendmail(sender_email, receiver_email, message)

当我运行我的代码时它唯一返回的是我的密码......没有其他任何事情发生,没有错误,也没有 email

我会使用 try except 这样你就可以查看是否有任何错误。 例如,

https://docs.python.org/3/tutorial/errors.html#handling-exceptions

try:
    server = smtplib.SMTP(smtp_server,port)
    server.ehlo()
    server.starttls(context=context)
    server.ehlo()
    server.login(sender_email, password)
    server.sendmail(sender_email, receiver_email, message)
except Exception as e:
    # Print any error messages to stdout
    print(e)
finally:
    server.quit()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM