解决Python通过STMP发送邮件出现socket.error: [Errno 10061]
2016-10-20 22:18
591 查看
在学习Python通过STMP发送邮件过程中出现了socket.error: [Errno 10061]的问题。以下是出问题的代码。
经过查询之后发现需要在本机上安装sendmail,上述代码才可以正常执行。我没有装sendmail,所以想要了解的可自己查阅使用sendmail发送邮件。
如果本机没有安装sendmail的话,则需要借助其他邮件服务商提供的SMTP访问(这里我使用的是163邮箱)。
这样就成功的完成了邮件的发送。注意:使用的sender和receivers 都需要在对应的邮箱设置中开启SMTP服务,否则发送不会成功。
References:
菜鸟教程-Python教程
# -*- coding:utf-8 -*- import smtplib from email.mime.text import MIMEText from email.header import Header sender = '***@**.com' receivers = ['***@**.com'] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱 # 三个参数:第一个为文本内容,第二个 plain 设置文本格式,第三个 utf-8 设置编码 message = MIMEText('Python SMTP 邮件发送测试', 'plain', 'utf-8') message['From'] = Header("From测试", 'utf-8') message['To'] = Header("To测试", 'utf-8') subject = 'Python SMTP 邮件测试' message['Subject'] = Header(subject, 'utf-8') try: smtpObj = smtplib.SMTP('localhost') smtpObj.sendmail(sender,receivers,message.as_string()) print "邮件发送成功" except smtplib.SMTPException: print "Error: 无法发送邮件"
经过查询之后发现需要在本机上安装sendmail,上述代码才可以正常执行。我没有装sendmail,所以想要了解的可自己查阅使用sendmail发送邮件。
如果本机没有安装sendmail的话,则需要借助其他邮件服务商提供的SMTP访问(这里我使用的是163邮箱)。
# -*- coding:utf-8 -*- import smtplib from email.mime.text import MIMEText from email.header import Header # 第三方 SMTP 服务 mail_host="smtp.163.com" #设置服务器 mail_user="***@163.com" #用户名 mail_pass="***" #口令 sender = '***@163.com' receivers = '[***@163.com]' # 接收邮件,可设置为你的QQ邮箱或者其他邮箱 message = MIMEText('Python SMTP 邮件发送测试', 'plain', 'utf-8') message['From'] = Header("From测试",'utf-8') message['To'] = Header("To测试", 'utf-8') subject = 'Python SMTP 邮件测试' message['Subject'] = Header(subject, 'utf-8') try: smtpObj = smtplib.SMTP() smtpObj.connect(mail_host, 25) # 25 为 SMTP 端口号 smtpObj.login(mail_user,mail_pass) smtpObj.sendmail(sender, receivers, message.as_string()) print "邮件发送成功" except smtplib.SMTPException: print "Error: 无法发送邮件"
这样就成功的完成了邮件的发送。注意:使用的sender和receivers 都需要在对应的邮箱设置中开启SMTP服务,否则发送不会成功。
References:
菜鸟教程-Python教程
相关文章推荐
- python django 框架中socket.error: [Errno 10061]解决方法
- python 发送邮件535, 'Error: authentication failed' 解决
- Windows下flask发送邮件完美解决in create_connection raise err error: [Errno 10061]等错误问题
- Python socket.error: [Errno 98] Address already in use的原因和解决方法
- urlopen error [Errno 10061]解决方法 分类: python 2013-06-09 11:22 3358人阅读 评论(0) 收藏
- windows python flask上传文件出现IOError: [Errno 13] Permission denied: 'E:\\git\\test\\static\\uploads'的解决方法
- Python Code: 利用QQ邮箱发送邮件,解决SMTPAuthenticationError:530错误
- 【python】 URLError: <urlopen error [Errno 10061]报错解决方法
- python学习--for Mac 问题解决:socket.gaierror: [Errno -2] Name or service not known
- python3.5 URLError: <urlopen error [Errno 10061]报错解决方法
- 用python发送QQ邮件遇到的错误记录(ssl.SSLError: [Errno 1] _ssl.c:504)
- Python socket.error: [Errno 98] Address already in use的原因和解决方法
- socket.error: [Errno 10061]解决方法
- python使用urllib.urlretrieve时出现“IOError:[Errno 2] No such file or directory”解决办法
- 解决socket.error: [Errno 98] Address already in use问题
- Godaddy服务器发送邮件出现Could not connect to SMTP host错误 解决办法
- 将App通过XCode上传到AppStore出现这个错误“An error occurred uploading to the iTunes Store”的解决方法
- dedecms出现Error: Errno:0 SQL::错误解决方法
- python32版本,出现问题“struct.error: argument for 's' must be a bytes object”的解决办法
- python - 如何通过GMail发送邮件:smtplib的使用