![63e18881c4cb1959de041f78020c4b78.png](https://i-blog.csdnimg.cn/blog_migrate/76872e10964c84318cc901ea9678557a.jpeg)
Python发邮件详解
一、利用163的smtp发送邮件
import smtplibfrom email.mime.text import MIMETextfrom email.header import Header# 第三方SMTP服务mail_server = 'smtp.163.com'mail_port = 587mail_user = 'daw***an@163.com'mail_pass = '*********' # 这里要使用授权码,而不是邮箱密码sender = 'daw***an@163.com'receivers = ['daw***an@163.com','dav***an@live.com','dav***an@qq.com']# 需要定义三个参数# 第一个参数为文本内容# 第二个参数plain设置文本格式# 第三个参数utf-8设置编码格式message = MIMEText("Python发送邮件", "plain", "utf-8")message['From'] = sender # 由于邮箱安全认证,需要把发件人放到message['From'], 否由会”出现554 SPM错误“message['To'] = sender # 由于邮箱安全认证,需要把发件人放到message['To'], 否由会”出现554 SPM错误“subject = 'Python SMTP发送邮件测试'message['Subject'] = Header(subject, 'utf-8')try: mailserver = smtplib.SMTP_SSL(mail_host, 587) mailserver.login(mail_user, mail_pass) mailserver.sendmail(sender, receivers, message.as_string()) mailserver.quit() print('邮件发送成功')except smtplib.SMTPException: print("发送邮件失败!")
- 163邮箱用户smtp和获取授权码的方法如下图所示:
![94d21cdc7a95cdca2fca88a3e529975a.png](https://i-blog.csdnimg.cn/blog_migrate/ef0bf8e92a15db88dbd03d8c5df73b45.jpeg)
163邮箱用户smtp和获取授权码
- 发送网邮件效果:
![b9d6da1b119033eb60d939efc042beab.png](https://i-blog.csdnimg.cn/blog_migrate/b8a47b0a91bc60de7f5d13b90bc44862.jpeg)
发送邮件效果图
二、封装通用发邮件工具类
import osimport randomimport smtplibimport timefrom email.header import Headerfrom email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipartclass EmailUtils(object): def __init__(self, sender, receivers, username, password, smtp_server='smtp.qq.com', smtp_port=465): self.sender = sender self.receivers = receivers self.username = username self.password = password self.smtp_server=smtp_server self.smtp_port = smtp_port # 连接服务器 self.connect_server() def create_email(self, title): # 创建一个带附件的实例 message = MIMEMultipart() message['From'] = self.sender message['To'] = self.receivers message['Subject'] = Header(title, 'utf-8') self.message = message # 附件内容,如文本文件,图片文件等 def email_appendix(self, file_path): att1 = MIMEText(open(file_path, 'rb').read(), 'base64', 'utf-8') # 指定头部信息 att1['Content-Type'] = 'application/octet-stream' #内容为二进制流 att1['Content-Disposition'] = 'attachment; filename="%s"'%(os.path.basename(file_path)) self.message.attach(att1) def email_content(self, content, content_type='plain'): # 邮件正文内容 # plain正常文本内容,html可以发送html格式内容 self.message.attach(MIMEText(content, content_type, 'utf-8')) def connect_server(self): # 连接邮件的服务器 mailserver = smtplib.SMTP_SSL(self.smtp_server, self.smtp_port) try: # 连接qq的smtp服务器 mailserver.login(self.username, self.password) except smtplib.SMTPAuthenticationError: print('请检查用户名和授权码是否正确') return else: self.mailserver = mailserver def send_email(self): # 发送一封邮件 self.mailserver.sendmail(self.sender, self.receivers, self.message.as_string()) print('发送邮件成功!') def __del__(self): self.mailserver.close()if __name__ == "__main__":# 此处的密码为授权码 mail = EmailUtils(sender='dav***an@qq.com', receivers='dav***an@live.com', username='dav***an@qq.com', password='**********') mail.create_email('创建一封测试邮件') # 添加邮件内容 mail.email_content('您好!欢迎阅读我的内容') # 添加附件 mail.email_appendix('background.jpg') # 发送邮件 mail.send_email()
- QQ邮箱获取授权码方法
![d553e5fe788be798bd6fd9219f49cb2e.png](https://i-blog.csdnimg.cn/blog_migrate/dfa307ccaebd49a171952500ec057b69.jpeg)
QQ邮箱获取授权码
![ea8ee9d6a73184034040834c343b314c.png](https://i-blog.csdnimg.cn/blog_migrate/72a4d58a4a9d4ec8fd0980e4f3406323.jpeg)
邮件效果