【问题标题】:Error SMTP can't send an email from gmail错误 SMTP 无法从 gmail 发送电子邮件
【发布时间】:2022-01-21 02:55:23
【问题描述】:

我目前正在尝试使用 smtp 将电子邮件从我的 gmail 发送到另一个雅虎电子邮件地址。我从我的 Gmail 帐户启用了不太安全的应用程序。我也通过了 Gmail Captcha。我也尝试添加一个端口号(587、2525),但没有成功。我无法让代码工作。我在这里发布了我的代码,然后是错误消息。

奇怪的是,几个月前这对我有用!

import requests
from bs4 import BeautifulSoup
# import lxml
import smtplib

my_email = "MY_EMAIL@gmail.com"
password = "MY_PASSWORD"

URL = "https://www.amazon.com/Instant-Pot-Plus-60-Programmable/dp/B01NBKTPTS/ref=sr_1_1_sspa?crid=34PO9PKK0CUSX" \
      "&keywords=instant%2Bpot&qid=1639842722&sprefix=instant%2Caps%2C76&sr=8-1-spons&spLa=ZW5jcnlwdGVkUXVhbGlmaWV" \
      "yPUEyUlM1TDQ5N0RQS1VXJmVuY3J5cHRlZElkPUEwNzc4NTk5M0c2VVhTVlE1Q1lBTyZlbmNyeXB0ZWRBZElkPUEwMjE0NzMyNUlTVUg0V1" \
      "BYQTZWJndpZGdldE5hbWU9c3BfYXRmJmFjdGlvbj1jbGlja1JlZGlyZWN0JmRvTm90TG9nQ2xpY2s9dHJ1ZQ&th=1 "

TARGET_PRICE = 200

headers = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36",
    "Accept-Language": "en-GB,en-US;q=0.9,en;q=0.8"
}

response = requests.get(URL, headers=headers)
website_html = response.text

print(response.status_code)

soup = BeautifulSoup(website_html, "lxml")

price = float(soup.find(id="attach-base-product-price").attrs["value"])
title = soup.find(id="productTitle").get_text()


# sending a notification email
if price <= TARGET_PRICE:
    message = f"{title} is now {price}"

    with smtplib.SMTP("smtp.gmail.com") as connection:
        connection.starttls()
        result = connection.login(my_email, password)
        connection.sendmail(
            from_addr=my_email,
            to_addrs="SOME_EMAIL@yahoo.com",
            msg=f"Subject:Amazon Price Alert!\n\n{message}\n{URL}"
        )

print("done")

错误信息

Traceback (most recent call last):
  File "/Users/Charaf/PycharmProjects/Intermediate+/AmazonPriceTracker/main.py", line 45, in <module>
    with smtplib.SMTP("smtp.gmail.com") as connection:
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 255, in __init__
    (code, msg) = self.connect(host, port)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 341, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 312, in _get_socket
    return socket.create_connection((host, port), timeout,
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/socket.py", line 843, in create_connection
    raise err
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/socket.py", line 831, in create_connection
    sock.connect(sa)
TimeoutError: [Errno 60] Operation timed out

【问题讨论】:

    标签: python email smtp


    【解决方案1】:

    您是否尝试过使用 Google 应用密码 (https://support.google.com/accounts/answer/185833?hl=en)?我刚刚用我的亚马逊价格检查器对其进行了测试,该检查器非常相似,并且可以在我的 Google 应用密码上运行。

    不过这是我通常用来发送邮件的方式-

        def Send_Mail(email,password,remail): #Function for sending a mail to the user 
        server = smtplib.SMTP('smtp.gmail.com',587)
        server.ehlo()
        server.starttls()
        server.ehlo()
        
        server.login(email,password)
        
        body = f"Price Fell Down\n\nCheck -{url}"
        
        server.sendmail(email,remail,body)
    

    【讨论】:

    • 不幸的是,我用这种方法得到了同样的错误!
    【解决方案2】:

    Gmail 默认使用隐式 TLS 连接 (SSL)。

    这意味着您不会升级与 starttls() 的 Gmail 连接。相反,您连接到不同的端口; ssl 端口465

    删除:

    server.starttls()
    server.ehlo()
    

    ..并且只保留一个server.ehlo(),当然连接到端口465

    在下面的列表中,您的代码应该适用于 Gmail 和 Rackspace 以外的任何服务。

    • GMAIL.COM - 隐式 TLS 连接 (SSL)
    • RACKSPACE.COM - 隐式 TLS 连接 (SSL)
    • MAILTRAP.COM - 显式 TLS 连接 (STARTTLS)
    • iCLOUD.COM - 显式 TLS 连接 (STARTTLS)
    • 展望 - 显式 TLS 连接 (STARTTLS)
    • OUTLOOK.COM - 显式 TLS 连接 (STARTTLS)
    • YAHOO.COM - 显式 TLS 连接 (STARTTLS)
    • MAIL.COM - 显式 TLS 连接 (STARTTLS)
    • AOL.COM - 显式 TLS 连接 (STARTTLS)
    • COMCAST.NET - 显式 TLS 连接 (STARTTLS)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-21
      • 2011-11-30
      • 2016-08-24
      • 2021-07-06
      • 2013-11-24
      • 2013-06-24
      • 1970-01-01
      相关资源
      最近更新 更多