1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > smtp实现qq邮件发送

smtp实现qq邮件发送

时间:2024-02-22 13:04:20

相关推荐

smtp实现qq邮件发送

文本邮件的发送

#实现对邮件发送import smtplib#email实现对邮件构建from email.mime.text import MIMETextfrom email.header import Header"""smtp:simple mail transfer protocol 简单邮件传输协议"""#如何实现文本邮件的发送#plain :纯文本类型#配置邮件内容message=MIMEText(_text="python 邮件发送测试...",_subtype="plain",_charset="utf-8")#配置发送人信息message["From"]=Header("Tiramisu","utf-8")#配置收件人信息message["To"]=Header("Tiramisu+","utf-8")#配置邮件主题message["Subject"]=Header("python email send test","utf-8")smtpob=smtplib.SMTP()#host和port为qq的smtp服务器地址mail_host=""try:#连接smtp服务器smtpob.connect(host=mail_host,port="587")#用户登录,用户名为发送者地址,密码不是账号的密码,是授权码#如何获取授权码:发送者邮件点击设置--账户--开启pop3/smtp协议 获取授权码smtpob.login(user="1256021@",password="cwrfbbyibabh")#定义发送人sender="1256021@"#定义收件人,可以在列表里添加多人receiver=["1256021@"]#实现邮件发送smtpob.sendmail(sender,receiver,message.as_string())print("邮件发送成功")except smtplib.SMTPException:print("邮件发送失败")

html类型的邮件发送

import smtplibfrom email.header import Headerfrom email.mime.text import MIMEText#plain :纯文本类型 发送HTML类型的就把类型改为html#编辑邮件内容html="""<p>python 邮件发送测试</p><p><a href="">百度</a></p>"""#配置邮件文本message=MIMEText(_text=html,_subtype="html",_charset="utf-8")#配置发送人信息message["From"]=Header("Tiramisu","utf-8")#配置收件人信息message["To"]=Header("Tiramisu+","utf-8")#配置邮件主题message["Subject"]=Header("python email send test","utf-8")smtpob=smtplib.SMTP()#host和port为qq的smtp服务器地址mail_host=""try:#连接smtp服务器smtpob.connect(host=mail_host,port="587")#用户登录,用户名为发送者地址,密码不是账号的密码,是授权码#如何获取授权码:发送者邮件点击设置--账户--开启pop3/smtp协议 获取授权码smtpob.login(user="1256021@",password="cwrfbbyibabh")#定义发送人sender="1256021@"#定义收件人,可以在列表里添加多人receiver=["1256021@"]#实现邮件发送smtpob.sendmail(sender,receiver,message.as_string())print("邮件发送成功")except smtplib.SMTPException:print("邮件发送失败")

发送带附件的邮件

import smtplibfrom email.mime.multipart import MIMEMultipartfrom email.mime.text import MIMETextfrom email.header import Header#发送带附件的邮件message=MIMEMultipart()message.attach(MIMEText("这是一份测试文件...",_subtype="plain",_charset="utf-8"))att1=MIMEText(open("test.txt","rb").read(),"base64","utf-8")att1["Content-Type"]="application/octet-stream"#这里的filename可以任意写,写什么名字,邮件中显示什么名字att1["Content-Disposition"]="attachment;filename='1.txt' "message.attach(att1)#继续添加多个文件为附件att2=MIMEText(open("test.txt","rb").read(),"base64","utf-8")att2["Content-Type"]="application/octet-stream"#这里的filename可以任意写,写什么名字,邮件中显示什么名字att2["Content-Disposition"]="attachment;filename='2.txt' "message.attach(att2)#配置发送人信息message["From"]=Header("Tiramisu","utf-8")#配置收件人信息message["To"]=Header("Tiramisu+","utf-8")#配置邮件主题message["Subject"]=Header("python 发送带附件的邮件","utf-8")try:smtpob=smtplib.SMTP()#host和port为qq的smtp服务器地址mail_host=""#连接smtp服务器smtpob.connect(host=mail_host,port="587")#用户登录,用户名为发送者地址,密码不是账号的密码,是授权码#如何获取授权码:发送者邮件点击设置--账户--开启pop3/smtp协议 获取授权码smtpob.login(user="1256021@",password="cwrfbbyibabh")#定义发送人sender="1256021@"#定义收件人,可以在列表里添加多人receiver=["1256021@"]#实现邮件发送smtpob.sendmail(sender,receiver,message.as_string())print("邮件发送成功")except smtplib.SMTPException:print("邮件发送失败")

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。