0

由于某种原因,我在运行此代码时出错:

with smtplib.SMTP_SSL('smtp.yandex.com.tr', 465) as server:
    server.login(email_from, password_from)
    server.sendmail(
        from_addr='mymail@yandex.ru',
        to_addrs='yourmail@yandex.ru',
        msg='Hello'
    )

错误是:

Traceback (most recent call last):
  File "report.py", line 86, in <module>
    send_report()
  File "report.py", line 82, in send_report
    notificate(*create_html_msg())
  File "report.py", line 69, in notificate
    with smtplib.SMTP_SSL('smtp.yandex.com.tr', 465, context=context) as server:
  File "/usr/lib/python3.8/smtplib.py", line 1034, in __init__
    SMTP.__init__(self, host, port, local_hostname, timeout,
  File "/usr/lib/python3.8/smtplib.py", line 253, in __init__
    (code, msg) = self.connect(host, port)
  File "/usr/lib/python3.8/smtplib.py", line 339, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/usr/lib/python3.8/smtplib.py", line 1040, in _get_socket
    new_socket = socket.create_connection((host, port), timeout,
  File "/usr/lib/python3.8/socket.py", line 808, in create_connection
    raise err
  File "/usr/lib/python3.8/socket.py", line 796, in create_connection
    sock.connect(sa)
OSError: [Errno 101] Network is unreachable

操作系统是 Ubuntu 20.04。我的代码在另一台机器上完美运行,所以问题显然出在机器的某个地方。

我试过了:

sudo ufw allow 465/tcp

我还安装并运行了 postfix 邮件服务器并将其添加到 /etc/postfix/main.cf:

smtps     inet  n       -       y       -       -       smtpd
  -o syslog_name=postfix/smtps
  -o smtpd_tls_wrappermode=yes
  -o smtpd_sasl_auth_enable=yes
  -o smtpd_recipient_restrictions=permit_mynetworks,permit_sasl_authenticated,reject
  -o content_filter=smtp-amavis:[127.0.0.1]:10026

然后重新启动后缀

sudo systemctl restart postfix

但没有什么能解决问题。请帮我解决这个错误。

4

0 回答 0