python- 发送电子邮件时出错

python- error sending an email

我可以从一个 gmail 帐户发送到另一个 gmail 帐户,从一个 outlook 帐户发送到另一个 outlook 帐户。 但是当我尝试从 outlook 发送邮件到 gmail 时遇到问题。

这是代码

import smtplib
content ='example email stuff here'
email='xyz@outlook.com'
password="password"
fromemail="xyz@outlook.com"
receive="abc@gmail.com"
mail=smtplib.SMTP('smtp-mail.outlook.com',587)
mail.ehlo()
mail.starttls()
mail.login(email,password)
mail.sendmail(fromemail,receive,content)
mail.close()

当我在 运行 脚本中出现以下错误

Traceback (most recent call last):
  File "smtp_google.py", line 7, in <module>
    mail=smtplib.SMTP('smtp-mail.outlook.com',587)
  File "C:\Python27\lib\smtplib.py", line 256, in __init__
    (code, msg) = self.connect(host, port)
  File "C:\Python27\lib\smtplib.py", line 316, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "C:\Python27\lib\smtplib.py", line 291, in _get_socket
    return socket.create_connection((host, port), timeout)
  File "C:\Python27\lib\socket.py", line 575, in create_connection
    raise err
socket.error: [Errno 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

解决方案 1:

尝试给出超时时间:

mail=smtplib.SMTP('smtp-mail.outlook.com',587)

作为

mail=smtplib.SMTP('smtp-mail.outlook.com',587, timeout=120)

或尝试通过 SSL 连接而不是 TLS/STARTTLS

    mail=smtplib.SMTP_SSL('smtp-mail.outlook.com',587)

解决方案 2: 您的电子邮件帐户必须允许 smtp,默认情况下不一定启用。您可以尝试按照此处给出的步骤更改设置 enable smtp authentication