Python: when sending email, always blocked in the clause: smtpserver = smtplib.SMTP(“smtp.gmail.com”,587)

前端 未结 4 1938
轮回少年
轮回少年 2021-01-19 08:40

I am writing a Python program to send an email. But every time when executing the clause:

smtpserver = smtplib.SMTP(\"smtp.gmail.com\",587)

4条回答
  •  鱼传尺愫
    2021-01-19 09:02

    There may be some issue with the connection (maybe it is being blocked by your proxy or firewall?) and the timeout may be pretty big for you to do not see it going further.

    The documentation of smtplib.SMTP says:

    class smtplib.SMTP([host[, port[, local_hostname[, timeout]]]])

    (...) An SMTPConnectError is raised if the specified host doesn’t respond correctly. The optional timeout parameter specifies a timeout in seconds for blocking operations like the connection attempt (if not specified, the global default timeout setting will be used).

    Try specifying the timeout yourself:

    smtpserver = smtplib.SMTP("smtp.gmail.com", 587, timeout=30)
    

提交回复
热议问题