【www.bbyears.com--python】
代码如下#!/usr/bin/env python
#-*- coding:UTF-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
sender = "xxxxxxx@163.com"
receiver = "xxxxx@163.com"
subject = "python email test中文"
smtpserver = "smtp.163.com"
username = "xxxxxx"
password = "xxxxxx"
sender = "xxxxxx@163.com"
rcpt = "xxxxxxxx@163.com"
msg = MIMEMultipart("alternatvie")
msg["Subject"] = Header("测试发信","utf-8") #组装信头
msg["From"] = r"%s
msg["To"] = rcpt
#html = open("html.tpl").read() #读取HTML模板
html = "name is ge哈哈"
html_part = MIMEText(html,"html") #实例化为html部分
html_part.set_charset("utf-8") #设置编码
msg.attach(html_part) #绑定到message里
try:
s = smtplib.SMTP("smtp.163.com") #登录SMTP服务器,发信
s.login("xxxx","xxxxxx")
s.sendmail(sender,rcpt,msg.as_string())
except Exception,e:
print "OK Ha"
"""
#发出来不能显示邮件正文
msg = MIMEText("what are you","text")#中文需参数‘utf-8’,单字节字符不需要 //发送不显示正文
msg["Subject"] = Header(subject, "utf-8")
msg["From"] = sender
msg["To"] = receiver
smtp = smtplib.SMTP()
smtp.connect("smtp.163.com")
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
print "OK"
"""