Python:电子邮件问题

Phi*_*hil 4 python email

我正在使用以下脚本向自己发送电子邮件,该脚本运行正常,没有错误,但我没有收到任何电子邮件。

import smtplib

sender = 'foo@hotmail.com'
receivers = ['foo@hotmail.com']

message = """From: From Person <foo@hotmail.com>
To: To Person <foo@hotmail.com>
Subject: SMTP e-mail test

This is a test e-mail message.
"""

try:
   smtpObj = smtplib.SMTP('localhost')
   smtpObj.sendmail(sender, receivers, message)         
   print "Successfully sent email"
except SMTPException:
   print "Error: unable to send email"
Run Code Online (Sandbox Code Playgroud)

编辑

脚本名为 test.py

Kla*_*ark 5

为什么要使用localhost作为SMTP?

如果您正在使用hotmail,则需要使用hotmail帐户,提供密码,输入端口和SMTP服务器等。

这是您需要的一切:http : //techblissonline.com/hotmail-pop3-and-smtp-settings/

编辑:这是使用gmail的示例:

def mail(to, subject, text):
    msg = MIMEMultipart()

    msg['From'] = gmail_user
    msg['To'] = to
    msg['Subject'] = subject

    msg.attach(MIMEText(text))

    part = MIMEBase('application', 'octet-stream')
    Encoders.encode_base64(part)
    msg.attach(part)

    mailServer = smtplib.SMTP("smtp.gmail.com", 587)
    mailServer.ehlo()
    mailServer.starttls()
    mailServer.ehlo()
    mailServer.login(gmail_user, gmail_pwd)
    mailServer.sendmail(gmail_user, to, msg.as_string())
    # Should be mailServer.quit(), but that crashes...
    mailServer.close()
Run Code Online (Sandbox Code Playgroud)