简体   繁体   English

如何获取Outlook的最新消息

[英]How to get latest messages from Outlook

I'm writing a work management application in C# for my team at work (because our stupid employer didn't give us any appropriate tools).我在 C# 为我的工作团队编写一个工作管理应用程序(因为我们愚蠢的雇主没有给我们任何合适的工具)。 I need to check for new messages in Outlook every once in a while and I need to get messages since the last time I checked them.我需要每隔一段时间检查 Outlook 中的新消息,并且我需要获取自上次检查以来的消息。

I've tried the Items.Restrict() method and used the filter "[ReceivedTime] > ' "+lastUpdate+" ' " , but it doesn't seem to work correctly.我已经尝试了Items.Restrict()方法并使用了过滤器"[ReceivedTime] > ' "+lastUpdate+" ' " ,但它似乎无法正常工作。 I've probably tried every possible format of lastUpdate - converted to universal time, to string, to US date/time format.我可能已经尝试了所有可能的lastUpdate格式——转换为通用时间、字符串、美国日期/时间格式。 NOTHING works correctly. NOTHING 工作正常。 It either gives me messages in absolutely different time range or doesn't find any msgs at all.它要么在完全不同的时间范围内给我消息,要么根本找不到任何消息。

Any idea what I'm doing wrong?知道我做错了什么吗? I'm also thinking about using AdvancedSearch() but will it be easy and quick to implement?我也在考虑使用AdvancedSearch() ,但实施起来是否简单快捷? Thanks in advance for your replies!提前感谢您的回复!

OK, I've wasted some more time on this and found a simplier solution which doesn't force me to rewrite my whole application from scratch.好的,我在这上面浪费了更多时间,并找到了一个更简单的解决方案,它不会强迫我从头开始重写整个应用程序。

Instead of using the Items.Restrict() method to get the latest messages I'm using Items.Sort() to sort them by ReceivedTime and then loop through the first messages from the Items list until I find a message which is older than my last update timestamp.我没有使用Items.Restrict()方法来获取最新消息,而是使用Items.Sort()按 ReceivedTime 对它们进行排序,然后循环遍历 Items 列表中的第一条消息,直到找到比我的旧的消息上次更新时间戳。

Example:例子:

givenFolder.Items.Sort("[ReceivedTime]", true); //true means, it will be descending (latest at the beginning)

This solution, unfortunately, has a drawback because Items.Sort() seems to work only in Exchange mailbox (doesn't sort properly in ordinary POP mailbox) but I need it only for Exchange anyway, so it's enough for me.不幸的是,这个解决方案有一个缺点,因为Items.Sort()似乎只能在 Exchange 邮箱中工作(在普通 POP 邮箱中不能正确排序),但无论如何我只需要它用于 Exchange,所以对我来说已经足够了。

I think, it's also possible to use Outlook's AdvancedSearch or MAPI tables but I haven't tried that.我认为,也可以使用 Outlook 的 AdvancedSearch 或 MAPI 表,但我还没有尝试过。

While not a direct answer, take a look at Exchange Web Services Managed API 1.2 SDK http://msdn.microsoft.com/en-us/library/dd633710(v=EXCHG.80).aspx .虽然不是直接的答案,但请查看Exchange Web Services Managed API 1.2 SDK http://msdn.microsoft.com/en-us/library/dd633710(v=EXCHG.80).aspx It is much easier to deal with, is so much more flexible, and with no dependency on Outlook.它更容易处理,更灵活,并且不依赖于 Outlook。

Working with search by using the EWS Managed API http://msdn.microsoft.com/en-us/library/dd633671(v=exchg.80).aspx使用 EWS 托管 API 处理搜索http://msdn.microsoft.com/en-us/library/dd633671(v=exchg.80).aspx

Working with search filters by using the EWS Managed API http://msdn.microsoft.com/en-us/library/dd633659(v=exchg.80).aspx通过使用 EWS 托管 API http://msdn.microsoft.com/en-us/library/dd633659(v=exchg.80).aspx使用搜索过滤器

Look for a MAPI Viewer.寻找 MAPI 查看器。 Microsoft provides one and there are others.微软提供了一个,还有其他的。 With the viewer, you can look at the folder and item properties to see how you need to filter.使用查看器,您可以查看文件夹和项目属性以了解您需要如何过滤。

Yet, I think the examples in Working with search filters by using the EWS Managed API will get you going.但是,我认为使用 EWS 托管 API 处理搜索过滤器中的示例会让您有所了解。

For those who come across this question, a similar one was asked and answered here: Restrict Outlook Items by Date对于那些遇到这个问题的人,在这里提出并回答了一个类似的问题: 按日期限制 Outlook 项目

This is the documentation page: https://docs.microsoft.com/en-us/office/vba/api/Outlook.Items.Restrict这是文档页面: https : //docs.microsoft.com/en-us/office/vba/api/Outlook.Items.Restrict

The date has to be formatted like so: M/d/yy h:mm tt (in .NET date format string) Example: May 6, 2007 at 8:09 AM becomes 5/6/07 8:09 AM Full filter string then becomes [ReceivedTime] >= '5/6/07 8:09 AM' (single quotes are important)日期的格式必须如下: M/d/yy h:mm tt (在 .NET 日期格式字符串中) 示例:2007 年 5 月 6 日上午 8:09 变为5/6/07 8:09 AM完整过滤器字符串然后变成[ReceivedTime] >= '5/6/07 8:09 AM' (单引号很重要)

An example how I fixed this using the documentation that also Mr. TA refers too:我如何使用 TA 先生也提到的文档修复此问题的示例:

            string filterDateString = lastUpdate.Day + "/" + lastUpdate.Month + "/" + lastUpdate.Year + " " + filterDate.ToShortTimeString();
            string filter = "[ReceivedTime] > '" + filterDateString + "'";

            Application outlookApplication = new Application();
            NameSpace outlookNamespace = outlookApplication.GetNamespace("MAPI");
            MAPIFolder inboxFolder = outlookNamespace.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
            Items mailItems = inboxFolder.Items.Restrict(filter);

EDIT: filterDate.ToShortTimeString() only works in the right way if your CultureInfo is ("en-us") otherwise you need to add AM or PM or do this:编辑: filterDate.ToShortTimeString() 只有在您的 CultureInfo 是(“en-us”)时才能以正确的方式工作,否则您需要添加 AM 或 PM 或执行此操作:

CultureInfo us = new CultureInfo("en-US");
Thread.CurrentThread.CurrentCulture = us;

Why not connecting directly to your mail server and issuing POP3 or IMAP commands through a simple connection ?为什么不直接连接到您的邮件服务器并通过简单的连接发出 POP3 或 IMAP 命令? I used to do that directly in telnet ages ago.很久以前,我曾经直接在 telnet 中这样做。 Commands are pretty simple and should work also under windows.命令非常简单,应该也可以在 Windows 下使用。 ( for ref see this ) 参考见这个

Another way may be opening directly the Outlook folder, but this will imply that a client version of Outlook is actually running and downloading messages from the server.另一种方法可能是直接打开 Outlook 文件夹,但这意味着 Outlook 的客户端版本实际上正在运行并从服务器下载邮件。

Btw, a program which tells you if you've got new mail already exists, is called biff (and I suspect it exists even on MS platform).顺便说一句,一个告诉你是否有新邮件已经存在的程序叫做 biff (我怀疑它甚至在 MS 平台上也存在)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM