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

Send QQ email in Python

編輯:Python

Sign in QQ, Get into QQ Email page , Configure mailbox POP3 Information

Send a text message , Click I've sent

To write python Code , Send text

#!/usr/bin/env python
# -*- coding:utf-8 _*-
"""
@author: Ten
@Time: 2022/6/22 11:47
"""
import smtplib
import ssl
from email.message import EmailMessage
# No need to install third-party libraries
key = ' Email authorization code ' # Change it to yours QQ mailbox SMTP Authorization code of (QQ Mailbox settings )
EMAIL_ADDRESS = '190****[email protected]' # Change to your email address
EMAIL_PASSWORD = key
smtp = smtplib.SMTP('smtp.qq.com', 25)
context = ssl.create_default_context()
sender = EMAIL_ADDRESS # Sending mailbox
receiver = EMAIL_ADDRESS # Mail to
subject = "python email subject" # Email title
body = "Hello,this is an email sent by python!" # Email content
msg = EmailMessage()
msg['subject'] = subject # Email subject
msg['From'] = sender
msg['To'] = receiver
msg.set_content(body) # Email content
with smtplib.SMTP_SSL("smtp.qq.com", 465, context=context) as smtp:
smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
smtp.send_message(msg)

Sending attachments

key=' Email authorization code '
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=' Attachment name '
with open(file_name,'rb') as f:
file_data=f.read()
msg.add_attachment(file_data,maintype='image',subtype='png',filename=file_name) # Modify as required , Attached picture this time
with smtplib.SMTP_SSL("smtp.qq.com",465,context=context) as smtp:
smtp.login(EMAIL_ADDRESS,EMAIL_PASSWORD)
smtp.send_message(msg)

Send mail test report HTML Format

key=' Authorization code '
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 I 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)

Configure to send mail to multiple users

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] # Multiple users
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)

Send successful screenshot


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