无法使用python 3 smtplib连接到本地主机

时间:2019-02-20 17:36:00

标签: python python-3.x smtp smtplib

我正在尝试使用python smptlib发送邮件,但无法连接到本地主机

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart



msg=MIMEMultipart('related')
msg['Subject']='subject'
msg['From']='sender'
msg['To']='receiver'
ptmsg=MIMEMultipart('alternative')
msg.attach(ptmsg)
ptmsg.attach(MIMEText("THIS IS PLAIN TEXT",'text'))
s=smtplib.SMTP('localhost')
s.sendmail('sender','receiver',msg.as_string())

并且我遇到以下错误:

File "/usr/lib/python3.6/smtplib.py", line 251, in __init__
    (code, msg) = self.connect(host, port)
  File "/usr/lib/python3.6/smtplib.py", line 336, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/usr/lib/python3.6/smtplib.py", line 307, in _get_socket
    self.source_address)
  File "/usr/lib/python3.6/socket.py", line 724, in create_connection
    raise err
  File "/usr/lib/python3.6/socket.py", line 713, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused

0 个答案:

没有答案