Python:发送电子邮件时,总是在子句中被阻止:smtpserver = smtplib.SMTP("smtp.gmail.com",587)

Python: when sending email, always blocked in the clause: smtpserver = smtplib.SMTP(quot;smtp.gmail.comquot;,587)(Python:发送电子邮件时,总是在子句中被阻止:smtpserver = smtplib.SMTP(smtp.gmail.com,587))
本文介绍了Python:发送电子邮件时,总是在子句中被阻止:smtpserver = smtplib.SMTP("smtp.gmail.com",587)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 Python 程序来发送电子邮件.但是每次执行子句时:

I am writing a Python program to send an email. But every time when executing the clause:

smtpserver = smtplib.SMTP("smtp.gmail.com",587)

它会在这里阻塞,一直保持执行状态,没有任何提示和错误.我不知道为什么.谁能帮助我?

it will block here and always stay the executing status without any prompts and errors. I don't know why. And can anyone help me?

代码如下:导入smtplib

The code is as following: import smtplib

to = 'toemail@gmail.com'
gmail_user = 'user@gmail.com'
gmail_pwd = 'password'
smtpserver = smtplib.SMTP("smtp.gmail.com",587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(gmail_user, gmail_pwd)
header = 'To:' + to + '
' + 'From: ' + gmail_user + '
' + 'Subject:testing 
'
print header
msg = header + '
 this is test msg from mkyong.com 

'
smtpserver.sendmail(gmail_user, to, msg)
print 'done!'
smtpserver.close()

推荐答案

也许我迟到了 4 年,但这对我有用,可能对其他人有帮助!

Maybe I am 4 years late, but this is what worked for me and might help someone else!

server = smtplib.SMTP("smtp.gmail.com", 587, None, 30)

这篇关于Python:发送电子邮件时,总是在子句中被阻止:smtpserver = smtplib.SMTP("smtp.gmail.com",587)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Leetcode 234: Palindrome LinkedList(Leetcode 234:回文链接列表)
How do I read an Excel file directly from Dropbox#39;s API using pandas.read_excel()?(如何使用PANDAS.READ_EXCEL()直接从Dropbox的API读取Excel文件?)
subprocess.Popen tries to write to nonexistent pipe(子进程。打开尝试写入不存在的管道)
I want to realize Popen-code from Windows to Linux:(我想实现从Windows到Linux的POpen-code:)
Reading stdout from a subprocess in real time(实时读取子进程中的标准输出)
How to call type safely on a random file in Python?(如何在Python中安全地调用随机文件上的类型?)