使用python2.7发送邮件,代码如下:

from  email.header import Header
from email.mime.text import MIMEText
import smtplib
#发送邮箱
sender='yq_test@126.com'
password='123456'
#接受邮箱
receiver='41618174@qq.com'

smtpserver='smtp.126.com'

subject='python email test'
msg=MIMEText('下雨了,大家关好窗户','plain','utf-8')
msg['Subject']=Header(subject,'utf-8')
try:
smtp=smtplib.SMTP()
smtp.connect(smtpserver)
smtp.login(sender,password)
smtp.sendmail(sender,receiver,msg.as_string())
smtp.quit()
print "邮件发送成功"
except smtplib.SMTPException:
print "Error:无法发邮件"
运行时提示:
Error:无法发邮件
<class 'smtplib.SMTPException'>
解决办法:
在try的上面加上以下两行代码,问题解决:
msg['From']=sender
msg['to']=receiver

 各位看官,看完后不要忘了为我乐于分享的精神点个赞哦



最新文章

  1. matlab中数组创建方法
  2. JavaScript深入浅出2-表达式和运算符
  3. flex lineChart 显示所有的数据节点
  4. No.3__C#
  5. C# 字符串替换Replace
  6. 轻量级模块化开发框架 Hasor 核心模块 v0.0.2 发布
  7. angular2怎么使用第三方的库(jquery等)
  8. hadoop-1.2.1伪分布模式配置
  9. 自定义tableViewCell
  10. PHP_string
  11. Linux驱动设计——内存与IO访问
  12. HDU 3555 Bomb 数位dp
  13. Go 解析JSON
  14. 开始AFNetworking
  15. awk学习总结(两) How awk works and awk CMD in a file
  16. 1637: [Usaco2007 Mar]Balanced Lineup
  17. net core appsetting配置
  18. java学习教程与笔记
  19. Vue对Html标签应用条件渲染
  20. Shell脚本编程(二):shell变量

热门文章

  1. selenium 控制窗口无限向下滚动
  2. BJD4th pwn pi
  3. 任务日历关联(Project)
  4. response.setHeader(&quot;xxx&quot;,&quot;大侠&quot;)如果赋值中文,那么将不会在页面出值,
  5. JAVA获取昨天、今天、明天等日期
  6. Windows库函数获取 可执行程序所在路径
  7. 【LeetCode】940. Distinct Subsequences II 解题报告(Python)
  8. 【LeetCode】641. Design Circular Deque 解题报告(Python & C++)
  9. 【LeetCode】228. Summary Ranges 解题报告(Python)
  10. VUE3 之 循环渲染