Python上使用第三方STMP
来源:互联网 发布:windows日志服务器 编辑:程序博客网 时间:2024/06/14 07:06
菜鸟级别的我,学习了基本的Python语法,今天把SMTP内容整理一下。
python的smtplib提供了一种很方便的途径发送电子邮件。在此前需要了解到电脑上是否已安装了支持SMTP 服务,如:sendmail
我的电脑上没有安装,所以用的是第三方STMP服务发送。用的是QQ邮箱,需要在QQ邮箱里点击设置,开启STMP服务,获得授权码。下面是两个例子:
例1、仅仅发送文本内容:
# -*- coding: utf-8 -*-#借助Python使用QQ邮箱发送邮件import smtplibfrom email.mime.text import MIMEText_user="***@qq.com"#发送者_pwd="***"# 在QQ邮箱中开启SMTP后获得的授权码_to="***@qq.com"#接受者msg=MIMEText("Test")#邮件内容msg["Subject"]="anything u like is ok"#邮件主题msg["From"]=_usermsg["To"]=_totry: s=smtplib.SMTP_SSL("smtp.qq.com",465)#建立SSL连接 ,SMTP服务器:smtp.qq.com ,SSL端口465 s.login(_user,_pwd)#登录服务器 s.sendmail(_user,_to,msg.as_string()) s.quit() print "Success!"except smtplib.SMTPException,e: print "Failed,%s" %e
例2、发送HTML格式的邮件,并附图片
# -*- coding: utf-8 -*-#借助Python使用QQ邮箱发送邮件,附带链接和图片显示import smtplibfrom email.mime.text import MIMETextfrom email.mime.image import MIMEImagefrom email.mime.multipart import MIMEMultipartfrom email.header import Header_user="***@qq.com"#发送者_pwd="***"# 在QQ邮箱中开启SMTP后获得的授权码_to="***@qq.com"#接受者#msg=MIMEText("Test")#邮件内容msg=MIMEMultipart('related')msg["Subject"]="anything you like is ok"#邮件主题msg["From"]=_usermsg["To"]=_tomsgAlter=MIMEMultipart('alternative')msg.attach(msgAlter)mail_msg="""<p>this is a test for picture...</p><p><a href="http://www.baidu.com"> 度娘一下了</a></p><p>show a pic: </p><p><img src="cid:image1"></p>"""msgAlter.attach(MIMEText(mail_msg,'html','utf-8'))#指定图片为当前目录fp=open('image1.png','rb')msgImage=MIMEImage(fp.read())fp.close()#定义图片ID,在HTML文本中引用msgImage.add_header('Content-ID','<image1>')msg.attach(msgImage)try: s=smtplib.SMTP_SSL("smtp.qq.com",465)#建立SSL连接 ,SMTP服务器:smtp.qq.com ,SSL端口465 s.login(_user,_pwd)#登录服务器 s.sendmail(_user,_to,msg.as_string()) s.quit() print "Success!"except smtplib.SMTPException,e: print "Failed,%s" %e
0 0
- Python上使用第三方STMP
- 如何在mapreduce上使用第三方的Python库
- 第三方邮箱登录126,163等服务器配置STMP
- python第三方库使用
- Mac上python安装第三方库
- [Python]第三方库-Scrapy入门使用
- python第三方库使用经验
- Python下如何使用第三方函数库
- python中使用第三方模块
- 使用Pycharm安装Python第三方库
- Python第三方包(package)的使用
- 使用conda安装Python第三方工具包
- python第三方库numpy的使用
- Python入门:如何使用第三方库?
- Python第三方库的安装使用
- 使用Python调用Java第三方库
- PyThon第三方库
- 第三方python库
- web前端的css基础01
- 解决myeclipse2015破解后无法创建web项目
- 利用MATLAB演奏最炫民族风
- nyoj 467(表达式求值)
- Java进阶(三)Java安全通信:HTTPS与SSL
- Python上使用第三方STMP
- Java设计模式之适配器(Adapter)
- 线程之间通信
- 关于layer以及layui的使用心得
- MyBatis
- SQL语句中的Left Join、Right join、Inner Join 、Full Join关键字
- 【Java基础】——JSP九个内置对象
- 2017年腾讯、阿里暑期前端实习面试总结
- Java进阶(四)Java反射TypeToken解决泛型运行时类型擦除问题