【问题标题】:smptlib.SMTP throws an error while using 'localhost',why does it happen?smptlib.SMTP 在使用 'localhost' 时抛出错误,为什么会这样?
【发布时间】:2020-10-19 07:12:35
【问题描述】:

我只是想学习如何使用电子邮件模块和 smtplib 发送电子邮件。这就是我得到的。

[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import smtplib
>>> from email.message import EmailMessage
>>> with open("file.txt") as fp:
...     msg = EmailMessage()
...     msg.set_content(fp.read())
... 
>>> msg['Subject'] = f'The contents of file'
>>> msg['From'] = "s*i*a*d*1*@gmail.com"
>>> msg['To'] = "s*i*a*k*9*@gmail.com"
>>> s = smtplib.STMP('localhost')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'smtplib' has no attribute 'STMP'
>>> s = smtplib.SMTP('localhost')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  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

我想使用 localhost 托管它,但它会引发此错误。谁能告诉我怎么解决?

【问题讨论】:

    标签: python-3.x email localhost smtplib


    【解决方案1】:

    很可能没有 SMTP 在本地运行(接受传入的 SMTP 连接)。

    本地 SMTP 服务器是 Unix/Linux 上的默认设置,与 MS 操作系统不同。

    AFAIR 在 Ubuntu 上,本地 SMTP 服务器默认由 postfix 包提供。它也可以由 senndmail 或 exim 包(或一些不太流行的包)提供。

    【讨论】:

    • 那么,解决办法是什么??
    • @SairajK 命名您正在使用的操作系统(Windows/Linux)
    • Linux Ubuntu 18.04 LTS
    • 您是否安装了 MTA/SMTP 或 SMTP 服务器(包)? AFAIR Ubuntu 更喜欢 postfix(其他标准替代品是 sendmail 或 exim)。
    • 哦!好的,明白了。
    猜你喜欢
    • 2019-09-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-28
    • 2014-06-25
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多