一、zmial发送邮件

zmial是第三方库,需进行安装

pip install zmail

完成后,来给发一封邮件

subject:标题
content_text:内容

 import zmail
 server = zmail.server('发件人邮箱地址','授权码')
 
 server.send_mail('收件人邮箱地址',{'subject':'Hello!','content_text':'By zmail.'})

二、smtplib发送邮件

import smtplib
from email.mime.text import MIMEText
#--------发件相关参数--------
smtpserver="smtp.qq.com"  #连接服务器
port = 465           #端口
sender = "741841851@qq.com"#账号
psw = "xxxxx"#密码 授权码
receiver="741841851@qq.com"#接收人

#--------编辑邮件内容--------

subject="qq邮件主题"
body= '<p>这个是发送的qq邮件</p>'
msg = MIMEText(body,'html','utf-8')
msg['from']=sender
msg['to']='741841851@qq.com'
msg['subject']=subject

#-----------test_email-------
smtp = smtplib.SMTP_SSL(smtpserver,port)#连接服务器
smtp.login(sender,psw)#登录
smtp.sendmail(sender,receiver,msg.as_string())#发送邮件
smtp.quit()

三、发送带附件的邮件

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import os

smtpserver='smtp.qq.com'
port =465
sender='741841851@qq.com'
psw = 'xxxx'
recevier = "741841851@qq.com"

filenamepath = os.path.join(os.path.dirname(os.path.realpath(__file__)),'ceshi.html')

with open(filenamepath,'rb') as f:
  mail_body=f.read().decode('utf-8')

msg = MIMEMultipart()
msg['from']=sender#发件人
msg['to']=recevier#收件人
msg['subject']='这是我的主题99'#主题

# 正文
body = MIMEText(mail_body,'html','utf-8')
msg.attach(body)
#附件
att = MIMEText(mail_body,'base64','gbk')#用utf-8会出现乱码
att['Content-Type']='application/octet-stream'
att['Content-Disposition']='attachment;filename="test_report.html"'
msg.attach(att)

####发送邮件
try:
  smtp = smtplib.SMTP()
  smtp.connect(smtpserver)#连接服务器
  smtp.login(sender,psw)#登录
except:
  smtp = smtplib.SMTP_SSL(smtpserver,port)
  smtp.login(sender,psw)#登录

smtp.sendmail(sender,recevier,msg.as_string())#发送邮件
smtp.quit()

以上就是如何利用python发送邮件的详细内容,更多关于python 发送邮件的资料请关注其它相关文章!

华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com

P70系列延期,华为新旗舰将在下月发布

3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。

而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?

根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。