申明:資料來源于網路及書本,通過理解、實踐、整理成學習筆記,
文章目錄
- 發送電子郵件
- 發送純文本格式的郵件
- 發送HTML格式的郵件
- 發送帶附件的郵件
- 發送圖片的郵件
- 接收電子郵件
- 使用POP3協議下載郵件
- 使用IMAP協議下載郵件
- 決議郵件
發送電子郵件
Python標準庫提供了smtplib,用于實作SMTP協議發送郵件,標準庫還提供email模塊幫助我們構建郵件格式,SMTP(Simple Mail Transfer Protocol,即簡單郵件傳輸協議),是一組有源地址到目的地址傳送郵件的規則,用來控制信件的中轉方式,
- 獲取QQ郵箱密碼(授權碼)

發送純文本格式的郵件
import smtplib
from email.mime.text import MIMEText
from email.header import Header
# 郵箱用戶名
sender = 'dad@qq.com'(輸入你的郵箱)
# 郵箱密碼(部分郵箱為授權碼)
password = '123456'(輸入你的密碼)
# 收件人郵箱地址,注意需要[]包裹,這意味著你可以寫多個郵件地址群發
receiver = ['baby@qq.com', ](輸入你要發送人的郵箱)
# 郵件正文
text = 'Hello,baby'
message = MIMEText(text, 'plain', 'utf-8')
# 發件人顯式的名字
message['From'] = Header('拿頭來堅持', 'utf-8')
# 收件人顯式的名字
message['To'] = Header('baby', 'utf-8')
# 郵件標題
message['Subject'] = '爸爸來信,請接收!'
try:
# 使用QQ企業郵箱服務器發送
smtp = smtplib.SMTP('smtp.qq.com')
# 登錄
smtp.login(sender, password)
# 發送
smtp.sendmail(sender, receiver, message.as_string())
print('郵件發送成功!')
# 退出服務器
smtp.quit()
except smtplib.SMTPException as e:
print('Error!郵件發送失敗!', e)
發送純文本格式的郵件執行結果:

發送HTML格式的郵件
import smtplib
from email.mime.text import MIMEText
from email.header import Header
# 郵箱用戶名
sender = 'dad@qq.com'(輸入你的郵箱)
# 郵箱密碼(部分郵箱為授權碼)
password = '123456'(輸入你的密碼)
# 收件人郵箱地址,注意需要[]包裹,這意味著你可以寫多個郵件地址群發
receiver = ['baby@qq.com', ](輸入你要發送人的郵箱)
# 郵件正文
msg = '''
<p><a href='https://blog.csdn.net/weixin_46382560?spm=1011.2124.3001.5343'>拿頭來堅持</p>
Life goes on, learning goes on
</p> <!----></div></div> <div class="user-profile-head-info-b" data-v-d1dbb6f8><ul data-v-d1dbb6f8><li data-v-d1dbb6f8><div class="user-profile-statistics-num" data-v-d1dbb6f8>22,574</div> <div class="user-profile-statistics-name" data-v-d1dbb6f8>被訪問量</div></li> <li data-v-d1dbb6f8><a href="javascript:;" data-v-d1dbb6f8><div class="user-profile-statistics-num" data-v-d1dbb6f8>24</div> <div class="user-profile-statistics-name" data-v-d1dbb6f8>原創文章</div></a></li> <li data-v-d1dbb6f8><a href="https://blog.csdn.net/rank/list/total" target="_blank" data-report-click="{"spm":"3001.5476"}" data-report-query="spm=3001.5476" data-v-d1dbb6f8><div class="user-profile-statistics-num" data-v-d1dbb6f8>128,997</div> <div class="user-profile-statistics-name" data-v-d1dbb6f8>作者排名</div></a></li> <li data-v-d1dbb6f8><a href="javascript:;" data-v-d1dbb6f8><div class="user-profile-statistics-num" data-v-d1dbb6f8>762</div> <div class="user-profile-statistics-name" data-v-d1dbb6f8>粉絲數量</div></a></li></ul></div></div></div> <div class="user-profile-body" data-v-3f0fdf46 data-v-80922f46><div class="user-profile-body-inner" data-v-3f0fdf46><div class="user-profile-body-left" data-v-3f0fdf46><div class="user-profile-aside" data-v-d487ed78 data-v-3f0fdf46><div class="user-general-info single-general-info" data-v-d487ed78><ul data-v-d487ed78><!----> <!----> <li class="user-general-info-join-csdn" data-v-d487ed78><i data-v-d487ed78></i> <span data-v-d487ed78>于</span> <span class="user-general-info-key-word" data-v-d487ed78>2020-02-22</span> <span data-v-d487ed78>加入CSDN</span></li></ul></div> <!----> <div class="user-achievement user-profile-aside-common-box" data-v-d487ed78><div class="aside-common-box-head" data-v-d487ed78>獲得成就</div> <div class="aside-common-box-bottom" data-v-d487ed78><div class="aside-common-box-content" data-v-d487ed78><ul data-v-d487ed78><li data-v-d487ed78>
<i style="background-image: url(https://img-home.csdnimg.cn/images/20210114022819.png)"></i>
<div>獲得<span>212</span>次點贊</div>
</li><li data-v-d487ed78>
<i style="background-image: url(https://img-home.csdnimg.cn/images/20210114022831.png)"></i>
<div>內容獲得<span>111</span>次評論</div>
</li><li data-v-d487ed78>
<i style="background-image: url(https://img-home.csdnimg.cn/images/20210114022828.png)"></i>
<div>獲得<span>562</span>次收藏</div>
'''
# 指定訊息體使用HTML格式
message = MIMEText(msg, 'html', 'utf-8')
# 發件人顯式的名字
message['From'] = Header('拿頭來堅持', 'utf-8')
# 收件人顯式的名字
message['To'] = Header('baby', 'utf-8')
# 郵件標題
message['Subject'] = '爸爸來信,請接收!'
try:
# 使用QQ企業郵箱服務器發送
smtp = smtplib.SMTP('smtp.qq.com')
# 登錄
smtp.login(sender, password)
# 發送
smtp.sendmail(sender, receiver, message.as_string())
print('郵件發送成功!')
# 退出服務器
smtp.quit()
except smtplib.SMTPException as e:
print('Error!郵件發送失敗!', e)
發送HTML格式的郵件的執行結果:

