使用Python发送邮件(QQ邮箱为例)

Python自动发送邮件(直接套用即可)

代码前准备:

获取smtp授权码:
1.进入设置
使用Python发送邮件(QQ邮箱为例)_第1张图片
2.进入账户
使用Python发送邮件(QQ邮箱为例)_第2张图片
3.开启smtp服务并且获取授权码(可以生成多个授权码,授权码就是后面代码的key)

使用Python发送邮件(QQ邮箱为例)_第3张图片

1.1使用smtplib发送纯文本邮件

#无需安装第三方库
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)

1.2如何使用email.message来发送附件,HTML内容

1.2.1附带文件

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)

1.2.2附带HTML页面

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)

1.3给多个用户发邮件

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)

你可能感兴趣的:(Python,python)