【Python】Python网络编程之发送E-mail
发送邮件一般使用SMTP协议,Python内置对SMTP的支持,可以发送纯文本邮件、HTML邮件以及带附件的邮件。
Python对SMTP支持有smtplib和email两个模块,email负责构造邮件,smtplib负责发送邮件。
一、email模块简介
纯文本文件
from email.mime.text import MIMEText
msg = MIMEText('hello', 'plain', 'utf-8')
html文件
from email.mime.text import MIMEText
msg = MIMEText('<html><body><h1>Hello</h1></body></html>', 'html', 'utf-8')
二、smtplib模块简介
1、使用smtplib模块的SMTP类创建SMTP对象实例,原型如下
SMTP(host, port, local_hostname)
host: 连接服务器名,可选参数
port: 服务器端口,可选参数
local_hostname: 本地主机名,可选参数
例如:smtp = smtplib.SMTP('smtp.163.com', 25)
2、登陆到SMTP服务器,使用SMTP对象的login方法,原型如下
login(user, password)
user 登陆服务器的用户名
password 登陆服务器的授权码(注意:不是邮箱密码)
3、若要打印出和SMTP服务器交互的详细信息,使用SMTP对象的set_debuglevel方法,原型如下
set_debuglevel( level )
level 调试级别
4、若向SMTP服务器发送命令,使用SMTP对象的docmd方法,原型如下
docmd( cmd, argstring)
cmd 发送的命令
argstring 命令参数,可选参数
5、发送邮件,使用SMTP对象的sendmail方法,原型如下
sendmail(from_addr, to_address, msg, mial_options, rcpt_options)
from_addr 发送方邮件地址
to_address 接收方邮件地址
msg 邮件内容
mial_options 邮件ESMTP操作,可选参数
rcpt_options RCPT操作, 可选参数
三、使用Python发送邮件
对于网易163邮箱,SMTP服务器地址为:smtp.163.com,端口值:25
对于QQ邮箱,SMTP服务器地址为:smtp.qq.com,端口值:465或587
本文中介绍使用网易163邮箱发送邮件,首先需要注册邮箱,开启SMTP 服务,开启客户端授权码
撰写代码:使用email模块
import smtplib
from email.mime.text import MIMEText
class Window:
def __init__(self):
self.msg_from = '[email protected]'
self.passward = 'xxx' # 授权码
self.msg_to = '[email protected]' # 接收方
self.subject = 'xxx' # 主题
self.content = '<html><body><h1>Hello !</h1></body></html>' # 内容
self.msg = MIMEText(self.content, 'html', 'utf-8') # html格式
# self.msg = MIMEText(self.content, 'plain', 'utf-8') # 文本格式
self.msg['Subject'] = self.subject
self.msg['From'] = self.msg_from
self.msg['To'] = self.msg_to
def send(self):
try:
self.s = smtplib.SMTP('smtp.163.com', 25)
self.s.ehlo()
self.s.starttls()
self.s.login(self.msg_from, self.passward)
self.s.sendmail(self.msg_from, self.msg_to, self.msg.as_string())
print('发送成功')
self.s.quit()
return True
except smtplib.SMTPException as e:
print('发送失败' + format(e))
return False
window = Window()
print("1")
window.send()
print("2")
撰写代码:不使用email模块
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.enrtyPOP = tkinter.Entry(root)
self.enrtyPORT = tkinter.Entry(root)
self.enrtyUSER = tkinter.Entry(root)
self.enrtyPASS = tkinter.Entry(root, show='*')
self.enrtyTO = tkinter.Entry(root)
self.enrtySUB = tkinter.Entry(root)
self.enrtyFROM = tkinter.Entry(root)
self.enrtyPOP.place(x=50, y=5)
self.enrtyPORT.place(x=50, y=30)
self.enrtyUSER.place(x=50, y=55)
self.enrtyPASS.place(x=50, y=80)
self.enrtyTO.place(x=50, y=105)
self.enrtySUB.place(x=50, y=130)
self.enrtyFROM.place(x=50, y=155)
self.enrtyPOP.insert(tkinter.END, 'smtp.163.com') # SMTP服务器
self.enrtyPORT.insert(tkinter.END, '25') # 默认端口
self.send = tkinter.Button(root, text='发送邮件', command=self.Send)
self.send.place(x=60, y=180)
self.text = tkinter.Text(root, fg='red', bg='white')
self.text.place(y=220)
def Send(self): # 发送按钮事件
try:
host = self.enrtyPOP.get() # 获取服务器地址
port = int(self.enrtyPORT.get()) # 获取端口
user = self.enrtyUSER.get()
pw = self.enrtyPASS.get()
fromaddr = self.enrtyFROM.get()
toaddr = self.enrtyTO.get()
subject = self.enrtySUB.get()
text = self.text.get(1.0, tkinter.END)
msg = ("From: %s\nTO: %s\nSubject: %s\n\n" % (fromaddr, toaddr, subject))
msg = msg + text
global smtp
smtp = smtplib.SMTP(host, port)
smtp.set_debuglevel(1)
smtp.login(user, pw)
smtp.sendmail(fromaddr, toaddr, msg)
smtp.quit()
self.text.insert(tkinter.END, '发送成功\n')
except Exception as e:
self.text.insert(tkinter.END, '发送错误\n')
print(e)
root = tkinter.Tk()
root.title("邮件发送")
window = Window(root)
root.minsize(600, 400)
root.mainloop()
GUI界面如下:
四、自己遇到的问题
Connection unexpectedly closed问题的解决办法: 由于网络不通畅的缘故,使用自己的手机热点即可
554错误问题的解决办法:SMTP服务没有开启,如果开启了还报错误,就关闭了SMTP服务,再打开服务
更多教程可以参考:
菜鸟教程:
http://www.runoob.com/python3/python3-smtp.html
W3CSchool: