【问题标题】:Error to send e-mail using module smtplib使用模块 smtplib 发送电子邮件时出错
【发布时间】:2019-11-01 05:29:20
【问题描述】:

我正在编写代码以使用 Python 和本地服务器(我工作的地方)自动发送电子邮件。我不知道为什么会发生这个错误。

我尝试使用模块 smtplib -> smtplib.SMTP_SSL(hot, port) 和 smtplib.SMTP(hot,port) 中的命令连接服务器,但两者都不起作用。

import smtplib

server = smtplib.SMTP('IPfromCompanyServer')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\dbou\AppData\Local\Programs\Python\Python37\lib\smtplib.py", line 251, in __init__
    (code, msg) = self.connect(host, port)
  File "C:\Users\dbou\AppData\Local\Programs\Python\Python37\lib\smtplib.py", line 336, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "C:\Users\dbou\AppData\Local\Programs\Python\Python37\lib\smtplib.py", line 307, in _get_socket
    self.source_address)
  File "C:\Users\dbou\AppData\Local\Programs\Python\Python37\lib\socket.py", line 727, in create_connection
    raise err
  File "C:\Users\dbou\AppData\Local\Programs\Python\Python37\lib\socket.py", line 716, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

【问题讨论】:

  • 你试过阅读错误信息吗?)

标签: python module smtplib


【解决方案1】:

确保您访问的邮件服务器 IP 和端口号正确。并且某些客户端(例如 Gmail)不允许您在不禁用安全邮件传输功能的情况下自动发送邮件。

此外,您可以在登录之前将这段代码添加到您的程序中。

 try:
        self.smtp.ehlo()
        self.smtp.starttls()
        self.smtp.ehlo
 except:
        print "No TLS "
 #login here

也许这个链接有点帮助: https://superuser.com/questions/1292420/sending-an-email-from-python-using-local-python-smtp-server

【讨论】:

    猜你喜欢
    • 2023-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-20
    • 2011-11-06
    • 2022-07-21
    • 1970-01-01
    • 2018-12-09
    相关资源
    最近更新 更多