我无法使用python发送邮件

时间:2019-01-26 14:43:37

标签: python smtp

我的问题是,我无法使用Google的SMTP服务发送状态代码200。我使用pycharm检查我的代码是否有效。我已经导入了所有必需的软件包,也尝试了其他几封邮件,但结果相同(错误代码也未记录在邮件中)。

import requests

request = requests.get ('http://www.google.com')
if request.status_code == 200:
    print('Web site exists')
else:
    print('Web site does not exist')

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText


fromaddr = "MY ADDRESS"
toaddr = "ADDRESS I WANT TO SEND TO"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "SUBJECT OF THE MAIL"

body = "YOUR MESSAGE HERE"
msg.attach(MIMEText(body, 'plain'))

server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, "YOUR PASSWORD")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()

0 个答案:

没有答案