python发送zipQQ邮箱
时间: 2023-11-04 14:06:48 浏览: 68
python实现126邮箱发送邮件
以下是Python代码示例,用于将zip文件作为附件发送到QQ邮箱:
```python
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email import encoders
# 发件人邮箱地址
fromaddr = "your_email@qq.com"
# 发件人邮箱密码
password = "your_password"
# 收件人邮箱地址
toaddr = "recipient_email@qq.com"
# 创建邮件对象并设置主题
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Sending zip file as attachment"
# 添加邮件正文
body = "Please find the attached zip file."
msg.attach(MIMEText(body, 'plain'))
# 添加zip文件附件
filename = "example.zip"
attachment = open("example.zip", "rb")
p = MIMEBase('application', 'octet-stream')
p.set_payload((attachment).read())
encoders.encode_base64(p)
p.add_header('Content-Disposition', "attachment; filename= %s" % filename)
msg.attach(p)
# 发送邮件
server = smtplib.SMTP('smtp.qq.com', 587)
server.starttls()
server.login(fromaddr, password)
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()
print("Email sent successfully")
```
请注意,您需要将代码中的“your_email@qq.com”和“your_password”替换为您自己的QQ邮箱地址和密码,将“recipient_email@qq.com”替换为收件人的QQ邮箱地址,并在代码中提供要附加的zip文件的正确路径和名称。
阅读全文