新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:python如何實(shí)現(xiàn)郵件的收發(fā)
1、安裝郵件庫。

pip install PyEmail
2、郵件接口,需要開啟郵箱的設(shè)置-賬戶里SMTP服務(wù)。
接下來會(huì)通過發(fā)送短信驗(yàn)證來獲得授權(quán)碼,有了授權(quán)碼后就可以在代碼里添加了
3、設(shè)置登錄信息、發(fā)送內(nèi)容等,開啟smtp服務(wù),連接服務(wù)器。登錄郵箱發(fā)送或者接收郵件,最后關(guān)閉服務(wù)。
實(shí)例
import smtplib from email.mime.text import MIMEText from email.header import Header import time #發(fā)信方的信息:發(fā)信郵箱,QQ 郵箱授權(quán)碼 from_addr = "[email protected]" password = "xxxx" #收信方郵箱 to_addr = "[email protected]" #發(fā)信服務(wù)器 smtp_server = "smtp.qq.com" #郵箱正文內(nèi)容,第一個(gè)參數(shù)為內(nèi)容,第二個(gè)參數(shù)為格式(plain 為純文本),第三個(gè)參數(shù)為編碼 msg = MIMEText('羅本帥','plain','utf-8') #郵件頭信息 msg['From'] = Header(from_addr) msg['To'] = Header(to_addr) msg['Subject'] = Header('python') #開啟發(fā)信服務(wù),這里使用的是加密傳輸 server = smtplib.SMTP_SSL(smtp_server) server.connect(smtp_server,465) #登錄發(fā)信郵箱 server.login(from_addr,password) for i in range(2): #發(fā)送郵件 server.sendmail(from_addr,to_addr,msg.as_string()) time.sleep(1) server.quit()# 關(guān)閉服務(wù)器
以上就是python實(shí)現(xiàn)郵件收發(fā)的方法,希望對大家有所幫助。更多Python學(xué)習(xí)指路:創(chuàng)新互聯(lián)Python教程
本文教程操作環(huán)境:windows7系統(tǒng)、Python 3.9.1,DELL G3電腦。
網(wǎng)站欄目:創(chuàng)新互聯(lián)Python教程:python如何實(shí)現(xiàn)郵件的收發(fā)
本文URL:http://m.fisionsoft.com.cn/article/cdsdiog.html


咨詢
建站咨詢
