使用smtplib从python3.4发送电子邮件

时间:2016-07-08 09:34:58

标签: python python-3.4 smtplib

您好我正在尝试通过python发送电子邮件。我用这段代码发送:

    server = smtplib.SMTP(host='send.one.com',port=465)
    server.starttls()
    server.login(USER, PASS)
    text = msg.as_string()
    server.sendmail(mailFrom, mailTo, text)
    server.quit() 

但我在第一行收到错误:

File "/home/emil/Name_Generator/VoteMail.py", line 69, in sendVoteMail
    server = smtplib.SMTP(host='send.one.com',port=465)
  File "/usr/lib/python3.4/smtplib.py", line 242, in __init__
    (code, msg) = self.connect(host, port)
  File "/usr/lib/python3.4/smtplib.py", line 323, in connect
    (code, msg) = self.getreply()
  File "/usr/lib/python3.4/smtplib.py", line 376, in getreply
    raise SMTPServerDisconnected("Connection unexpectedly closed")
smtplib.SMTPServerDisconnected: Connection unexpectedly closed

有没有其他人尝试使用Python连接到one.com smtp服务器?

1 个答案:

答案 0 :(得分:0)

对于使用one.com作为您的电子邮件并且您想要连接到SMTP服务器的其他人,我发现他们使用SSL,这就是为什么我的代码没有以正确的方式工作如下:

server = smtplib.SMTP_SSL(host='send.one.com',port=465)
server.login(USER, PASS)
text = msg.as_string()
server.sendmail(mailFrom, mailTo, text)
server.quit()