2012-11-16 46 views
1

我想从我的服务器发送电子邮件。我正尝试使用我已有的hotmail帐户发送。通过python中的hotmail发送电子邮件

user = "[email protected]" 
passwd = "xxxxxxxxx" 

from_addr = "[email protected]" 
to_addr = "[email protected]" 
smtp_srv = "smtp.live.com" 

subject = "Home Server Uptime" 
message = "The server has been online for: %s" %(uptime) 

smtp = smtplib.SMTP(smtp_srv,587) 
smtp.ehlo() 
smtp.starttls() 
smtp.ehlo() 
smtp.login(user, passwd) 
smtp.sendmail(from_addr, to_addr, message) 
smtp.quit() 

我得到这个错误:

Traceback (most recent call last): 
    File "sendemail.py", line 23, in <module> 
    smtp.starttls() 
    File "/usr/lib/python2.7/smtplib.py", line 636, in starttls 
    (resp, reply) = self.docmd("STARTTLS") 
    File "/usr/lib/python2.7/smtplib.py", line 385, in docmd 
    return self.getreply() 
    File "/usr/lib/python2.7/smtplib.py", line 358, in getreply 
    + str(e)) 
smtplib.SMTPServerDisconnected: Connection unexpectedly closed: [Errno 104] Connection reset by peer 

我不知道如何解决这个。

这里是set_debuglevel的输出(1)弥敦道建议:

send: 'ehlo [127.0.1.1]\r\n' 
reply: '250-BLU0-SMTP190.blu0.hotmail.com Hello [my-ip-address]\r\n' 
reply: '250-TURN\r\n' 
reply: '250-SIZE 41943040\r\n' 
reply: '250-ETRN\r\n' 
reply: '250-PIPELINING\r\n' 
reply: '250-DSN\r\n' 
reply: '250-ENHANCEDSTATUSCODES\r\n' 
reply: '250-8bitmime\r\n' 
reply: '250-BINARYMIME\r\n' 
reply: '250-CHUNKING\r\n' 
reply: '250-VRFY\r\n' 
reply: '250-TLS\r\n' 
reply: '250-STARTTLS\r\n' 
reply: '250 OK\r\n' 
reply: retcode (250); Msg: BLU0-SMTP190.blu0.hotmail.com Hello [my-ip-address] 
TURN 
SIZE 41943040 
ETRN 
PIPELINING 
DSN 
ENHANCEDSTATUSCODES 
8bitmime 
BINARYMIME 
CHUNKING 
VRFY 
TLS 
STARTTLS 
OK 
send: 'STARTTLS\r\n' 
Traceback (most recent call last): 
    File "sendemail.py", line 24, in <module> 
    smtp.starttls() 
    File "/usr/lib/python2.7/smtplib.py", line 636, in starttls 
    (resp, reply) = self.docmd("STARTTLS") 
    File "/usr/lib/python2.7/smtplib.py", line 385, in docmd 
    return self.getreply() 
    File "/usr/lib/python2.7/smtplib.py", line 358, in getreply 
    + str(e)) 
smtplib.SMTPServerDisconnected: Connection unexpectedly closed: [Errno 104] Connection reset by peer 
+0

你试过'smtp.set_debuglevel(1)'?你可能会得到更多有用的输出。 –

+0

在主文章中粘贴了set_debuglevel(1)的输出 – koogee

回答

1

看起来试图启动TLS(smtp.starttls())当你的代码失败。我刚刚测试了smtp.live.com,我可以在您的代码中尝试登录,因此可能是由于您的网络设置方式。

这是不相关的,但根据smtplib文档,你必须在消息的开头添加fromto地址标题​​:

parts = ("From: " + from_addr, 
     "To: " + to_addr, 
     "Subject: " + subject, 
     "", 
     message)  
msg = '\r\n'.join(parts) 
+0

添加了'msg =“到:%s \ n来自:%s \ n主题:%s \ n \ n%s”%(to_addr,from_addr,subject,message) '并更改了'smtp.sendmail(from_addr,to_addr,msg)'。还是同样的错误:( – koogee

+0

它基本上是一个运行samba连接到路由器的Ubuntu文件服务器。我没有设置任何邮件服务器或网络的任何特殊更改。我关掉了防火墙,看看是否导致问题仍然无法通过Hotmail进行身份验证可以通过网络浏览器进行身份验证 – koogee

+1

这似乎适用于OSX和Ubuntu Server上的我 –

7

试试这个

import email 
import smtplib 

msg = email.message_from_string('warning') 
msg['From'] = "[email protected]" 
msg['To'] = "[email protected]" 
msg['Subject'] = "helOoooOo" 

s = smtplib.SMTP("smtp.live.com",587) 
s.ehlo() # Hostname to send for this command defaults to the fully qualified domain name of the local host. 
s.starttls() #Puts connection to SMTP server in TLS mode 
s.ehlo() 
s.login('[email protected]', 'pass') 

s.sendmail("[email protected]", "[email protected]", msg.as_string()) 

s.quit() 
+5

您应该解释为什么您的代码正在修复提问者问题。 – morkro