使用python发送gmail失败

时间:2015-12-26 06:03:51

标签: python anaconda smtplib

1。点错误

我做了

pip install smtplib

然后它说

Could not find a version that satisfies the requirement smtplib (from versions: ) No matching distribution found for smtplib

我确认了

pip install youtube_dl

的工作原理。所以pip可能不是问题。

2。导入错误

无论如何,我正在运行的代码是

import smtplib

GMAIL_USERNAME = "something"
GMAIL_PASSWORD = "something"
recipient = "something"
body_of_email = "body"
email_subject = "ha"

# The below code never changes, though obviously those variables need values.
session = smtplib.SMTP('smtp.gmail.com', 587)
session.ehlo()
session.starttls()
session.login(GMAIL_USERNAME, GMAIL_PASSWORD)


headers = "\r\n".join(["from: " + GMAIL_USERNAME,
                       "subject: " + email_subject,
                       "to: " + recipient,
                       "mime-version: 1.0",
                       "content-type: text/html"])

# body_of_email can be plaintext or html!                    
content = headers + "\r\n\r\n" + body_of_email
session.sendmail(GMAIL_USERNAME, recipient, content)

但Anaconda提示显示错误

ImportError: cannot import name 'SMTP'

无法弄清楚为什么会发生这种奇怪的事情......

0 个答案:

没有答案