首页 > 解决方案 > smtplib.SMTPConnectError: (421, b'无法连接到 SMTP 服务器 2a00:1450:400c:c09::6c (2a00:1450:400c:c09::6c:25),连接错误 10060')

问题描述

我想知道为什么这段代码不起作用。我在 VSCode,python 3.8.0-32bit。我不知道这个问题是来自代码还是来自我应该对我的 gmail 执行的操作。你能告诉我这个吗


sender_email = input(str("enter your email address: "))
password = input(str("enter your password: "))
rec_email = input(str("enter the email address to whom you want to send the message: "))
message = str("message envoyé avec python")
print(".")
server = smtplib.SMTP('smtp.gmail.com')
print(".")
server.starttls()
print(".")
server.login(sender_email,password)
print("login success")
server.sendmail(sender_email,rec_email,message)
print("email has be sent to", rec_email)```
And python sends me back:
```Windows PowerShell
Copyright (C) Microsoft Corporation. Tous droits réservés.

Testez le nouveau système multiplateforme PowerShell https://aka.ms/pscore6

PS C:\Users\*************> & C:/Users/evain/AppData/Local/Programs/Python/Python38-32/python.exe c:/Users/evain/OneDrive/Bureau/mail.py
enter your email address: ev******************4@gmail.com
enter your password: **********************
enter the email address to whom you want to send the message: ma*****************1@gmail.com
.
Traceback (most recent call last):
  File "c:/Users/***********/OneDrive/Bureau/mail.py", line 8, in <module>
    server = smtplib.SMTP('smtp.gmail.com')
  File "C:\Users\***********\AppData\Local\Programs\Python\Python38-32\lib\smtplib.py", line 256, in __init__
    raise SMTPConnectError(code, msg)
smtplib.SMTPConnectError: (421, b'Cannot connect to SMTP server 2a00:1450:400c:c09::6c (2a00:1450:400c:c09::6c:25), connect error 10060')```

标签: pythonemailsmtplib

解决方案


我的 Outlook 设置显示服务器“smtp.gmail.com”使用端口 465 和 SSL/TLS 加密。因此我使用:server = smtplib.SMTP_SSL('smtp.gmail.com') 而不是:server = smtplib.SMTP('smtp.gmail.com') server.starttls()。作为这个主题的新手,我在How to send an email with Gmail as provider using Python? 下找到了这个解决方案?.


推荐阅读