Python使用smtplib发送邮件

http://www.w3cschool.cc/python/python-email.htmlfrom
<pre name="code" class="python"># -*- coding: gb2312 -*-
import smtplib
from email.mime.text import MIMEText


mailto_list=["284208276@qq.com"]
mail_host="smtp.126.com" #设置服务器
mail_user="werm520" #用户名
mail_pass="******" #口令
mail_postfix="126.com" #发件箱的后缀


def send_mail(to_list,sub,content): #to_list:收件人;sub:主题;content:邮件内容 
    me="hello"+"<"+mail_user+"@"+mail_postfix+">" #这里的hello可以任意设置,收到信后,将按照设置显示 
    msg = MIMEText(content,_subtype='html',_charset='gb2312') #创建一个实例,这里设置为html格式邮件 
    msg['Subject'] = sub #设置主题 
    msg['From'] = me 
    msg['To'] = ";".join(to_list) 
    try:
        s = smtplib.SMTP()
        s.connect(mail_host) #连接smtp服务器 
        s.login(mail_user,mail_pass) #登陆服务器 
        s.sendmail(me, to_list, msg.as_string()) #发送邮件 
        s.close() 
    return True
    except Exception, e: 
        print str(e) 
        return False


if __name__ == '__main__': 
    if send_mail(mailto_list,"hello","<a href='http://http://blog.csdn.net/werm520'>测试文件</a>"): 
       print "发送成功" 
    else: 
       print "发送失败"


 
 
 

或者你也可以在消息体中指定Content-type为text/html,如下实例:

# -*- coding: cp936 -*-

import smtplib
from email.mime.text import MIMEText

print "debug"

mailto_list=["284208276@qq.com"]
mail_host="smtp.126.com"  #设置服务器
mail_user="werm520"    #用户名
mail_pass="----"   #口令
mail_postfix="126.com"  #发件箱的后缀

print "here"

message = """From: From Person <from@fromdomain.com>
To: To Person <to@todomain.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

<b>This is HTML message.</b>
<h1>This is headline.</h1>
"""

print "try"

try:
    smtpObj = smtplib.SMTP()
    smtpObj.connect(mail_host)
    smtpObj.login(mail_user,mail_pass)
    smtpObj.sendmail(mail_user+'@'+mail_postfix, mailto_list, message)         
    print "Successfully sent email"
except Exception,e:
    print str(e)    
    print "Error: unable to send email"

发送带附件的邮件:

# -*- coding: gb2312 -*-
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

mailto_list=["284208276@qq.com"]
mail_host="smtp.126.com"  #设置服务器
mail_user="werm520"    #用户名
mail_pass="-----"   #口令
mail_postfix="126.com"  #发件箱的后缀

def send_mail(to_list,sub,content):  #to_list:收件人;sub:主题;content:邮件内容
    me="hello"+"<"+mail_user+"@"+mail_postfix+">"   #这里的hello可以任意设置,收到信后,将按照设置显示
    msg = MIMEMultipart()  #创建一个带附件的实例

    msg['Subject'] = sub    #设置主题
    msg['From'] = me
    msg['To'] = ";".join(to_list)

    att1 = MIMEText(open('f:\\激活码\\jihuoma.txt','rb').read(),'base64','gb2312')
#    att1["Content-Type"] = 'attachment; filename="test.txt"'<span style="white-space:pre">	</span># 这种重命名附件名称的方式不知道为什么不成功
    att1.add_header('Content-Disposition','attachment', filename = '1.test')
    msg.attach(att1)

    body = MIMEText(content,_subtype='html',_charset='gb2312')<span style="white-space:pre">		</span><span style="font-family: Arial, Helvetica, sans-serif;">#创建一个实例,这里设置为html格式邮件</span><span style="white-space:pre">
</span>
    msg.attach(body)

    try:
        s = smtplib.SMTP()
        s.connect(mail_host)  #连接smtp服务器
        s.login(mail_user,mail_pass)  #登陆服务器
        s.sendmail(me, to_list, msg.as_string())  #发送邮件
        s.close()
        return True
    except Exception, e:
        print str(e)
        return False
if __name__ == '__main__':
    if send_mail(mailto_list,"hello","<a href='http://http://blog.csdn.net/werm520'>测试文件</a>"):
        print "发送成功"
    else:
        print "发送失败"  


更加详细的BLOG:

Python SMTP 发送带附件电子邮件
http://blog.csdn.net/zm2714/article/details/7993732


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值