通過SMTP協定自動發送郵件
為了更好地摸魚,想讓大模型訓練完之後發郵件給我。
開啟SMTP服務與取得授權碼
1.打開設定->賬戶->打開服務
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIyZuBnL1YTOzU2Y1QWM2cjZ5MTMiVzM2QzNkNGOhJGOlRGZlJzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
然後會獲得授權碼
代碼示例
#!/usr/bin/python
\# -*- coding: utf-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.header import Header
mail_host="smtp.qq.com"#設定的郵件伺服器host必須是發送郵箱的伺服器,與接收郵箱無關。
mail_user="[email protected]"#qq郵箱登陸名
mail_pass="you are so funny" #開啟stmp服務的時候并設定的授權碼,注意!不是QQ密碼。
sender='[email protected]'#發送方qq郵箱
receivers=['[email protected]']#接收方qq郵箱
message=MIMEText('測試發送 python 郵件','plain','utf-8')
message['From']=Header("colab",'utf-8') #設定顯示在郵件裡的發件人
message['To']=Header("you",'utf-8') #設定顯示在郵件裡的收件人
subject ='python smtp email test'
message['Subject']=Header(subject,'utf-8') #設定主題和格式
try:
smtpobj=smtplib.SMTP_SSL(mail_host,465) #本地如果有本地伺服器,則用localhost ,預設端口25,騰訊的(端口465或587)
smtpobj.set_debuglevel(1)
smtpobj.login(mail_user,mail_pass)#登陸QQ郵箱伺服器
smtpobj.sendmail(sender,receivers,message.as_string())#發送郵件
print("郵件發送成功")
smtpobj.quit()#退出
except smtplib.SMTPException as e :
print("Error:無法發送郵件")
print(e)
運作,發送成功
參考文獻:
機器學習小技巧:模型訓練完成後發送郵件
python實作QQ郵箱發送郵件
n實作QQ郵箱發送郵件](https://cloud.tencent.com/developer/article/1741852)