2010-07-27 198 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"