發送帶附件的郵件
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
# 郵箱用戶名
sender = 'dad@qq.com'(輸入你的郵箱)
# 郵箱密碼(部分郵箱為授權碼)
password = '123456'(輸入你的密碼)
# 收件人郵箱地址,注意需要[]包裹,這意味著你可以寫多個郵件地址群發
receiver = ['baby@qq.com', ](輸入你要發送人的郵箱)
# 指定訊息體使用復合型別
message = MIMEMultipart()
# 發件人顯式的名字
message['From'] = Header('拿頭來堅持', 'utf-8')
# 收件人顯式的名字
message['To'] = Header('baby', 'utf-8')
# 郵件標題
message['Subject'] = '爸爸來信,請接收!'
# 郵件正文
msg = '''
<p><a href='https://blog.csdn.net/weixin_46382560?spm=1011.2124.3001.5343'>拿頭來堅持</p>
Life goes on, learning goes on
</p> <!----></div></div> <div class="user-profile-head-info-b" data-v-d1dbb6f8><ul data-v-d1dbb6f8><li data-v-d1dbb6f8><div class="user-profile-statistics-num" data-v-d1dbb6f8>22,574</div> <div class="user-profile-statistics-name" data-v-d1dbb6f8>被訪問量</div></li> <li data-v-d1dbb6f8><a href="javascript:;" data-v-d1dbb6f8><div class="user-profile-statistics-num" data-v-d1dbb6f8>24</div> <div class="user-profile-statistics-name" data-v-d1dbb6f8>原創文章</div></a></li> <li data-v-d1dbb6f8><a href="https://blog.csdn.net/rank/list/total" target="_blank" data-report-click="{"spm":"3001.5476"}" data-report-query="spm=3001.5476" data-v-d1dbb6f8><div class="user-profile-statistics-num" data-v-d1dbb6f8>128,997</div> <div class="user-profile-statistics-name" data-v-d1dbb6f8>作者排名</div></a></li> <li data-v-d1dbb6f8><a href="javascript:;" data-v-d1dbb6f8><div class="user-profile-statistics-num" data-v-d1dbb6f8>762</div> <div class="user-profile-statistics-name" data-v-d1dbb6f8>粉絲數量</div></a></li></ul></div></div></div> <div class="user-profile-body" data-v-3f0fdf46 data-v-80922f46><div class="user-profile-body-inner" data-v-3f0fdf46><div class="user-profile-body-left" data-v-3f0fdf46><div class="user-profile-aside" data-v-d487ed78 data-v-3f0fdf46><div class="user-general-info single-general-info" data-v-d487ed78><ul data-v-d487ed78><!----> <!----> <li class="user-general-info-join-csdn" data-v-d487ed78><i data-v-d487ed78></i> <span data-v-d487ed78>于</span> <span class="user-general-info-key-word" data-v-d487ed78>2020-02-22</span> <span data-v-d487ed78>加入CSDN</span></li></ul></div> <!----> <div class="user-achievement user-profile-aside-common-box" data-v-d487ed78><div class="aside-common-box-head" data-v-d487ed78>獲得成就</div> <div class="aside-common-box-bottom" data-v-d487ed78><div class="aside-common-box-content" data-v-d487ed78><ul data-v-d487ed78><li data-v-d487ed78>
<i style="background-image: url(https://img-home.csdnimg.cn/images/20210114022819.png)"></i>
<div>獲得<span>212</span>次點贊</div>
</li><li data-v-d487ed78>
<i style="background-image: url(https://img-home.csdnimg.cn/images/20210114022831.png)"></i>
<div>內容獲得<span>111</span>次評論</div>
</li><li data-v-d487ed78>
<i style="background-image: url(https://img-home.csdnimg.cn/images/20210114022828.png)"></i>
<div>獲得<span>562</span>次收藏</div>
'''
# 郵件附加html檔案
message.attach(MIMEText(msg, 'html', 'utf-8'))
# 添加附件
attached_file = MIMEText(open(__file__, encoding='utf-8').read(), 'base64', 'utf-8')
# 指定附件的檔案名和原先的檔案不一樣
attached_file['Content-Disposition'] = 'attachment;filename="mail.py"'
# 郵件附加附件
message.attach(attached_file)
try:
# 使用QQ企業郵箱服務器發送
smtp = smtplib.SMTP('smtp.qq.com')
# 登錄
smtp.login(sender, password)
# 發送
smtp.sendmail(sender, receiver, message.as_string())
print('郵件發送成功!')
# 退出服務器
smtp.quit()
except smtplib.SMTPException as e:
print('Error!郵件發送失敗!', e)
發送帶附件的郵件執行結果:

