【脚本语言系列】关于Python邮件服务smtplib,你需要知道的事
2017-05-10 16:29
901 查看
如何使用smtplib
注意:使用第三方邮件客户端,需要使用短信开通并获取授权码。http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256
发送QQ邮件
# -*- coding:utf-8 -*- # 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.entrySMTP = 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.entrySMTP.insert(Tkinter.END, "smtp.qq.com") self.entryPort.insert(Tkinter.END, '465') self.entryUser.insert(Tkinter.END, "******@qq.com")# ******@qq.com为测试邮箱,下同 self.entryPass = Tkinter.Entry(root, show='*') self.entryPass.insert(Tkinter.END, "***********") # 填入“授权码” self.entryTo.insert(Tkinter.END, "******@qq.com") self.entrySub.insert(Tkinter.END, "Hello, World") self.entryFrom.insert(Tkinter.END, "******@qq.com") self.entrySMTP.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=200) def Get(self): try: host = self.entrySMTP.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 = smtplib.SMTP_SSL(host, port) smtp.set_debuglevel(1) smtp.login(user, pw) smtp.sendmail(fromaddr, toaddr, msg) smtp.quit() except: self.text.insert(Tkinter.END, '发送错误\n') root = Tkinter.Tk() window = Window(root) root.mainloop()
用户界面
服务器反馈
成功发送邮件
什么是smtplib
Python中smtp模块提供了对SMTP协议的支持。使用SMTP可以发送邮件。
相关文章推荐
- 【脚本语言系列】关于Python邮件服务poplib,你需要知道的事
- 【脚本语言系列】关于Python网页服务Microsoft IIS,你需要知道的事
- 【脚本语言系列】关于Python网页服务Plone,你需要知道的事
- 【脚本语言系列】关于PythonWeb服务自动化Web API,你需要知道的事
- 【脚本语言系列】关于Python网页服务Zope, 你需要知道的事
- 【脚本语言系列】关于PythonWeb服务自动化webbrowser模块,你需要知道的事
- 【脚本语言系列】关于PythonWeb服务自动化BeautifulSoup,你需要知道的事
- 【脚本语言系列】关于Python网页服务Apache,你需要知道的事
- 【脚本语言系列】关于PythonWeb服务自动化REST,你需要知道的事
- 【脚本语言系列】关于Python数据库处理SQLite,你需要知道的事
- 【脚本语言系列】关于Python音频处理DirectSound, 你需要知道的事
- 【脚本语言系列】关于Python音频处理WMPlayerOCX,你需要知道的事情
- 【脚本语言系列】关于PythonGUI编程wxPython, 你需要知道的事
- 【脚本语言系列】关于PythonGUI界面PyQT,你需要知道的事
- 【脚本语言系列】关于Python数值计算Pandas,你需要知道的事
- 【脚本语言系列】关于Python机器学习scikits-learn,你需要知道的事
- 【脚本语言系列】关于Python数据库处理MySQL, 你需要知道的事
- 【脚本语言系列】关于Python游戏工具Pygame,你需要知道的事
- 【脚本语言系列】关于Python测试工具Pyflakes, 你需要知道的事
- 【脚本语言系列】关于Python性能分析工具timeit, 你需要知道的事