Python实现发送与接收邮件的方法详解


Posted in Python onMarch 28, 2018

本文实例讲述了Python实现发送与接收邮件的方法。分享给大家供大家参考,具体如下:

一、发送邮件

这里实现给网易邮箱发送邮件功能:

import smtplib
import tkinter
class Window:
  def __init__(self,root):
    label1 = tkinter.Label(root,text='SMTP')
    label2 = tkinter.Label(root,text='Port')
    label3 = tkinter.Label(root,text='用户名')
    label4 = tkinter.Label(root,text='密码')
    label5 = tkinter.Label(root,text='收件人')
    label6 = tkinter.Label(root,text='主题')
    label7 = tkinter.Label(root,text='发件人')
    label1.place(x=5,y=5)
    label2.place(x=5,y=30)
    label3.place(x=5,y=55)
    label4.place(x=5,y=80)
    label5.place(x=5,y=105)
    label6.place(x=5,y=130)
    label7.place(x=5,y=155)
    self.entryPop = tkinter.Entry(root)
    self.entryPort = tkinter.Entry(root)
    self.entryUser = tkinter.Entry(root)
    self.entryPass = tkinter.Entry(root,show = '*')
    self.entryTo = tkinter.Entry(root)
    self.entrySub = tkinter.Entry(root)
    self.entryFrom = tkinter.Entry(root)
    self.entryPort.insert(tkinter.END,'25')
    self.entryPop.place(x=50,y=5)
    self.entryPort.place(x=50,y=30)
    self.entryUser.place(x=50,y=55)
    self.entryPass.place(x=50,y=80)
    self.entryTo.place(x=50,y=105)
    self.entrySub.place(x=50,y=130)
    self.entryFrom.place(x=50,y=155)
    self.get = tkinter.Button(root,text='发送邮件',command = self.Get)
    self.get.place(x=60,y=180)
    self.text=tkinter.Text(root)
    self.text.place(y=220)
  def Get(self):
    try:
      host = self.entryPop.get()
      port =int(self.entryPort.get())
      user = self.entryUser.get()
      pw = self.entryPass.get()
      fromaddr = self.entryFrom.get()
      toaddr=self.entryTo.get()
      subject=self.entrySub.get()
      text = self.text.get(1.0,tkinter.END)
      msg =("From:%s\nTo:%s\nSubject:%s\n\n"
         % (fromaddr,toaddr,subject))
      msg = msg+text
      smtp=smtplib.SMTP(host,port)
      smtp.set_debuglevel(1)
      smtp.login(user,pw)
      smtp.sendmail(fromaddr,toaddr,msg)
      smtp.quit()
    except Exception as e:
      self.text.insert(tkinter.END,'发送错误\n')
root =tkinter.Tk()
window=Window(root)
root.minsize(600,400)
root.mainloop()

运行结果

Python实现发送与接收邮件的方法详解

二、接收邮件

这里实现从网易POP3服务器接收邮件:

import poplib
import re
import tkinter
class Window:
  def __init__(self,root):
    label1 = tkinter.Label(root,text='POP3')
    label2 = tkinter.Label(root,text='Port')
    label3 = tkinter.Label(root,text='用户名')
    label4 = tkinter.Label(root,text='密码')
    label1.place(x=5,y=5)
    label2.place(x=5,y=30)
    label3.place(x=5,y=55)
    label4.place(x=5,y=80)
    self.entryPop = tkinter.Entry(root)
    self.entryPort = tkinter.Entry(root)
    self.entryUser = tkinter.Entry(root)
    self.entryPass = tkinter.Entry(root,show = '*')
    self.entryPort.insert(tkinter.END,'110')
    self.entryPop.place(x=50,y=5)
    self.entryPort.place(x=50,y=30)
    self.entryUser.place(x=50,y=55)
    self.entryPass.place(x=50,y=80)
    self.get = tkinter.Button(root,text='收取邮件',command = self.Get)
    self.get.place(x=60,y=120)
    self.text=tkinter.Text(root)
    self.text.place(y=150)
  def Get(self):
    try:
      host = self.entryPop.get()
      port =int(self.entryPort.get())
      user = self.entryUser.get()
      pw = self.entryPass.get()
      pop=poplib.POP3(host)
      pop.user(user)
      pop.pass_(pw)
      stat=pop.stat()
      self.text.insert(tkinter.END,'Staus:%d message(s),%d bytes\n' % stat)
      rx_headers = re.compile(r"^(From|To|Subject)")
      for n in range(stat[0]):
        response,lines,bytes = pop.top(n+1,10)
        self.text.insert(tkinter.END,"Message %d (%d bytes)\n" % (n+1,bytes))
        self.text.insert(tkinter.END,"-"*30+'\n')
        str_lines=[]
        for l in lines:
          str_lines.append(l.decode(encoding = 'utf-8'))
        self.text.insert(tkinter.END,"\n".join(filter(rx_headers.match,str_lines)))
        self.text.insert(tkinter.END,'\n')
        self.text.insert(tkinter.END,"-"*30+'\n')
    except Exception as e:
        self.text.insert(tkinter.END,'接收错误\n')
