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)