如何使用win32com Outlook在Python中刷新电子邮件?

如何使用win32com Outlook在Python中刷新电子邮件?,python,outlook-restapi,Python,Outlook Restapi,我有一个程序可以显示我最近从Outlook发送的电子邮件。如果在程序运行时收到新电子邮件,则不会刷新并显示新电子邮件。有没有关于我如何做到这一点的建议 我的代码没有更新到最新的电子邮件: import win32com.client import os import threading # use the Timer outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI") inbox =

我有一个程序可以显示我最近从Outlook发送的电子邮件。如果在程序运行时收到新电子邮件,则不会刷新并显示新电子邮件。有没有关于我如何做到这一点的建议

我的代码没有更新到最新的电子邮件:

import win32com.client
import os 
import threading # use the Timer 

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

def timer():


    print (body_content) 

    threading.Timer(30, timer).start()

timer()

我发现COM库没有在新线程中初始化。所以我不得不把它添加到函数中

    import pythoncom           # These 2 lines are here because COM library
    pythoncom.CoInitialize()   # is not initialized in the new thread
这将是正确的代码:

def timer():

    import pythoncom           # These 2 lines are here because COM library
    pythoncom.CoInitialize()   # is not initialized in the new thread

    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

    os.system('cls' if os.name == 'nt' else 'clear')
    print (body_content) # orginally used body_content.encode("utf-8") to fixed character encoding issue
                         # caused by Windows CMD.  But then figured out you can type 'chcp 65001' in cmd
    threading.Timer(30, timer).start() # refresh rate goes her

我发现COM库没有在新线程中初始化。所以我不得不把它添加到函数中

    import pythoncom           # These 2 lines are here because COM library
    pythoncom.CoInitialize()   # is not initialized in the new thread
这将是正确的代码:

def timer():

    import pythoncom           # These 2 lines are here because COM library
    pythoncom.CoInitialize()   # is not initialized in the new thread

    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

    os.system('cls' if os.name == 'nt' else 'clear')
    print (body_content) # orginally used body_content.encode("utf-8") to fixed character encoding issue
                         # caused by Windows CMD.  But then figured out you can type 'chcp 65001' in cmd
    threading.Timer(30, timer).start() # refresh rate goes her