root =tkinter.Tk()
window=Window(root)
root.mainloop()

运行结果

Python实现发送与接收邮件的方法详解

更多关于Python相关内容可查看本站专题:《Python Socket编程技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
在Django的模板中使用认证数据的方法
Jul 23 Python
深入浅出分析Python装饰器用法
Jul 28 Python
使用Python的turtle模块画图的方法
Nov 15 Python
基于python代码实现简易滤除数字的方法
Jul 17 Python
Django 表单模型选择框如何使用分组
May 16 Python
Python3视频转字符动画的实例代码
Aug 29 Python
Python 元组操作总结
Sep 18 Python
如何解决flask修改静态资源后缓存文件不能及时更改问题
Aug 02 Python
python如何控制进程或者线程的个数
Oct 16 Python
Python远程linux执行命令实现
Nov 11 Python
pycharm激活码免费分享适用最新pycharm2020.2.3永久激活
Nov 25 Python
OpenCV+Python3.5 简易手势识别的实现
Dec 21 Python
Python实现线程状态监测简单示例
Mar 28 #Python
python实现朴素贝叶斯分类器
Mar 28 #Python
详解Python中where()函数的用法
Mar 27 #Python
Django基于ORM操作数据库的方法详解
Mar 27 #Python
利用Python批量提取Win10锁屏壁纸实战教程
Mar 27 #Python
Django学习笔记之ORM基础教程
Mar 27 #Python
Python使用xlwt模块操作Excel的方法详解
Mar 27 #Python
You might like
php基础知识:函数基础知识
2006/12/13 PHP
PHP+MYSQL开发工具及资源收藏
2007/01/02 PHP
去掉destoon资讯内容页keywords关键字自带的文章标题的方法
2014/08/21 PHP
php实现根据url自动生成缩略图的方法
2014/09/23 PHP
firefox插件Firebug的使用教程
2010/01/02 Javascript
js中opener与parent的区别详细解析
2014/01/14 Javascript
Javascript字符串浏览器兼容问题分析
2014/12/01 Javascript
基于豆瓣API+Angular开发的web App
2015/01/02 Javascript
js实现点击切换checkbox背景图片的简单实例
2017/05/08 Javascript
vue.js框架实现表单排序和分页效果
2017/08/09 Javascript
Node.js+jade抓取博客所有文章生成静态html文件的实例
2017/09/19 Javascript
vue-cli脚手架引入弹出层layer插件的几种方法
2019/06/24 Javascript
[01:21:07]EG vs Liquid 2018国际邀请赛淘汰赛BO3 第一场 8.25
2018/08/29 DOTA
python利用MethodType绑定方法到类示例代码
2017/08/27 Python
Python 利用切片从列表中取出一部分使用的方法
2019/02/01 Python
Python模块future用法原理详解
2020/01/20 Python
python IP地址转整数
2020/11/20 Python
python读写数据读写csv文件(pandas用法)
2020/12/14 Python
详解appium自动化测试工具(monitor、uiautomatorviewer)
2021/01/27 Python
使用placeholder属性设置input文本框的提示信息
2020/02/19 HTML / CSS
美国老牌主机服务商:iPage
2016/07/22 全球购物
加拿大时尚床上用品零售商:QE Home | Quilts Etc
2018/01/22 全球购物
Topshop法国官网:英国快速时尚品牌
2018/04/08 全球购物
Carrs Silver官网:英国著名的银器品牌
2020/08/29 全球购物
汇智创新科技发展有限公司
2015/12/06 面试题
平面网站制作专科生的自我评价分享
2013/12/11 职场文书
函授本科个人自我鉴定
2014/03/25 职场文书
党的群众路线教育实践活动心得体会(医院)
2014/11/03 职场文书
2015年元旦主持词结束语
2014/12/14 职场文书
求职意向书范本
2015/05/11 职场文书
《童年》读后感(三篇)
2019/08/27 职场文书
php 解析非标准json、非规范json
2021/04/01 PHP
如何将numpy二维数组中的np.nan值替换为指定的值
2021/05/14 Python
浅谈python中的多态
2021/06/15 Python
SpringCloud Alibaba项目实战之nacos-server服务搭建过程
2021/06/21 Java/Android
Nginx 配置 HTTPS的详细过程
2022/05/30 Servers