python实现的发邮件功能示例

yipeiwu_com6年前Python基础

本文实例讲述了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相关内容可查看本站专题:《Python Socket编程技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总

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

相关文章

编写Python脚本把sqlAlchemy对象转换成dict的教程

在用sqlAlchemy写web应用的时候,经常会用json进行通信,跟json最接近的对象就是dict,有时候操作dict也会比操作ORM对象更为方便,毕竟不用管数据库session的...

浅谈python数据类型及类型转换

Python中核心的数据类型有哪些? 变量(数字、字符串、元组、列表、字典) 什么是数据的不可变性?哪些数据类型具有不可变性 数据的不可变是指数据不可更改,比如: a = ("ab...

Windows和Linux下使用Python访问SqlServer的方法介绍

经常用Python写demo来验证方案的可行性,最近遇到了Python访问SqlServer的问题,这里总结下。 一、Windows下配置Python访问Sqlserver 环境:Win...

Anaconda2下实现Python2.7和Python3.5的共存方法

Anaconda2下实现Python2.7和Python3.5的共存方法

Anaconda 本质上是一个软件发行版,包含了 conda、Python 等 180 多个科学包及其依赖项。 因为包含了大量的科学包,Anaconda 的下载文件比较大(约 500 M...

两个使用Python脚本操作文件的小示例分享

1这是一个创建一个文件,并在控制台写入行到新建的文件中. #!/usr/bin/env python 'makeTextFile.py -- create text file'...