写的是Python中smtp的gui编程,用的163邮箱给qq邮箱发送邮件做测试,如果你发现你的发送失败,试着用以下方法解决:
1.网页登陆你的邮箱,设置中查看smtp是否开启,比如163邮箱的smtp是否开启
2.你输入的密码是不是163设置的远程客户端的授权密码,请用远程登录密码登陆测试
希望以上2点能帮助你测试成功·
效果图:
#*-* coding:utf-8 *-*
import Tkinter as tk
from email import encoders
from email.header import Header
from email.mime.text import MIMEText
from email.utils import parseaddr, formataddr
import smtplib
root = tk.Tk()
root.geometry('500x400')
root.title('SMTP发送')
#输入发送者邮箱
fram = tk.Frame(root)
frameL = tk.Frame(fram)
tk.Label(frameL,text='请输入你的邮箱',).pack()
frameL.pack(side = 'left')
frameR = tk.Frame(fram)
sender = tk.StringVar()
msgsend = tk.Entry(frameR,textvariable = sender,).pack()
sender.set('example@163.com')
frameR.pack(side = 'right')
fram.pack()
#输入邮箱密码
fram = tk.Frame(root)
frameL = tk.Frame(fram)
tk.Label(frameL,text='请输入邮箱密码',).pack()
frameL.pack(side = 'left')
frameR = tk.Frame(fram)
passwo = tk.StringVar()
msgpass = tk.Entry(frameR,textvariable = passwo,).pack()
passwo.set('*****')
frameR.pack(side = 'right')
fram.pack()
#输入接收者者邮箱
fram = tk.Frame(root)
frameL = tk.Frame(fram)
tk.Label(frameL,text='请输入对方的邮箱',).pack(side = 'left')
frameL.pack(side = 'left')
frameR = tk.Frame(fram)
receive = tk.StringVar()
msgreveive = tk.Entry(frameR,textvariable = receive,).pack()
receive.set('example@qq.com')
frameR.pack(side = 'left')
fram.pack()
#输入邮件内容
tk.Label(root,text='请输入邮件内容',).pack(side = 'left')
msgrecv = tk.Text(root)
msgrecv.pack()
#格式化数据
def _format_addr(s):
name, addr = parseaddr(s)
return formataddr(( \
Header(name, 'utf-8').encode(), \
addr.encode('utf-8') if isinstance(addr, unicode) else addr))
def sendEmail():
#取出发送者邮箱,登录密码,接收者邮箱
global sender
send = sender.get()
global passwo
passwd = passwo.get()
global receive
receive = receive.get().encode()
#赋值发邮件所需要的信息
from_addr = send
password = passwd
to_addr = receive
smtp_server = 'smtp.163.com' #163邮箱服务器
msg2 = msgrecv.get('0.0', 'end')
msg = MIMEText(msg2, 'plain', 'utf-8') #信息内容
msg['From'] = _format_addr(u'Python爱好者 <%s>' % from_addr) #发件人
msg['To'] = _format_addr(u'%s' % to_addr) #收件人
msg['Subject'] = Header(u'来自SMTP的问候……', 'utf-8').encode() #邮件标题
server = smtplib.SMTP(smtp_server, 25)
server.set_debuglevel(1)
server.login(from_addr, password)
server.sendmail(from_addr, [to_addr], msg.as_string())
server.quit()
msgrecv.insert('end', u'\n发送成功!')
#删除输入的邮件信息
def reset():
msgrecv.delete('1.0','end')
framButton = tk.Frame(root)
send=tk.Button(framButton, text='发送', command=sendEmail, bg = 'green').pack(side = 'left')
reset=tk.Button(framButton, text='取消', command=reset).pack(side = 'left')
quit=tk.Button(framButton, text='退出', command=root.quit, bg = 'red').pack(side = 'right')
framButton.pack()