Python使用smtplib发送邮件

http://www.w3cschool.cc/python/python-email.htmlfrom
# -*- coding: gb2312 -*-
import smtplib
from email.mime.text import MIMEText


mailto_list=["[email protected]"]
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","测试文件"): 
       print "发送成功" 
    else: 
       print "发送失败"


 
  
 
  
 
  

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

# -*- coding: cp936 -*-

import smtplib
from email.mime.text import MIMEText

print "debug"

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

print "here"

message = """From: From Person 
To: To Person 
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

This is HTML message.

This is headline.

""" 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=["[email protected]"]
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"'	# 这种重命名附件名称的方式不知道为什么不成功
    att1.add_header('Content-Disposition','attachment', filename = '1.test')
    msg.attach(att1)

    body = MIMEText(content,_subtype='html',_charset='gb2312')		#创建一个实例,这里设置为html格式邮件

    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","测试文件"):
        print "发送成功"
    else:
        print "发送失败"  


更加详细的BLOG:

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


你可能感兴趣的:(python)