[英]Error SMTP can't send an email from gmail
我目前正在尝试使用smtp
将 email 从我的 gmail 发送到另一个雅虎 Z0C83F57C786AC0BEBCZ 地址。 我从我的 Gmail 帐户启用了不太安全的应用程序。 我还通过了 Gmail 验证码。 我也尝试添加一个端口号(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
您是否尝试过使用 Google 应用密码 ( https://support.google.com/accounts/answer/185833?hl=en )? 我刚刚用我的亚马逊价格检查器对其进行了测试,它非常相似,它适用于我的,使用谷歌应用程序密码。
这是我通常用来发送邮件的方式-
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)
Gmail 默认使用隐式 TLS 连接 (SSL)。
这意味着您不会使用starttls()
升级 Gmail 连接。 相反,您连接到不同的端口; ssl 端口465
。
消除:
server.starttls()
server.ehlo()
..并且只保留一个server.ehlo()
而当然连接到端口465
。
在下面的列表中,您的代码应该适用于除 Gmail 和 Rackspace 之外的任何服务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.