python 发邮件 超大附件
时间: 2024-02-27 10:50:48 浏览: 36
在Python中发送邮件并附加超大附件可以使用smtplib和email库。下面是一个简单的示例代码:
```python
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
def send_email(sender_email, sender_password, receiver_email, subject, message, attachment_path):
# 创建邮件对象
msg = MIMEMultipart()
msg['From'] = sender_email
msg['To'] = receiver_email
msg['Subject'] = subject
# 添加邮件正文
msg.attach(MIMEText(message, 'plain'))
# 添加附件
attachment = open(attachment_path, 'rb')
part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % attachment_path)
msg.attach(part)
# 发送邮件
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(sender_email, sender_password)
server.sendmail(sender_email, receiver_email, msg.as_string())
server.quit()
# 使用示例
sender_email = "your_email@gmail.com"
sender_password = "your_password"
receiver_email = "recipient_email@gmail.com"
subject = "Email with Large Attachment"
message = "Please find the attached file."
attachment_path = "path_to_your_attachment"
send_email(sender_email, sender_password, receiver_email, subject, message, attachment_path)
```
请确保你已经安装了smtplib和email库,可以使用以下命令进行安装:
```
pip install secure-smtplib
pip install email
```
注意,这个示例使用了Gmail的SMTP服务器,如果你使用其他邮箱,请根据相应的SMTP服务器和端口进行修改。