【问题标题】:Error when sending email with python smtplib使用 python smtplib 发送电子邮件时出错
【发布时间】:2023-02-11 03:07:52
【问题描述】:

我在使用 smtplib 发送电子邮件时遇到问题

这是代码

import smtplib
port = 587

sender_email='************'
password='***********'

reciever_email='*************'

s = smtplib.SMTP('smtp.titan.mail',465)

s.starttls()

s.login(sender_email,password)

message ="""\
subject:  python email

this message is sent from Python"""

s.sendmail(sender_email,reciever_email,message)

s.quit()

这是错误:

Traceback (most recent call last):
  File "tejmail.py", line 8, in <module>
    s = smtplib.SMTP('smtp.titan.mail',465)
  File "/usr/lib/python3.8/smtplib.py", line 255, 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 310, in _get_socket
    return socket.create_connection((host, port), timeout,
  File "/usr/lib/python3.8/socket.py", line 787, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "/usr/lib/python3.8/socket.py", line 918, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known

请帮助。

【问题讨论】:

  • 听起来像是网络/DNS 问题。你能从你尝试发送电子邮件的机器上解析 smtp.titan.mail 吗?

标签: python smtplib


【解决方案1】:

我有同样的问题。就我而言,这是因为我在学校网络上有一些相当激进的防火墙。它托管自己的 DNS 名称解析,但我无权访问本地 DNS 服务器。

我的解决方法是使用 replit.com 并将需要邮寄的数据发送到 replit API,然后 replit VM 可以发送电子邮件。显然不是一个很好的解决方案。我还必须一直运行 replit 程序和我的主要程序才能正常工作。

另一种可能的解决方案是运行您自己的 DNS 服务器,其中仅包含您需要与之通信的网站的缓存。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-01
    • 1970-01-01
    • 2021-04-26
    • 1970-01-01
    • 2014-01-31
    • 1970-01-01
    • 1970-01-01
    • 2011-02-12
    相关资源
    最近更新 更多