获取smtp授权码:
1.进入设置
2.进入账户
3.开启smtp服务并且获取授权码(可以生成多个授权码,授权码就是后面代码的key)
#无需安装第三方库
key='***************' #换成你的QQ邮箱SMTP的授权码(QQ邮箱设置里)
EMAIL_ADDRESS='**********@qq.com' #换成你的邮箱地址
EMAIL_PASSWORD=key
import smtplib
smtp=smtplib.SMTP('smtp.qq.com',25)
import ssl
context=ssl.create_default_context()
sender=EMAIL_ADDRESS #发件邮箱
receiver=EMAIL_ADDRESS
#收件邮箱
from email.message import EmailMessage
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
EMAIL_ADDRESS='********@qq.com'
EMAIL_PASSWORD=key
smtp=smtplib.SMTP('smtp.qq.com',25)
import ssl
context=ssl.create_default_context()
sender=EMAIL_ADDRESS
receiver=EMAIL_ADDRESS
from email.message import EmailMessage
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='./1.png'
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)
key='n************a'
import smtplib
EMAIL_ADDRESS='***********@qq.com'
EMAIL_PASSWORD=key
smtp=smtplib.SMTP('smtp.qq.com',25)
import ssl
context=ssl.create_default_context()
sender=EMAIL_ADDRESS
receiver=EMAIL_ADDRESS
from email.message import EmailMessage
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(
"""\
This 我is an email sent by Python
""",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
EMAIL_ADDRESS='********@qq.com'
EMAIL_PASSWORD=key
smtp=smtplib.SMTP('smtp.qq.com',25)
import ssl
context=ssl.create_default_context()
sender=EMAIL_ADDRESS
receiver=EMAIL_ADDRESS
from email.message import EmailMessage
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)