蘋果應用市場慘不忍睹、自己的項目現在還是擱置一下、繼續好好學習!再次試驗了QQ發送的。
除了想要接受微信信息之外,還想收取郵件,現就爲大家介紹一下如何在JoinQuant的回測及研究中發送郵件!
(注:記得請先開啓郵箱的SMTP服務哦,下面以163郵箱爲例)
163郵箱函數形式如下:
def send_163_email(subject,message):
import smtplib
from email.mime.text import MIMEText
from email.header import Header
'''
記得請先開啓郵箱的SMTP服務
'''
## 發送郵件
sender = '量化模擬盤<[email protected]>' #發送的郵箱
receiver = '[email protected]' #要接受的郵箱(注:測試中發送其他郵箱會提示錯誤)
smtpserver = 'smtp.163.com'
username = '[email protected]' #你的郵箱賬號
password = '**********' #你的郵箱密碼
msg = MIMEText(str(message),'plain','utf-8') #中文需參數‘utf-8',單字節字符不需要
msg['Subject'] = Header(subject, 'utf-8') #郵件主題
msg['to'] = receiver
msg['from'] = sender #自己的郵件地址
smtp = smtplib.SMTP()
try :
smtp.connect('smtp.163.com') # 鏈接
smtp.login(username, password) # 登陸
smtp.sendmail(sender, receiver, msg.as_string()) #發送
print '郵件發送成功'
except:
print '郵件發送失敗'
smtp.quit() # 結束
df = get_price('000001.XSHE', start_date='2015-06-01', end_date='2015-06-05', frequency='daily', fields=['close','low'])
z = df['close']
send_163_email(subject='Hi,JoinQuant!',message=z)
結果如下:
由於QQ郵箱的smtp服務用了ssl認證,發往QQ郵箱函數形式如下:
# ============================ 代碼 =================================
def send_qq_email(subject,message): import smtplib from email.mime.text import MIMEText from email.header import Header ## 發送郵件 sender = '5***[email protected]' #發送的郵箱 receiver = '5***[email protected]' #要接受的郵箱(注:測試中發送其他郵箱會提示錯誤) smtpserver = 'smtp.qq.com' username = '5***[email protected]' #你的郵箱賬號 password = 'ee*******tg' #你的郵箱授權碼。一個16位字符串 msg = MIMEText(str(message),'plain','utf-8') #中文需參數‘utf-8',單字節字符不需要 msg['Subject'] = Header(subject, 'utf-8') #郵件主題 msg['to'] = receiver msg['from'] = sender #自己的郵件地址 server = smtplib.SMTP_SSL('smtp.qq.com') try : #server.connect() # ssl無需這條 server.login(username, password) # 登陸 server.sendmail(sender, receiver, msg.as_string()) #發送 print('郵件發送成功') except: print('郵件發送失敗') server.quit() # 結束 send_qq_email(subject='Info from JoinQuant', message="test mail")
你的郵箱授權碼。一個16位字符串:網頁版本登錄 QQ郵箱->設置->賬戶 這裏開啓即可。
如果使用joinquant的內置方法、需要用策略裏面的編輯運行纔可以、上部分代碼在pycharm上正常運行。
# ============================ 上面部分的執行代碼 =================================
df = get_price('000001.XSHE', start_date='2020-06-01', end_date='2020-06-05', frequency='daily',
fields=['close', 'low'])
z = df['close']
send_qq_email(subject='Hi,JoinQuant!',message=z)
發送結果:
這樣就成功了。 剩下的就是我現在看看pycharm上面要下載哪些庫才能執行、同步更新 共同繼續學習咯~ 加油