使用 Python 3 通过 SMTP 发送邮件时出现 TimeoutError
I got TimeoutError when sending mails with SMTP using Python 3
我正在使用 Python3.7,当我 运行 我的代码时出现以下错误:
TimeoutError: [WinError 10060]连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立连接失败,因为连接的主机没有响应
这是我的代码:
import smtplib
from email import encoders
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
server = smtplib.SMTP('smtp.gmail.com', 25)
server.connect("smtp.gmail.com",465)
server.ehlo()
with open('password.txt', 'r') as f:
password = f.read()
server.login('mymail@gmail.com', password)
msg = MIMEMultipart()
msg['From'] = 'mymail@gmail.com'
msg['To'] = 'mymail2@yahoo.com'
msg['Subject'] = 'Mail testing with Python'
with open('message.txt', 'r') as f:
message = f.read()
msg.attach(MIMEText(message, 'plain'))
text = msg.as_string()
server.sendmail('mymail@gmail.com', 'mymail2@yahoo.com', text)
好像是什么问题?我的代码有什么错误还是只是我的网络连接?
首先,要让 gmail 与 python 脚本一起正常工作,您需要配置 gmail 以允许低安全性应用程序。我假设您已经完成了那部分,否则您可以查看 gmail 帮助。
Gmail help on turn on less secure app
这是发送您的电子邮件的脚本:
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.ehlo()
server.login('mymail@gmail.com', "your password")
msg = MIMEMultipart('alternative',)
msg['From'] = 'mymail@gmail.com'
msg['To'] = 'mymail2@yahoo.com'
msg['Subject'] = 'Mail testing with Python'
with open('message.txt', 'r') as f:
message = f.read()
msg.attach(MIMEText(message, 'plain'))
text = msg.as_string().encode('utf8')
server.sendmail('mymail@gmail.com', 'mymail2@yahoo.com', text)
我正在使用 Python3.7,当我 运行 我的代码时出现以下错误:
TimeoutError: [WinError 10060]连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立连接失败,因为连接的主机没有响应
这是我的代码:
import smtplib
from email import encoders
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
server = smtplib.SMTP('smtp.gmail.com', 25)
server.connect("smtp.gmail.com",465)
server.ehlo()
with open('password.txt', 'r') as f:
password = f.read()
server.login('mymail@gmail.com', password)
msg = MIMEMultipart()
msg['From'] = 'mymail@gmail.com'
msg['To'] = 'mymail2@yahoo.com'
msg['Subject'] = 'Mail testing with Python'
with open('message.txt', 'r') as f:
message = f.read()
msg.attach(MIMEText(message, 'plain'))
text = msg.as_string()
server.sendmail('mymail@gmail.com', 'mymail2@yahoo.com', text)
好像是什么问题?我的代码有什么错误还是只是我的网络连接?
首先,要让 gmail 与 python 脚本一起正常工作,您需要配置 gmail 以允许低安全性应用程序。我假设您已经完成了那部分,否则您可以查看 gmail 帮助。
Gmail help on turn on less secure app
这是发送您的电子邮件的脚本:
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.ehlo()
server.login('mymail@gmail.com', "your password")
msg = MIMEMultipart('alternative',)
msg['From'] = 'mymail@gmail.com'
msg['To'] = 'mymail2@yahoo.com'
msg['Subject'] = 'Mail testing with Python'
with open('message.txt', 'r') as f:
message = f.read()
msg.attach(MIMEText(message, 'plain'))
text = msg.as_string().encode('utf8')
server.sendmail('mymail@gmail.com', 'mymail2@yahoo.com', text)