簡體   English   中英

TimeoutError: [WinError 10060] 連接嘗試失敗,因為連接方在一段時間后沒有正確響應或###

[英]TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time or###

  1. 我正在為“發送 gamil”編寫一個python 程序,但我們得到的錯誤名稱為“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

請幫助解決這個問題

我編寫了以下適用於我的實用程序 class:

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 調試服務器可視化 email 的示例,但實際上並沒有發送它們。 它對於測試目的非常有用。 要啟動調試服務器,只需在終端或 windows cli(例如命令提示符或 powershell)中鍵入python -m smtpd -c DebuggingServer -n localhost:1025

要發送真實的電子郵件,只需將 arguments serverport替換為真實值,並根據需要選擇使用標志htmluse_ssl

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM