Python發郵件代碼

wn25 9年前發布 | 2K 次閱讀 Python

author = 'administrator'

encoding: utf-8

!/usr/bin/python

import smtplib
from email.mime.text import MIMEText
mailto_list=['xx@qq.com','xx@126.com']
mail_host="smtp.126.com" #設置服務器
mail_user="xx@126.com" #用戶名
mail_pass="123456" #口令
mail_postfix="126.com" #發件箱的后綴

def send_mail(to_list,sub,content):
me="hello"+"<"+mail_user+"@"+mail_postfix+">"
msg = MIMEText(content,_subtype='plain',_charset='utf-8')
msg['Subject'] = sub
msg['From'] = me
msg['To'] = ";".join(to_list)
try:
server = smtplib.SMTP()
server.connect(mail_host)
server.login(mail_user,mail_pass)
server.sendmail(me, to_list, msg.as_string())
server.close()
return True
except Exception, e:
print str(e)
return False
if name == 'main':
if send_mail(mailto_list,"hello","hello world!我是中國"):
print "發送成功"
else:
print "發送失敗" </pre>

 本文由用戶 wn25 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!