qq邮箱批量发Word附件 python代码
时间: 2024-09-26 13:06:51 浏览: 41
在Python中,可以使用smtplib和email库来实现QQ邮箱的SMTP服务发送邮件,并且附加Word文档作为附件。以下是一个简单的示例代码,假设你已经有了授权的SMTP服务器信息以及需要发送的收件人列表:
```python
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
# 邮箱设置
smtp_server = 'smtp.qq.com'
port = 465
username = 'your_qq_email' # 发送者的QQ邮箱地址
password = 'your_password' # 发送者的QQ邮箱授权码
sender = username
receivers = ['receiver1@example.com', 'receiver2@example.com'] # 收件人列表
# 创建邮件对象
msg = MIMEMultipart()
msg['From'] = sender
msg['To'] = ', '.join(receivers)
msg['Subject'] = 'QQ邮箱批量发送Word附件'
# 添加Word文档附件
word_file_path = 'path_to_your_word_file.docx' # Word文档路径
with open(word_file_path, 'rb') as f:
attachment = MIMEApplication(f.read(), _subtype='docx')
attachment.add_header('Content-Disposition', 'attachment', filename=word_file_name)
msg.attach(attachment)
# SMTP连接并发送邮件
try:
server = smtplib.SMTP_SSL(smtp_server, port)
server.login(username, password)
text = msg.as_string()
server.sendmail(sender, receivers, text)
print("邮件已成功发送")
except Exception as e:
print(f"发送邮件失败: {str(e)}")
# 关闭SMTP连接
server.quit()
阅读全文