python 发送.db格式附件的邮件
时间: 2023-10-17 11:12:52 浏览: 119
可以使用 Python 标准库中的 `smtplib` 和 `email` 模块来发送包含 `.db` 格式附件的邮件。下面是一个示例代码:
```python
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
# 设置发件人、收件人、主题、正文等信息
sender = 'your_email_address'
password = 'your_email_password'
receiver = 'recipient_email_address'
subject = 'Email with DB attachment'
body = 'Please see the attached DB file.'
# 创建一个包含正文和附件的邮件对象
msg = MIMEMultipart()
msg['From'] = sender
msg['To'] = receiver
msg['Subject'] = subject
# 添加正文
msg.attach(MIMEText(body, 'plain'))
# 添加附件
with open('/path/to/your/db/file.db', 'rb') as f:
attachment = MIMEApplication(f.read(), _subtype='db')
attachment.add_header('Content-Disposition', 'attachment', filename='file.db')
msg.attach(attachment)
# 连接发件邮箱服务器并登录
smtp_server = 'smtp.your_email_provider.com'
smtp_port = 587
server = smtplib.SMTP(smtp_server, smtp_port)
server.starttls()
server.login(sender, password)
# 发送邮件
server.sendmail(sender, receiver, msg.as_string())
# 关闭连接
server.quit()
```
将上述代码中的 `your_email_address`、`your_email_password`、`recipient_email_address`、`/path/to/your/db/file.db`、`smtp.your_email_provider.com` 和 `587` 分别替换为你的发件人邮箱地址、密码、收件人邮箱地址、`.db` 文件路径、SMTP 服务器地址和端口号。
阅读全文