程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

python發送QQ郵件

編輯:Python

登錄QQ,進入QQ郵箱頁面,配置郵箱POP3信息

發送短信,點擊我已發送

編寫python代碼,發送文本

#!/usr/bin/env python
# -*- coding:utf-8 _*-
"""
@author:拾壹
@Time: 2022/6/22 11:47
"""
import smtplib
import ssl
from email.message import EmailMessage
# 無需安裝第三方庫
key = '郵箱授權碼' # 換成你的QQ郵箱SMTP的授權碼(QQ郵箱設置裡)
EMAIL_ADDRESS = '190****[email protected]' # 換成你的郵箱地址
EMAIL_PASSWORD = key
smtp = smtplib.SMTP('smtp.qq.com', 25)
context = ssl.create_default_context()
sender = EMAIL_ADDRESS # 發件郵箱
receiver = EMAIL_ADDRESS # 收件郵箱
subject = "python email subject" # 郵件標題
body = "Hello,this is an email sent by python!" # 郵件內容
msg = EmailMessage()
msg['subject'] = subject # 郵件主題
msg['From'] = sender
msg['To'] = receiver
msg.set_content(body) # 郵件內容
with smtplib.SMTP_SSL("smtp.qq.com", 465, context=context) as smtp:
smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
smtp.send_message(msg)

發送附件

key='郵件授權碼'
import smtplib
import ssl
from email.message import EmailMessage
EMAIL_ADDRESS='********@qq.com'
EMAIL_PASSWORD=key
smtp=smtplib.SMTP('smtp.qq.com',25)
context=ssl.create_default_context()
sender=EMAIL_ADDRESS
receiver=EMAIL_ADDRESS
subject="python email subject"
body="Hello,this is an email sent by python!"
msg=EmailMessage()
msg['subject']=subject
msg['From']=sender
msg['To']=receiver
msg.set_content(body)
file_name='附件名字'
with open(file_name,'rb') as f:
file_data=f.read()
msg.add_attachment(file_data,maintype='image',subtype='png',filename=file_name) #按要求修改,此次是附帶圖片
with smtplib.SMTP_SSL("smtp.qq.com",465,context=context) as smtp:
smtp.login(EMAIL_ADDRESS,EMAIL_PASSWORD)
smtp.send_message(msg)

發送郵件測試報告HTML格式的

key='授權碼'
import smtplib
import ssl
from email.message import EmailMessage
EMAIL_ADDRESS='***********@qq.com'
EMAIL_PASSWORD=key
smtp=smtplib.SMTP('smtp.qq.com',25)
context=ssl.create_default_context()
sender=EMAIL_ADDRESS
receiver=EMAIL_ADDRESS
subject="python email subject"
body="Hello,this is an email sent by python!"
msg=EmailMessage()
msg['subject']=subject
msg['From']=sender
msg['To']=receiver
msg.set_content(body)
msg.add_alternative(
"""\
<!DOCTYPE html>
<html>
<body>
<h1 >This 我is an email sent by Python</h1>
</body>
</html>
""",subtype='html'
)
#msg.add_attachment(file_data,maintype='image',subtype='png',filename=file_name)
with smtplib.SMTP_SSL("smtp.qq.com",465,context=context) as smtp:
smtp.login(EMAIL_ADDRESS,EMAIL_PASSWORD)
smtp.send_message(msg)

配置給多個用戶發郵件

key='*************'
import smtplib
import ssl
from email.message import EmailMessage
EMAIL_ADDRESS='********@qq.com'
EMAIL_PASSWORD=key
smtp=smtplib.SMTP('smtp.qq.com',25)
context=ssl.create_default_context()
sender=EMAIL_ADDRESS
receiver=EMAIL_ADDRESS
subject="python email subject"
body="Hello,this is an email sent by python!"
msg=EmailMessage()
msg['subject']=subject
msg['From']=sender
msg['To']=[receiver,*****@qq.com,****@qq.com] #多個用戶
msg.set_content(body)
with smtplib.SMTP_SSL("smtp.qq.com",465,context=context) as smtp:
smtp.login(EMAIL_ADDRESS,EMAIL_PASSWORD)
smtp.send_message(msg)

發送成功截圖


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved