Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过MAPI使用Python从Outlook读取电子邮件_Python_Outlook_Exchange Server_Mapi_Cdo.message - Fatal编程技术网

通过MAPI使用Python从Outlook读取电子邮件

通过MAPI使用Python从Outlook读取电子邮件,python,outlook,exchange-server,mapi,cdo.message,Python,Outlook,Exchange Server,Mapi,Cdo.message,我正在尝试编写一个简短的程序,它将读取exchange/Outlook配置文件中某个文件夹中的电子邮件内容,以便我可以处理数据。但是,我在查找有关python和exchange/Outlook集成的大量信息时遇到了问题。很多东西要么非常陈旧/没有文档/没有解释。我尝试了几个代码片段,但似乎都出现了相同的错误。我试过Tim Golden的密码: import win32com.client session = win32com.client.gencache.EnsureDispatch ("M

我正在尝试编写一个简短的程序,它将读取exchange/Outlook配置文件中某个文件夹中的电子邮件内容,以便我可以处理数据。但是,我在查找有关python和exchange/Outlook集成的大量信息时遇到了问题。很多东西要么非常陈旧/没有文档/没有解释。我尝试了几个代码片段,但似乎都出现了相同的错误。我试过Tim Golden的密码:

import win32com.client

session = win32com.client.gencache.EnsureDispatch ("MAPI.Session")

#
# Leave blank to be prompted for a session, or use
# your own profile name if not "Outlook". It is also
# possible to pull the default profile from the registry.
#
session.Logon ("Outlook")
messages = session.Inbox.Messages

#
# Although the inbox_messages collection can be accessed
# via getitem-style calls (inbox_messages[1] etc.) this
# is the recommended approach from Microsoft since the
# Inbox can mutate while you're iterating.
#
message = messages.GetFirst ()
while message:
    print message.Subject
    message = messages.GetNext ()
但是,我得到一个错误:

pywintypes.com_error: (-2147221005, 'Invalid class string', None, None)
不确定我的个人资料名称,因此我尝试了:

session.Logon()

提示,但也不起作用(相同错误)。我也尝试过打开和关闭Outlook,但都没有改变任何东西。

我遇到了与您相同的问题-没有发现多少有效的方法。然而,下面的代码就像一个符咒

import win32com.client

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")

inbox = outlook.GetDefaultFolder(6) # "6" refers to the index of a folder - in this case,
                                    # the inbox. You can change that number to reference
                                    # any other folder
messages = inbox.Items
message = messages.GetLast()
body_content = message.body
print body_content

我已经创建了自己的迭代器,通过python对Outlook对象进行迭代。问题是python试图从索引[0]开始迭代,但outlook希望第一个项索引[1]。。。为了使Ruby更简单,下面有一个帮助器类 方法:

.items()-生成一个元组(索引,项)

.prop()-帮助内省outlook对象,公开可用属性(方法和属性)


我也有同样的问题。结合来自互联网(及以上)的各种方法,提出以下方法(checkEmails.py)

为了简洁起见,我还包括FileWriter类的代码(在FileWrapper.py中找到)。我需要这个是因为 试图将UTF8管道传输到windows中的文件无效

class FileWriter(object):
    '''
    convenient file wrapper for writing to files
    '''


    def __init__(self, filename):
        '''
        Constructor
        '''
        self.file = open(filename, "w")

    def pl(self, a_string):
        str_uni = a_string.encode('utf-8')
        self.file.write(str_uni)
        self.file.write("\n")

    def flush(self):
        self.file.flush()
对不起,我的英语不好。 使用Python和MAPI检查邮件更容易

outlook =win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
folder = outlook.Folders[5]
Subfldr = folder.Folders[5]
messages_REACH = Subfldr.Items
message = messages_REACH.GetFirst()
在这里,我们可以将最多的第一封邮件放入邮箱或任何子文件夹。实际上,我们需要检查邮箱号码和方向。借助此分析,我们可以检查每个邮箱及其子邮箱文件夹

同样,请找到下面的代码,我们可以看到,最后/早期的邮件。我们需要如何检查

`outlook =win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
folder = outlook.Folders[5]
Subfldr = folder.Folders[5]
messages_REACH = Subfldr.Items
message = messages_REACH.GetLast()`
有了它,我们可以将最新的电子邮件发送到邮箱中。
根据上述代码,我们可以检查所有邮箱及其子文件夹。

您是否考虑过对服务器使用IMAP而不是依赖Outlook客户端?根据您的使用情况,IMAP可能被证明是可行的,并且更具可移植性(客户端和服务器)。@Jason IMAP看起来不错,但不幸的是,我使用的帐户上没有启用。是否有方法查看其他消息属性?我想使用您的示例来获取收到邮件的日期和时间。找到解决方案:
message.CreationTime
由于某种原因,当我使用dir()@sequoia时,属性不会出现-使用Microsoft的邮件项目COM属性列表(例如Outlook邮件)是我找到邮件所有属性的方式(例如message.SenderEmailAddress):GetDefaultFolder参数可在“6”处找到,指的是收件箱文件夹。其他文件夹如何?如果创建新文件夹,如何知道它得到的序列号?它们是否按特定方式排序?如从最近到最旧?
outlook =win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
folder = outlook.Folders[5]
Subfldr = folder.Folders[5]
messages_REACH = Subfldr.Items
message = messages_REACH.GetFirst()
`outlook =win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
folder = outlook.Folders[5]
Subfldr = folder.Folders[5]
messages_REACH = Subfldr.Items
message = messages_REACH.GetLast()`