在Python中通过Hotmail发送电子邮件



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

user = "myaddress@hotmail.com"
passwd = "xxxxxxxxx"
from_addr = "myaddress@hotmail.com"
to_addr = "someaddress@gmail.com"
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

我不知道如何对此进行故障排除。

以下是 Nathan 建议的 set_debuglevel(1) 的输出:

send: 'ehlo [127.0.1.1]rn'
reply: '250-BLU0-SMTP190.blu0.hotmail.com Hello [my-ip-address]rn'
reply: '250-TURNrn'
reply: '250-SIZE 41943040rn'
reply: '250-ETRNrn'
reply: '250-PIPELININGrn'
reply: '250-DSNrn'
reply: '250-ENHANCEDSTATUSCODESrn'
reply: '250-8bitmimern'
reply: '250-BINARYMIMErn'
reply: '250-CHUNKINGrn'
reply: '250-VRFYrn'
reply: '250-TLSrn'
reply: '250-STARTTLSrn'
reply: '250 OKrn'
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: 'STARTTLSrn'
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

试试这个

import email
import smtplib
msg = email.message_from_string('warning')
msg['From'] = "example1@hotmail.fr"
msg['To'] = "example2@hotmail.fr"
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('example1@hotmail.fr', 'pass')
s.sendmail("example1@hotmail.fr", "example2@hotmail.fr", msg.as_string())
s.quit()

尝试启动 TLS 时,您的代码似乎失败了 ( smtp.starttls() )。我刚刚测试了smtp.live.com,我能够进入您尝试登录的代码中的行,因此这可能是由于您的网络设置方式造成的。

这是无关的,但根据 smtplib 文档,您必须在消息开头添加fromto地址标头:

parts = ("From: " + from_addr,
         "To: " + to_addr,
         "Subject: " + subject,
         "",
         message)    
msg = 'rn'.join(parts)

使用 smtp-mail.outlook.com 作为邮件服务器。我测试了一下,它不是smtp.live.com.否则其余代码就可以了。尽管Gmail不允许以这种方式发送。

相关内容

  • 没有找到相关文章

最新更新