python 代码生成 imaplib操作outlook邮件实现邮件保存到草稿箱
时间: 2024-05-22 17:16:34 浏览: 11
import imaplib
import email
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
# 连接邮箱服务器
mail = imaplib.IMAP4_SSL('imap-mail.outlook.com')
# 登录邮箱
mail.login('your_email_address', 'your_password')
# 选择草稿箱
mail.select('Drafts')
# 创建邮件内容
msg = MIMEMultipart()
msg['From'] = 'your_email_address'
msg['To'] = 'recipient_email_address'
msg['Subject'] = 'Test email'
body = 'This is a test email sent using Python'
msg.attach(MIMEText(body, 'plain'))
# 附加图片
with open('image.jpg', 'rb') as f:
img_data = f.read()
img = MIMEImage(img_data, name='image.jpg')
msg.attach(img)
# 将邮件保存到草稿箱
mail.append('Drafts', '', imaplib.Time2Internaldate(time.time()), str(msg).encode('utf-8'))
# 关闭连接
mail.close()
mail.logout()
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)