【发布时间】:2018-12-26 16:56:48
【问题描述】:
我正在编写一个python 脚本,该脚本连接到 Outlook 并保存电子邮件中的附件(如果指定路径中有)。
目前,该脚本连接到我的本地 outlook。
如何连接到本地以外的 Outlook 电子邮件?
下面是我目前使用的脚本
import os
os.environ["NLS_LANG"] = ".AL32UTF8"
import socket
import win32com.client
import datetime
from datetime import timedelta
hstname = socket.gethostname()
date_time = datetime.datetime.now().strftime("%m%d%Y%H%M%S")
#connects to the local outlook
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6).Folders("Received Files")
move_fol = inbox.Folders("Processed_Files")
messages = inbox.Items
attachment_name = 'bjc_file'
#Loop to pick messages that are unread
for message in messages:
if message.Unread == True:
attachments = message.Attachments
for attachment in attachments:
if attachment_name in attachment.Filename:
for i in range (attachments.count):
attachment = attachments.Item(i+1)
attachmentname = str(attachment).replace('.csv', '_' + date_time + '.csv')
attachment.SaveASFile('C:\\Users\\Desktop\\Work\\BJC_Files' + '\\' + attachmentname)
message.move(move_fol)
【问题讨论】:
标签: python-3.x outlook python-3.5 win32com