1. 程式人生 > >python 發送郵件

python 發送郵件

utf-8 root str try finally tex pri con linu

#!/usr/bin/env python
#encoding=utf8
#coding=utf-8
import smtplib, sys
reload(sys)
sys.setdefaultencoding(‘utf8‘)
from email.mime.text import MIMEText
msg_from=‘[email protected]‘ #發送方郵箱
passwd=‘za512sb163512az‘ #填入發送方郵箱的授權碼
msg_to=‘[email protected]‘ #收件人郵箱

subject="python郵件測試" #主題
fileObj = open(‘/root/port.txt‘)
content=fileObj.read()
msg = MIMEText(content)
msg[‘Subject‘] = subject
msg[‘From‘] = msg_from
msg[‘To‘] = msg_to
#try:
s = smtplib.SMTP("smtp.163.com",25)
s.login(msg_from, passwd)
s.sendmail(msg_from, msg_to, msg.as_string())
# print "發送成功"
#except s.SMTPException,e:
# print "發送失敗"
#finally:
s.quit()

python 發送郵件