我已在寡妇7中安装了Python 2.7。 我正在尝试一个基本的smtp python程序,以通过python脚本自动发送邮件。 但是,当我通过cmd提示符运行脚本时,它会无限期地卡住。
# Python code to illustrate Sending mail
# to multiple users
# from your Gmail account
import smtplib
# list of email_id to send the mail
li = ["ishank4@gmail.com"]
for i in range(len(li)):
s = smtplib.SMTP('smtp.gmail.com', 587)
s.starttls()
s.login("mymailid@gmail.com", "mypassword")
message = "Message_you_need_`enter code here`to_send"
s.sendmail("sender_email_id", li[i], message)
s.quit()*
答案 0 :(得分:-1)
尝试一下:
def connect():
while True:
try:
server = smtplib.SMTP("smtp.gmail.com")
except smtplib.SMTPException as error:
print(error)
continue
try:
server.ehlo()
if server.has_extn("starttls"):
server.starttls()
server.ehlo()
except (smtplib.SMTPException,ssl.SSLError) as error:
print(error)
disconnect(server)
continue
break
return server
def disconnect(server):
try:
server.quit()
except smtplib.SMTPException as error:
print(error)