首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >试图通过pythonCOM从outlook中保存附件

试图通过pythonCOM从outlook中保存附件
EN

Stack Overflow用户
提问于 2014-05-13 18:31:02
回答 1查看 9.5K关注 0票数 2

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

这是我的密码:

代码语言:javascript
复制
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()

这就是我遇到的错误:

代码语言:javascript
复制
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和output文件夹有权限。我也尝试过在管理CMD中运行它,但仍然得到了相同的错误。

顺便说一句,如果您知道如何将附件保存到python文件对象中,而不是将其保存到我的计算机中,则会更好。

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-13 18:39:03

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

代码语言:javascript
复制
fileAtt.SaveAsFile(r"C:\Users\Lidor\Desktop\Dina\scan-mail\" + fileAtt.FileName)
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23638963

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档