發送圖片的郵件
import smtplib
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.header import Header
# 郵箱用戶名
sender = 'dad@qq.com'(輸入你的郵箱)
# 郵箱密碼(部分郵箱為授權碼)
password = '123456'(輸入你的密碼)
# 收件人郵箱地址,注意需要[]包裹,這意味著你可以寫多個郵件地址群發
receiver = ['baby@qq.com', ](輸入你要發送人的郵箱)
# 利用related定義內嵌資源的郵件體
message = MIMEMultipart('related')
# 發件人顯式的名字
message['From'] = Header('拿頭來堅持', 'utf-8')
# 收件人顯式的名字
message['To'] = Header('baby', 'utf-8')
# 郵件標題
message['Subject'] = '爸爸來信,請接收!'
# 郵件正文
content = MIMEMultipart('alternative')
# html內容
msg = '''
<p><a href='https://blog.csdn.net/weixin_46382560?spm=1011.2124.3001.5343'>拿頭來堅持</p>
Life goes on, learning goes on
<p>
拿頭來堅持的個人主頁
<img src='cid:img01'>
</p>
'''
# 郵件附加html檔案
message.attach(MIMEText(msg, 'html', 'utf-8'))
# 添加圖片
with open('csdn.png', 'rb') as f:
img01 = MIMEImage(f.read())
# 定義資源的名字為img01
img01.add_header('Content-ID', 'img01')
# 郵件附加圖片
message.attach(img01)
try:
# 使用QQ企業郵箱服務器發送
smtp = smtplib.SMTP('smtp.qq.com')
# 登錄
smtp.login(sender, password)
# 發送
smtp.sendmail(sender, receiver, message.as_string())
print('郵件發送成功!')
# 退出服務器
smtp.quit()
except smtplib.SMTPException as e:
print('Error!郵件發送失敗!', e)
發送圖片的郵件執行結果:

接收電子郵件
接受郵件有兩種常用的協議:POP3和IMAP協議
POP3協議(Post Office Protocol-Version3,即郵局協議版本3):允許電子郵件客戶端下載服務器上的郵件,但是在客戶端的操作(如移動郵件、標記已讀等)不會反饋到服務器上,比如通過客戶端收取了郵箱的3封郵件并移動到其他檔案夾,郵件服務器上的這些郵件不會被同步移動,
IMAP協議(Internet Mail Access Protocol,即Internet郵件訪問協議):提供Webmail與電子郵件客戶端之間的雙向通信,任何在客戶端做的改變都會同步到服務器上,在客戶端對郵件進行了操作,服務器上的郵件也會進行相應的操作,
使用POP3協議下載郵件
import poplib
from email.parser import Parser
# 登錄郵箱的用戶名
username = 'baby@qq.com'(輸入你的郵箱)
# 登錄郵箱的密碼(部分郵箱為授權碼)
password = '123456'(輸入你的密碼)
# 連接郵箱服務器
pop_server = poplib.POP3('pop.qq.com')
# 列印出郵箱服務器的歡迎文字
print(pop_server.getwelcome())
# 登錄郵箱服務器
pop_server.user(username)
pop_server.pass_(password)
# 列印出當前賬號的狀態,第一個回傳值為郵件數,第二個回傳值為占用空間
print('Server stat', pop_server.stat())
# 獲取所以郵件串列
resp, mails, octets = pop_server.list()
print(mails)
# 獲取最新的一封郵件(序列號最大的),郵件索引從1開始計數
index = len(mails)
resp, lines, octets = pop_server.retr(index)
content = b'\r\n'.join(lines).decode('utf-8')
# 決議出郵件
msg = Parser().parsestr(content)
# 可以根據郵件索引號直接從服務器洗掉郵件
# pop_server.dele(index)
# 關閉連接
pop_server.quit()
執行結果:
b'+OK XMail POP3 Server v1.0 Service Ready(XMail v1.0)'
Server stat (15, 50814)
[b'1 1255', b'2 1286', b'3 1310', b'4 1398', b'5 1458', b'6 1450', b'7 1602', b'8 1633', b'9 5001', b'10 2347', b'11 2371', b'12 2267', b'13 5033', b'14 5077', b'15 17326']
如果正確連接上服務器并且列出郵件數量,說明我們已經正確使用了POP3協議,
使用IMAP協議下載郵件
決議郵件
一個堅持學習,堅持成長,堅持分享的人,即使再不聰明,也一定會成為優秀的人!
如果看完覺得有所識訓的話,記得一鍵三連哦,謝謝大家!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/280365.html
標籤:python
