如何利用python发送邮件
一、zmial发送邮件
zmial是第三方库,需进行安装
pip install zmail
完成后,来给发一封邮件
subject:标题content_text:内容
import zmail server = zmail.server(’发件人邮箱地址’,’授权码’) server.send_mail(’收件人邮箱地址’,{’subject’:’Hello!’,’content_text’:’By zmail.’})
二、smtplib发送邮件
import smtplibfrom 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’]=sendermsg[’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 smtplibfrom email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipartimport ossmtpserver=’smtp.qq.com’port =465sender=’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 发送邮件的资料请关注好吧啦网其它相关文章!
相关文章: