码迷,mamicode.com
首页 > 编程语言 > 详细

用Python写得用SMTP方式发送邮件

时间:2014-10-24 20:28:10      阅读:408      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   for   sp   

# -*- coding: cp936 -*-
#最简单的邮件发送
#!/usr/bin/python

import smtplib

sender = 13762385196@139.com
receivers = [13632582072@139.com]

message = """From: From Person <13762385196@139.com>
To: To Person <13632582072@139.com>
Subject: SMTP e-mail test

This is a test e-mail message.
"""

try:
    smtpObj = smtplib.SMTP(mail.10086.cn , 25 , localhost)
    smtpObj.login(13762385196,xinxin123)
    #while 1:
    smtpObj.sendmail(sender, receivers, message)
    print "Successfully sent mail"
except Exception:
    print "Error: unable to send email"

 

# -*- coding: cp936 -*-
#发送HTML格式的邮件
import smtplib
from email.mime.text import MIMEText
mailto_list=["13760264364@139.com"]
mail_host="mail.10086.cn"   #服务器
mail_user="13762385196"     #用户名
mail_pass="xinxin123"       #密码
mail_postfix="@139.com"     #发件箱的后缀

def send_mail(mailto_list,sub,content):   #to_list:收件人 sub:主题 content:内容
    me="<"+mail_user+mail_postfix+">"
    msg=MIMEText(content,_subtype=html,_charset=gb2312)
    msg[Subject]=sub   #设置主题
    msg[From]=me
    msg[To]=";".join(mailto_list)
    try:
        s=smtplib.SMTP()
        s.connect(mail_host)  #连接SMTP服务器
        s.login(mail_user,mail_pass)
        s.sendmail(me,mailto_list,msg.as_string())
        s.close()
        return True
    except Exception,e:
        print str(e)
        return False

if send_mail(mailto_list,"hello","<a href=‘http://www.cnblogs.com/xiaowuyi‘>小五义</a>"):
    print "发送成功"
else:
    print "发送失败"

        
    

 

#!/usr/bin/python
# -*- coding: cp936 -*-

import smtplib
sender=13762385196@139.com
receivers=["zhangxinxin@richinfo.cn","13632582072@139.com"]   #添加多个收信人用[]括起来
message = """From: From Person <13762385196@139.com>
To: To Person <13632582072@139.com>
MIME-Version: 1.0
Content-type: text/html
Subject: SMTP HTML e-mail test

This is an e-mail message to be sent in HTML format

<br><font color=‘red‘ size=15>test</font>
<h1>This is a headline.</h1>
"""

try:
    smtpObj=smtplib.SMTP(mail.10086.cn)
    smtpObj.login(13762385196,xinxin123)
    smtpObj.sendmail(sender,receivers,message)
    print "Successfully sent email"
except Exception:
    print "error"

 

# -*- coding: cp936 -*-
#添加附件发送邮件
#首先要创建MIMEMultipart()实例,然后构造附件,如果有多个附件,可依次构造

from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
import smtplib
import mimetypes

#创建带附件的实例
msg = MIMEMultipart()

#构造附件1
#这里‘\‘是转义符,所以\\相当于\
#‘rb‘中r表示读,b表示二进制
att1 = MIMEText(open(F:\\彩讯学习记录\\Python\\test1.txt,rb).read(),
                base64,gb2312)
att1["Content-Type"] = application/octet-stream
#这里的filename的值可以随意写,写什么邮件附件名称显示什么
att1["Content-Disposition"] = attachment; filename="test1.txt"
msg.attach(att1)

#构造附件2
att2 = MIMEText(open(F:\\彩讯学习记录\\Python\\test2.txt,rb).read(),
                base64,gb2312)
att2["Content-Type"] = application/octet-stream
att2["Content-Disposition"] = attachment; filename="test2.txt"
msg.attach(att2)

#添加图片
imagee = MIMEImage(open(F:\\个人资料\\psbCAL197EL.jpg,rb).read())
imagee.add_header(Content-ID,<image1>)
imagee["Content-Type"] = application/octet-stream
imagee["Content-Disposition"] = attachment; filename="image.jpg"  #确定放图片的位置和在邮件中显示的名称
msg.attach(imagee)

#加邮件头
msg[To] = 13632582072@139.com
msg[From] = 13762385196@139.com
msg[Subject] = hello world
msg[MIME-Version]=1.0
msg[Content-type]=text/html
msg[Content]=<h1>This is a headline.</h1>
#加邮件正文
txt = MIMEText(<h1>我是正文</h1>,_subtype=html,_charset=gb2312)
msg.attach(txt)

#发送邮件
try:
    smtpObj=smtplib.SMTP(mail.10086.cn)
    smtpObj.login(13762385196,xinxin123)
    smtpObj.sendmail(msg[From],msg[To],msg.as_string())
    smtpObj.quit()
    print "send successfully"
except Exception,e:
    print str(e)

 

用Python写得用SMTP方式发送邮件

标签:style   blog   http   color   io   os   ar   for   sp   

原文地址:http://www.cnblogs.com/xinxinjava/p/4048993.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!