谷歌有电子邮件发送限制吗?

2024-04-25 01:55:57 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是我正在做的一个有趣的项目的代码,用Python发送电子邮件。我想知道谷歌是否有一次发送电子邮件的限制,每次代码运行时都会在89处停止,并给出以下错误消息:raise SMTPServerDisconnected(“连接意外关闭”)smtplib.SMTPServerDisconnected:连接意外关闭如果有限制,我可以绕过它吗?如果不是,我做错了什么

import smtplib, ssl
import requests
import json
import socket
import time

from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

start = time.perf_counter()

url = "https://embassy.goabroad.com/api/embassies?host_country_id=91&limit=986"
response = requests.get(url)
data = response.text
parsed = json.loads(data) 

for i in range(0,985): #def func and run with diff positional values a,b

    email = parsed['embassies'][i]['email']
    whom = parsed['embassies'][i]['title']

    sender_email = "some@gmail.com"
    receiver_email = "someother@gmail.com" 
    password = 'passwordthing'  

    message = MIMEMultipart("alternative")
    message["Subject"] = "Epic Message!"
    message["From"] = sender_email
    message["To"] = receiver_email

    text = f"Epic message"

    part1 = MIMEText(text, "plain")

    message.attach(part1)

    with smtplib.SMTP_SSL("smtp.gmail.com", 465) as server:
        server.login(sender_email, password)
        server.sendmail(
            sender_email, receiver_email, message.as_string()
        )

    print("Email sent to: ",whom)
    print("Email number: ",i+1)

end = time.perf_counter()

print(f'Finished in {round(end-start,2)} seconds')


#raise SMTPServerDisconnected("Connection unexpectedly closed")
#smtplib.SMTPServerDisconnected: Connection unexpectedly closed

Tags: textimportcommessageservertimeemailparsed