【问题标题】:TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time or###TimeoutError: [WinError 10060] 连接尝试失败,因为连接方在一段时间后没有正确响应或###
【发布时间】:2022-10-05 16:59:46
【问题描述】:
  1. 我正在写一个蟒蛇程序为了\'发送 gamil\'但我们得到的错误名称为\"TimeoutError: [WinError 10060] 连接尝试失败,因为连接方在一段时间后没有正确响应,或者连接失败,因为连接的主机没有响应\"

    2.程序代码为:

    import smtplib as s
    
    ob =s.SMTP(\"smtp.gamil.com\",587)
    ob.ehlo()
    ob.starttls()
    ob.login(\'pawarpraful950@gamil.com\',\'#######\')
    subject=\"test python\"
    body=\"I love python\"
    massage=\"subject:{}\\n\\n{}\".format(subject,body)
    listadd=[\'pnp950@gamil.com\']
    ob.sendmail(\'pawarpraful950@gamil.com\',listadd,massage)
    print(\"send mail\")
    ob.quit()
    

    2.错误如下:

    TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after 
    a period of time, or established connection failed because connected host has failed to respond
    

    请帮助解决这个问题

    标签: python jupyter-notebook smtp gmail timeoutexception


    【解决方案1】:

    我编写了以下适用于我的实用程序类:

    import ssl
    import smtplib
    from email.message import EmailMessage
    
    
    class Email:
        def __init__(self, server: str, port: int, address: str, password: str | None):
            self.server = server
            self.port = port
            self.address = address
            self.password = password
    
        def send_email(
                self, mail_to: str | list, subject: str, body: str, html: bool = False, use_ssl: bool = True
        ) -> None:
            """
            sending the email
    
            Args:
                mail_to: receiver, can be a list of strings
                subject: the subject of the email
                body: the body of the email
                html: whether the body is formatted with html or not
                use_ssl: whether to use a secure ssl connection and authentication
    
            Returns:
                None
            """
            if not isinstance(mail_to, str):
                mail_to = ', '.join(mail_to)
    
            if self.server == 'localhost':
                with smtplib.SMTP(self.server, self.port) as server:
                    message = f'Subject: {subject}\n\n{body}'
                    server.sendmail(self.address, mail_to, message)
                    return None
            else:
                mail = EmailMessage()
                mail['Subject'] = subject
                mail['From'] = self.address
                mail['To'] = mail_to
                if html:
                    mail.add_alternative(body, subtype='html')
                else:
                    mail.set_content(body)
    
            if use_ssl:
                with smtplib.SMTP_SSL(self.server, self.port, context=ssl.create_default_context()) as server:
                    server.login(self.address, self.password)
                    server.send_message(mail)
            else:
                with smtplib.SMTP(self.server, self.port) as server:
                    server.send_message(mail)
    
            return None
    

    你可以像这样使用它:

    my_email = Email(server='localhost', port=1025, address='sender@example.com', password='password')
    
    my_email.send_email(
        mail_to=['receiver1@example.com', 'receiver2@example.com'],
        subject='Email Subject',
        body='Email Body',
        html=False,
        use_ssl=False,
    )
    

    这是一个使用 python 调试服务器可视化电子邮件的示例,但实际上并未发送它们。它对于测试目的非常有用。要启动调试服务器,只需在终端或 Windows cli(例如命令提示符或 powershell)中键入 python -m smtpd -c DebuggingServer -n localhost:1025

    要发送真正的电子邮件,只需将参数serverport 替换为实际值,如果需要,还可以选择使用标志htmluse_ssl

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-06
      • 2022-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-17
      • 2013-06-13
      相关资源
      最近更新 更多