- 這是我的第一篇文章,希望能幫助到你,以后我會繼續努力的
首先開啟POP3/SMTP服務
- 這里以我自己的QQ為例,打開QQ空間,點擊設定

- 點擊賬戶

- 向下拉找到POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服務選項設定
- 找到POP3/SMTP服務,在右邊設定開啟
- 點擊下方的 生成授權碼

- 復制生成的授權碼(后面代碼會需要)

接下來上代碼
import re #匹配郵箱,檢驗是否正確
import smtplib #發送郵箱
from email.header import Header #設定郵箱頭資訊
from email.mime.text import MIMEText #設定郵箱內容
class Send_EMail():
#服務器
Smtp_Server = "smtp.qq.com"
#授權碼(在QQ空間獲取到的授權碼)
License_Key = "×××××××××××××"
def __init__(self,Send_Mail,Receive_Mail,Title,content):
#發件人
self.Send_Mail = Send_Mail
#收件人
self.Receive_Mail = Receive_Mail
#郵箱標題
self.Title = Title
#郵箱內容
self.content = content
#正文設定,第一個引數為內容,第二個引數為內容格式,第三個引數是編碼格式
self.Email = MIMEText(content,"plain","utf-8")
self.Email["From"] = Header(Send_Mail)
self.Email["To"] = Header(Receive_Mail)
self.Email["Subject"] = Header(Title)
def Send(self):
#開啟服務,使用SSL加密傳輸
server = smtplib.SMTP_SSL(self.Smtp_Server)
server.connect(self.Smtp_Server,465)
#登陸
server.login(self.Send_Mail, self.License_Key)
#發送
server.sendmail(self.Send_Mail, self.Receive_Mail, self.Email.as_string())
#關閉服務
server.quit()
if __name__ == "__main__":
while(True):
Send_Mail = input("請輸入發件人QQ郵箱:")
#檢驗郵箱
if not re.compile(r"^[1-9][0-9]{4,10}@qq.com").match(Send_Mail):
print("格式錯誤···請重新輸入···")
continue
break;
while(True):
Receive_Mail = input("請輸入收件人QQ郵箱:")
#檢驗郵箱
if not re.compile(r"^[1-9][0-9]{4,10}@qq.com").match(Receive_Mail):
print("格式錯誤···請重新輸入···")
continue
break;
Title = input("請輸入發送的郵箱標題:")
content = input("請輸入發送的郵箱內容:")
SE = Send_EMail(Send_Mail,Receive_Mail,Title,content)
SE.Send()
測驗結果


- 本次分享到這里就結束了,感謝閱讀
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/278479.html
標籤:python
