Python 2.7 / Windows:发送SMTP电子邮件的奇怪行为

时间:2014-08-22 01:02:26

标签: email python-2.7

这让我完全难过。以下是发送电子邮件的两个简单例程。第一个成功,第二个(带有一些次要的异常捕获)锁定Python(Cntl-C不会终止会话)。此外,我已经确定用于“主题”行的报价类型很重要。所以,如果我使用

msg['Subject'] = "Random text"

电子邮件将失败,但

msg['Subject'] = 'Random text'

这成功了

有人可以为黑暗照亮一些光线:

示例#1

import smtplib
from email.mime.text import MIMEText
addr = "10.0.0.178"
From = "me@somewhere.net"
recip = ["me@somewherelese.net","foo@bar.net"]
msg = "Message Body"
send_email(recip,"Subject Line",msg)

def send_email(To,Subj,Mess):
    msg = MIMEText(Mess)
    msg["Subject"] = Subj
    msg['From'] = From
    s = smtplib.SMTP(addr)
    for entry in To:
        msg['To'] = entry
        s.sendmail(From, [entry], msg.as_string())
    s.quit()

示例2:

import smtplib
from email.mime.text import MIMEText
addr = "10.0.0.178"
From = "me@somewhere.net"
recip = ["me@somewherelese.net","foo@bar.net"]
msg = "Message Body"
send_email(recip,"Subject Line",msg)

def send_email(To,Subj,Mess):
    msg = MIMEText(Mess)
    msg["Subject"] = Subj
    msg['From'] = From
    s = smtplib.SMTP(addr)
    for entry in To:
        msg['To'] = entry
        try:
            s.sendmail(From, [entry], msg.as_string())
        except:
            print "Error sending email"
    s.quit()

0 个答案:

没有答案