需要发件人邮箱地址、授权码和收件人邮箱地址
1、登录QQ邮箱后台,点击右上角设置,下拉找到第三方服务,开启SMTP服务,复制生成的授权码
2、新建一个python文件,输入以下源码,更替参数后运行即可
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart# QQ邮箱账号和授权码
qq_email = 'xxxxxxqq@qq.com' # 替换为你的QQ邮箱
qq_password = 'your_key' # 替换为你的授权码# 收件人邮箱
to_email = 'xxxxxxxx@qq.com' # 收件人邮箱# 创建邮件内容
subject = '臭骗子' # 邮件标题
body = '臭骗子!' # 邮件内容# 构造邮件
msg = MIMEMultipart()
msg['From'] = qq_email
msg['To'] = to_email
msg['Subject'] = subject# 添加邮件内容
msg.attach(MIMEText(body, 'plain'))try:# 连接QQ邮箱SMTP服务器server = smtplib.SMTP('smtp.qq.com', 587) # QQ SMTP服务器及端口server.starttls() # 启动TLS加密server.login(qq_email, qq_password) # 登录# 发送邮件server.sendmail(qq_email, to_email, msg.as_string())print('Email sent successfully!')except Exception as e:print(f'Failed to send email: {e}')finally:server.quit() # 退出服务器
3、运行成功打印
4、邮箱查看结果
PS:是因为有个人骗我六块钱,加好友拉黑,只能发邮件出出气了。。。
----------------------------------------------------------------
定时发送循环版本:
import smtplib
import time
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart# QQ邮箱账号和授权码
qq_email = 'xxxxxx@qq.com' # 替换为你的QQ邮箱
qq_password = 'your_key' # 替换为你的授权码# 收件人邮箱
to_email = 'xxxxxx@qq.com' # 收件人邮箱# 创建邮件内容
subject = '臭骗子'
body = '臭骗子!'# 构造邮件
msg = MIMEMultipart()
msg['From'] = qq_email
msg['To'] = to_email
msg['Subject'] = subject# 添加邮件内容
msg.attach(MIMEText(body, 'plain'))while True:try:# 连接QQ邮箱SMTP服务器server = smtplib.SMTP('smtp.qq.com', 587) # QQ SMTP服务器及端口server.starttls() # 启动TLS加密server.login(qq_email, qq_password) # 登录# 发送邮件server.sendmail(qq_email, to_email, msg.as_string())print('Email sent successfully!')except smtplib.SMTPServerDisconnected as e:print(f'Failed to send email: {e}')print('Reconnecting...')time.sleep(5) # 等待 5 秒后重试continue # 重新连接except Exception as e:print(f'Failed to send email: {e}')finally:try:server.quit() # 退出服务器except Exception as e:print(f'Error during quitting: {e}')# 每60秒发送一次time.sleep(3)