如何使用 win32com.mapi 从 python 中的 Outlook 获取新电子邮件?

发布于 2025-01-16 05:51:14 字数 1069 浏览 3 评论 0原文

我想在 Outlook 收到新电子邮件时收到一条消息。 我尝试使用Python。

我可以找到该文件夹​​,但找不到获取新电子邮件数量的方法。

import win32com.client

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
accounts = win32com.client.Dispatch("Outlook.Application").Session.Accounts;
inbox = outlook.Folders(accounts[0].DeliveryStore.DisplayName)
for obj in inbox.Folders: #how to know how many new email in this dir?
    try:
        if hasattr(obj, "__str__"):
            dirName =  obj.__str__() #as some new email in this obj.

        for message in obj.items: # how to know this email is not be read ?
            subject = sender = ''
            if hasattr(message, "Subject"):
                subject = message.Subject
            if hasattr(message, "SenderName"):
                sender = message.SenderName 
            print(sender, subject)
    except Exception as e:
        print(f"i:{obj.__str__()}")

我在哪里可以学习 win32com.mapi? 我想知道我可以在mapi中使用什么功能。

请给我一些信息~ 非常感谢!

I wanna get a message when a new email into outlook.
I try to use python.

I can find that folders,but can't find the way to get the number of new email.

import win32com.client

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
accounts = win32com.client.Dispatch("Outlook.Application").Session.Accounts;
inbox = outlook.Folders(accounts[0].DeliveryStore.DisplayName)
for obj in inbox.Folders: #how to know how many new email in this dir?
    try:
        if hasattr(obj, "__str__"):
            dirName =  obj.__str__() #as some new email in this obj.

        for message in obj.items: # how to know this email is not be read ?
            subject = sender = ''
            if hasattr(message, "Subject"):
                subject = message.Subject
            if hasattr(message, "SenderName"):
                sender = message.SenderName 
            print(sender, subject)
    except Exception as e:
        print(f"i:{obj.__str__()}")

and where can I learn win32com.mapi?
I wanna know what func can I use in mapi.

give me some info,please~
so much thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我是有多爱你 2025-01-23 05:51:14

您在寻找未读消息吗?使用 MAPIFolder.Items.Restrict("[Unread] = true") 检索应用了限制的 Items 集合。

Are you looking for unread messages? Use MAPIFolder.Items.Restrict("[Unread] = true") to retrieve Items collection with the restriction applied.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文