下载地址:http://m.pan38.com/download.php?code=TMZENR 提取码:2228
一、技术原理
本工具通过SMTP协议与QQ邮件服务器通信,主要实现以下功能:
读取Excel格式的收件人列
支持HTML格式邮件正
自动分批发送控制
发送结果日志记录
二、完整实现代码
import smtplib
import openpyxl
import time
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
class QQMailSender:
def init(self):
self.smtp_server = 'smtp.qq.com'
self.smtp_port = 465
self.sender = 'your_qq@qq.com'
self.password = 'your_authorization_code'
self.batch_size = 50 # 每批发送数量
self.interval = 60 # 批次间隔(秒)
def read_recipients(self, excel_path):
"""读取Excel收件人列表"""
wb = openpyxl.load_workbook(excel_path)
sheet = wb.active
recipients = []
for row in sheet.iter_rows(values_only=True):
if row[0]: # 假设第一列是邮箱
recipients.append({
'email': row[0],
'name': row[1] if len(row)>1 else ''
})
return recipients
def create_email(self, to_addr, to_name, subject, content):
"""创建邮件对象"""
msg = MIMEMultipart()
msg['From'] = self.sender
msg['To'] = f"{to_name} <{to_addr}>" if to_name else to_addr
msg['Subject'] = Header(subject, 'utf-8')
# HTML格式正文
html_part = MIMEText(content, 'html', 'utf-8')
msg.attach(html_part)
return msg
def send_batch(self, recipients, subject, content):
"""批量发送邮件"""
server = smtplib.SMTP_SSL(self.smtp_server, self.smtp_port)
server.login(self.sender, self.password)
success_count = 0
for i, recipient in enumerate(recipients):
try:
msg = self.create_email(
recipient['email'],
recipient['name'],
subject,
content
)
server.sendmail(self.sender, [recipient['email']], msg.as_string())
success_count += 1
print(f"发送成功: {recipient['email']}")
# 控制发送频率
if (i+1) % self.batch_size == 0:
print(f"已发送{i+1}封,等待{self.interval}秒...")
time.sleep(self.interval)
except Exception as e:
print(f"发送失败[{recipient['email']}]: {str(e)}")
server.quit()
return success_count
def run(self, excel_path, subject, content):
"""执行发送任务"""
print("开始读取收件人列表...")
recipients = self.read_recipients(excel_path)
print(f"共读取到{len(recipients)}个收件人")
print("开始发送邮件...")
start_time = time.time()
success = self.send_batch(recipients, subject, content)
print("\n发送完成!")
print(f"成功: {success} 失败: {len(recipients)-success}")
print(f"耗时: {time.time()-start_time:.2f}秒")
AI 代码解读
if name == 'main':
# 配置参数
excel_file = 'recipients.xlsx'
mail_subject = '重要通知'
mail_content = """
<html>
<body>
<h2>尊敬的客户:</h2>
<p>感谢您一直以来的支持!</p>
<p>这是我们最新的产品资讯...</p>
</body>
</html>
"""
# 创建并运行发送器
sender = QQMailSender()
sender.run(excel_file, mail_subject, mail_content)
AI 代码解读
三、使用说明
准备QQ邮箱:
开启SMTP服务(设置->账户->POP3/SMTP服务)
获取授权码代替密码
准备收件人列表:
创建Excel文件(recipients.xlsx)
第一列