python - 尝试通过 pythonCOM 从 Outlook 中保存附件

标签 python com outlook save attachment

我正在查看我的 outlook 邮件以获取所有附件并将它们保存在我的计算机中。

这是我的代码:

import win32com.client
import os, sys

class OutlookLib:

    def __init__(self, settings={}):
        self.settings = settings

    # Gets all messages in outlook   
    def get_messages(self):      
        outlook = win32com.client.Dispatch("Outlook.Application")

        # This allows us to access the "folder" hierarchy accessible within
        # Outlook. You can see this hierarchy yourself by opening Outlook
        # manually and bringing up the folder menu
        # (which typically says "Inbox" or "Outlook Today" or something).
        ns = outlook.GetNamespace("MAPI")

        all_inbox = ns.GetDefaultFolder(6).Items
        return all_inbox

    def get_body(self, msg):
        return msg.Body

    def get_subject(self, msg):
        return msg.Subject

    def get_sender(self, msg):
        return msg.SenderName

    def get_recipient(self, msg):
        return msg.To

    def get_attachments(self, msg):
        return msg.Attachments

# Gets an attachment
# Return true if clean
# Otherwise, return false
def checkAttach(fileAtt):
    pass # TODO something here

def Main():
    global attach

    outlook = OutlookLib()
    messages = outlook.get_messages()

    # Loop all messages
    msg = messages.GetFirst()
    while msg:
        #print msg.Subject
        if not len(msg.Attachments) is 0:
            attach.append((msg.Attachments, msg.Subject))
        msg = messages.GetNext()

    for attachTuple in attach:
        print "Checking attachments under " + attachTuple[1]
        for fileAtt in attachTuple[0]:
            fileAtt.SaveAsFile(r"C:\Users\Lidor\Desktop\Dina\scan-mail")
            if checkAttach(fileAtt):
                print fileAtt.FileName + " was found as malicous."

attach = []

if __name__ == "__main__":
    Main()

现在这是我得到的错误:

Traceback (most recent call last):
  File "C:\Users\Lidor\Desktop\Dina\scan-mail\mailScanner.py", line 67, in <module>
    Main()
  File "C:\Users\Lidor\Desktop\Dina\scan-mail\mailScanner.py", line 60, in Main
    fileAtt.SaveAsFile(r"C:\Users\Lidor\Desktop")
  File "<COMObject <unknown>>", line 2, in SaveAsFile
com_error: (-2147352567, 'Exception occurred.', (4096, u'Microsoft Outlook', u"Cannot save the attachment. You don't have appropriate permission to perform this operation.", None, 0, -2147024891), None)

这很奇怪,因为我是管理员并且我拥有 pst 和输出文件夹的权限。 我也尝试在管理员 CMD 中运行它,但仍然出现相同的错误。

顺便说一句,如果您知道一种将附件放入 python 文件对象而不是将其保存到我的计算机的方法,那就更好了。

谢谢。

最佳答案

您必须指定完整的附件文件名,包括名称部分。 你只是指定了目录名,你需要连接目录和文件名:

fileAtt.SaveAsFile(r"C:\Users\Lidor\Desktop\Dina\scan-mail\" + fileAtt.FileName)

关于python - 尝试通过 pythonCOM 从 Outlook 中保存附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23638963/

相关文章:

asp-classic - 经典 ASP 的奇怪 COM 问题

c# - Foreach 因编码 COM 接口(interface)而失败

c++ - 在邮件通知区域而不是收件箱中显示 Outlook 图标

android - 在 Android 中发送带附件的电子邮件。适用于 Gmail 但不适用于 Outlook

python - 有没有办法在 python selenium 中使用 send_keys 更改文本的颜色?

python - 使用多个 CPU 进行 Scikit-learn 机器学习模型训练

c# - 收到错误 "The ' VFPOLEDB。 1' provider is not registered on the local machine"即使在安装和注册提供商之后

outlook - 如何监听所有 "ItemAdd"文件夹的 "Sent items"?

python - 如何过滤我想在 Python 类中删除哪些属性?

python - 如何按最佳匹配(difflib 比率)对字符串列表进行排序