python邮件SMTP的GUI编程
写的是python中smtp的gui编程,用的163邮箱给qq邮箱发送邮件做测试,如果你发现你的发送失败,试着用以下方法解决:
1.网页登陆你的邮箱,设置中查看smtp是否开启,比如163邮箱的smtp是否开启
2.你输入的密码是不是163设置的远程客户端的授权密码,请用远程登录密码登陆测试
希望以上2点能帮助你测试成功·
效果图:
1 #*-* coding:utf-8 *-*
2 import Tkinter as tk
3 from email import encoders
4 from email.header import Header
5 from email.mime.text import MIMEText
6 from email.utils import parseaddr, formataddr
7 import smtplib
8
9
10
11 root = tk.Tk()
12 root.geometry('500x400')
13 root.title('SMTP发送')
14
15 #输入发送者邮箱
16 fram = tk.Frame(root)
17 frameL = tk.Frame(fram)
18 tk.Label(frameL,text='请输入你的邮箱',).pack()
19 frameL.pack(side = 'left')
20
21 frameR = tk.Frame(fram)
22 sender = tk.StringVar()
23 msgsend = tk.Entry(frameR,textvariable = sender,).pack()
24 sender.set('example@163.com')
25 frameR.pack(side = 'right')
26 fram.pack()
27
28
29 #输入邮箱密码
30 fram = tk.Frame(root)
31 frameL = tk.Frame(fram)
32 tk.Label(frameL,text='请输入邮箱密码',).pack()
33 frameL.pack(side = 'left')
34
35 frameR = tk.Frame(fram)
36 passwo = tk.StringVar()
37 msgpass = tk.Entry(frameR,textvariable = passwo,).pack()
38 passwo.set('*****')
39 frameR.pack(side = 'right')
40 fram.pack()
41
42
43 #输入接收者者邮箱
44 fram = tk.Frame(root)
45 frameL = tk.Frame(fram)
46 tk.Label(frameL,text='请输入对方的邮箱',).pack(side = 'left')
47 frameL.pack(side = 'left')
48
49 frameR = tk.Frame(fram)
50 receive = tk.StringVar()
51 msgreveive = tk.Entry(frameR,textvariable = receive,).pack()
52 receive.set('example@qq.com')
53 frameR.pack(side = 'left')
54 fram.pack()
55
56
57 #输入邮件内容
58 tk.Label(root,text='请输入邮件内容',).pack(side = 'left')
59 msgrecv = tk.Text(root)
60 msgrecv.pack()
61
62
63 #格式化数据
64 def _format_addr(s):
65 name, addr = parseaddr(s)
66 return formataddr(( \
67 Header(name, 'utf-8').encode(), \
68 addr.encode('utf-8') if isinstance(addr, unicode) else addr))
69
70
71 def sendEmail():
72 #取出发送者邮箱,登录密码,接收者邮箱
73 global sender
74 send = sender.get()
75 global passwo
76 passwd = passwo.get()
77 global receive
78 receive = receive.get().encode()
79
80 #赋值发邮件所需要的信息
81 from_addr = send
82 password = passwd
83 to_addr = receive
84 smtp_server = 'smtp.163.com' #163邮箱服务器
85 msg2 = msgrecv.get('0.0', 'end')
86
87
88 msg = MIMEText(msg2, 'plain', 'utf-8') #信息内容
89 msg['From'] = _format_addr(u'Python爱好者 <%s>' % from_addr) #发件人
90 msg['To'] = _format_addr(u'%s' % to_addr) #收件人
91 msg['Subject'] = Header(u'来自SMTP的问候……', 'utf-8').encode() #邮件标题
92 server = smtplib.SMTP(smtp_server, 25)
93 server.set_debuglevel(1)
94 server.login(from_addr, password)
95 server.sendmail(from_addr, [to_addr], msg.as_string())
96 server.quit()
97 msgrecv.insert('end', u'\n发送成功!')
98
99 #删除输入的邮件信息
100 def reset():
101 msgrecv.delete('1.0','end')
102
103
104 framButton = tk.Frame(root)
105 send=tk.Button(framButton, text='发送', command=sendEmail, bg = 'green').pack(side = 'left')
106 reset=tk.Button(framButton, text='取消', command=reset).pack(side = 'left')
107 quit=tk.Button(framButton, text='退出', command=root.quit, bg = 'red').pack(side = 'right')
108 framButton.pack()
109
110 root.mainloop()
欢迎来邮件交流:lq65535@163.com