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

 更新时间:2018年03月28日 11:19:52   作者:chengqiuming  
这篇文章主要介绍了Python实现发送与接收邮件的方法,结合实例形式分析了Python基于smtplib库使用SMTP协议进行邮件发送及基于poplib库使用POP3服务器接收邮件的相关操作技巧,需要的朋友可以参考下

本文实例讲述了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()

运行结果

二、接收邮件

这里实现从网易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 Socket编程技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总

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

相关文章

  • python3+PyQt5实现自定义流体混合窗口部件

    python3+PyQt5实现自定义流体混合窗口部件

    这篇文章主要为大家详细介绍了python3+PyQt5实现自定义流体混合窗口部件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-04-04
  • Python高阶函数与装饰器函数的深入讲解

    Python高阶函数与装饰器函数的深入讲解

    这篇文章主要给大家介绍了关于Python高阶函数与装饰器函数的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11
  • Python最长回文子串问题

    Python最长回文子串问题

    这篇文章主要介绍了Python最长回文子串问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-11-11
  • Python中os模块的使用及文件对象的读写详解

    Python中os模块的使用及文件对象的读写详解

    这篇文章主要介绍了Python中os模块的使用及文件对象的读写详解,Python open() 方法用于打开一个文件,并创建返回文件对象,在对文件进行处理过程都需要使用到这个函数,如果该文件无法被打开,会抛出 OSError,需要的朋友可以参考下
    2023-08-08
  • 基于python pygame实现的兔子吃月饼小游戏

    基于python pygame实现的兔子吃月饼小游戏

    pygame是用来开发游戏的一套基于SDL的模板,它可以是python创建完全界面化的游戏和多媒体程序,而且它基本上可以在任何系统上运行,这篇文章主要给大家介绍了基于python pygame实现的兔子吃月饼小游戏的相关资料,需要的朋友可以参考下
    2021-09-09
  • Python实现将视频按照时间维度剪切

    Python实现将视频按照时间维度剪切

    这篇文章主要为大家详细介绍了如何利用Python实现将视频按照时间维度进行剪切,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起了解一下
    2022-12-12
  • Python环境配置实现pip加速过程解析

    Python环境配置实现pip加速过程解析

    这篇文章主要介绍了Python环境配置实现pip加速过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-11-11
  • python判断变量是否为列表的方法

    python判断变量是否为列表的方法

    在本篇文章里小编给大家整理了关于python判断变量是否为列表的方法,有需要的朋友们可以学习下。
    2020-09-09
  • django开发之settings.py中变量的全局引用详解

    django开发之settings.py中变量的全局引用详解

    当网站里面的一些内容,如邮箱,网站标题,网站的描述,这些东西我们可以存在数据库中也可以存放在我们的setting 文件中,这篇文章主要给大家介绍了django中settings.py变量的全局引用的相关资料,文中介绍的非常详细,需要的朋友可以参考下。
    2017-03-03
  • Jupyter Notebook运行代码无反应问题及解决方法

    Jupyter Notebook运行代码无反应问题及解决方法

    这篇文章主要介绍了Jupyter Notebook运行代码无反应问题及解决方法,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-01-01

最新评论