您的位置:首页 > 编程语言 > Python开发

Python使用smtplib发送邮件

2014-08-29 17:34 836 查看
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 发送带附件电子邮件

/article/8418960.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: