2010-07-27 161 views
4

我使用下面的脚本向我发送电子邮件,脚本运行正常,没有错误,但我实际上没有收到电子邮件。Python:电子邮件问题

import smtplib 

sender = '[email protected]' 
receivers = ['[email protected]'] 

message = """From: From Person <[email protected]> 
To: To Person <[email protected]> 
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" 

编辑

脚本就是为什么你使用localhost作为SMTP命名为test.py

+4

如果我身体收到了一封电子邮件,我得有与我的谈话缩水。 – 2010-07-27 19:09:29

回答

4

如果你正在使用你需要使用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() 
+1

我收到一个NameError:多部分未定义 – Phil 2010-07-27 19:31:34

+0

@Phil你是否正确导入它? from email.mime.multipart import MIMEMultipart – krs1 2010-07-27 19:38:37

+0

抱歉,导入中存在拼写错误 – Phil 2010-07-27 19:46:17

1

杰夫·阿特伍德blog post从去年四月可能会有所帮助。

0

“localhost”SMTP服务器不适用于Hotmail。您必须对密码进行硬编码,以便Hotmail也可以对您进行身份验证。 Hotmail的默认的SMTP是在端口25上尝试“smtp.live.com”:

import smtplib 

sender = '[email protected]' 
receivers = ['[email protected]'] 
password = 'your email password' 

message = """From: From Person <[email protected]> 
To: To Person <[email protected]> 
Subject: SMTP e-mail test 

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

try: 
    smtpObj = smtplib.SMTP("smtp.live.com",25) 
    smtpObj.ehlo() 
    smtpObj.starttls() 
    smtpObj.ehlo() 
    smtpObj.login(sender, password) 
    smtpObj.sendmail(sender, receivers, message)   
    print "Successfully sent email" 
except SMTPException: 
    print "Error: unable to send email"