smtp郵件測試
阿新 • • 發佈:2018-01-22
except set rec 設置 ... == () ring sendmai
# -*- coding:utf-8 -*-
#!/usr/bin/python
import sys
reload(sys) # reload 才能調用 setdefaultencoding 方法
sys.setdefaultencoding(‘utf-8‘) # 設置 ‘utf-8‘
import smtplib
from email.mime.text import MIMEText
from email.header import Header
#第三方服務smtp
mail_host=‘smtp.yeah.net‘
mail_user=‘xxx‘
mail_pass=‘xxx‘
mail_postfix= ‘yeah.net‘
receivers=‘[email protected]‘
def send_mail():
me = mail_user+"<"+mail_user+"@"+mail_postfix+">" #郵件發送方
subject = "Python SMTP 郵件測試test"
message = MIMEText(‘Python 郵件發送測試...‘, ‘plain‘, ‘utf-8‘) #郵件內容
message[‘subject‘] = subject #郵件標題
message[‘from‘] = me #發送方
message[‘to‘] = receivers #接收方
try:
smtpObj = smtplib.SMTP()
smtpObj.connect(mail_host) #連接smtp
smtpObj.login(mail_user,mail_pass) #登錄
smtpObj.sendmail(me,receivers, message.as_string()) #獲取參數發送郵件
smtpObj.close() #關閉
print "郵件發送成功"
except smtplib.SMTPException,e:
print "ERROR,無法發送郵件"
print e
if __name__=="__main__":
send_mail()
smtp郵件測試