目錄
- 一、兩個模塊
- 二、SMTP端口
- 三、四大步驟
- 1、構造郵件內容
- 2、連接郵件服務器
- 3、登陸郵件服務器
- 4、發送郵件
- 四、常用場景
一、兩個模塊
Python使用SMTP發送郵件的兩個模塊:smtplib模塊、email模塊。
- smtplib:負責發送郵件
- email:負責構建郵件
二、SMTP端口
1)未加密端口,smtplib.SMTP接口,端口:25
2)使用SSL加密,smtplib.SMTP_SSL接口,端口:465
3)使用TLS加密,端口:587
三、四大步驟
1、構造郵件內容
# 純文本
msg = MIMEText(content)
# 附件
msg = MIMEMultipart()
2、連接郵件服務器
s = smtplib.SMTP("smtp.qq.com", 25)
3、登陸郵件服務器
s.login(msg_from, passwd)
msg_from:指發送者的郵箱
passwd:指發送者的密碼,這個密碼不是你的QQ登陸密碼,而是你在QQ郵箱設置開啟SMTP之后的一個授權碼

4、發送郵件
s.sendmail(msg_from, msg_to, msg.as_string())
msg_from:發送方
msg_to:收件方
msg.as_string():要發送的消息
四、常用場景
1、純文本郵件
import smtplib
from email.mime.text import MIMEText
from email.header import Header
# 發送者
msg_from = "xxxxx@qq.com"
# 這里的密碼不是QQ郵箱的密碼,而是在設置里開啟SMTP服務器后的授權碼
passwd = "xxxxx"
# 接受者
msg_to = "xxxx@qq.com"
# 郵件文本
content = 'Python 郵件發送測試...'
# 郵件主題
subject = "test"
# 生成一個MIMEText對象(還有一些其它參數)
msg = MIMEText(content)
# 放入郵件主題
msg['Subject'] = Header(subject, 'utf-8')
# 放入發件人
msg['From'] = msg_from
try:
# 連接郵件服務器
s = smtplib.SMTP("smtp.qq.com", 25)
# 登錄到郵箱
s.login(msg_from, passwd)
# 發送郵件:發送方,收件方,要發送的消息
s.sendmail(msg_from, msg_to, msg.as_string())
print('成功')
except s.SMTPException as e:
print(e)
finally:
s.quit()
2、發送html文本
import smtplib
from email.mime.text import MIMEText
from email.header import Header
# 發送者
msg_from = "xxxx@qq.com"
# 這里的密碼不是QQ郵箱的密碼,而是在設置里開啟SMTP服務器后的授權碼
passwd = "xxxx"
# 接受者
msg_to = "xxxx@qq.com"
# 郵件文本
content = """
p>Python 郵件發送測試.../p>
p>a rel="external nofollow" >這是一個鏈接/a>/p>
"""
# 郵件主題
subject = "test"
# 生成一個MIMEText對象(
msg = MIMEText(content, 'html', 'utf-8')
# 放入郵件主題
msg['Subject'] = Header(subject, 'utf-8')
# 放入發件人
msg['From'] = msg_from
try:
# 連接郵件服務器
s = smtplib.SMTP("smtp.qq.com", 25)
# 登錄到郵箱
s.login(msg_from, passwd)
# 發送郵件:發送方,收件方,要發送的消息
s.sendmail(msg_from, msg_to, msg.as_string())
print('成功')
except s.SMTPException as e:
print(e)
finally:
s.quit()
3、發送附件
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
# 發送者
msg_from = "xxxx@qq.com"
# 這里的密碼不是QQ郵箱的密碼,而是在設置里開啟SMTP服務器后的授權碼
passwd = "xxxx"
# 接受者
msg_to = "xxxx@qq.com"
# 郵件主題
subject = "test"
# 生成一個MIMEMultipart對象(
msg = message = MIMEMultipart()
# 郵件文本
message.attach(MIMEText('這是菜鳥教程Python 郵件發送測試……', 'plain', 'utf-8'))
# 放入郵件主題
msg['Subject'] = Header(subject, 'utf-8')
# 放入發件人
msg['From'] = msg_from
# 添加附件
att1 = MIMEText(open('./wordcloud_singer.py', 'rb').read(), 'base64', 'utf-8')
att1["Content-Type"] = 'application/octet-stream'
att1["Content-Disposition"] = 'attachment; filename="test.txt"'
msg.attach(att1)
try:
# 連接郵件服務器
s = smtplib.SMTP("smtp.qq.com", 25)
# 登錄到郵箱
s.login(msg_from, passwd)
# 發送郵件:發送方,收件方,要發送的消息
s.sendmail(msg_from, msg_to, msg.as_string())
print('成功')
except s.SMTPException as e:
print(e)
finally:
s.quit()
以上就是Python 發送SMTP郵件的簡單教程的詳細內容,更多關于Python 發送郵件的資料請關注腳本之家其它相關文章!
您可能感興趣的文章:- python實現自動化辦公郵件合并功能
- Python利用機器學習算法實現垃圾郵件的識別
- Python一行代碼實現自動發郵件功能
- Python基礎詳解之郵件處理
- Python 調用API發送郵件
- Python基于SMTP發送郵件的方法
- python基于SMTP發送QQ郵件
- python 自動監控最新郵件并讀取的操作
- python實現發送郵件
- python 實現網易郵箱郵件閱讀和刪除的輔助小腳本
- python如何發送帶有附件、正文為HTML的郵件
- python使用Windows的wmic命令監控文件運行狀況,如有異常發送郵件報警
- 用python監控服務器的cpu,磁盤空間,內存,超過郵件報警
- python郵件中附加文字、html、圖片、附件實現方法
- Python用20行代碼實現完整郵件功能