在处理 C# Outlook 之前按接收时间对电子邮件进行排序

发布于 2024-12-03 12:26:42 字数 552 浏览 1 评论 0原文

在处理电子邮件并将数据输入数据库时​​,我需要先按接收时间对电子邮件进行排序。

我需要它,以便将收到的最新电子邮件放入数据库中以覆盖旧版本(如果有旧版本)。

Microsoft.Office.Interop.Outlook.Items 项目 = (Outlook.Items)source.Items;

源是包含我想要排序的电子邮件的文件夹

我已经尝试了以下四种方法:

            items.Sort("ReceivedTime", false);
            items.Sort("[ReceivedTime]", Outlook.OlSortOrder.olAscending);
            items.Sort("ReceivedTime", Outlook.OlSortOrder.olSortNone);
            items.Sort("[ReceivedTime]");

这似乎没有对它进行排序,因为它仍然将最旧的邮件放入第二个数据库中,覆盖最新的提交。

有什么想法吗?

I need to sort my emails by Received time before processing them as I am processing emails and entering data from it into a database.

I need it so the newest email to be received gets put into the database to overwrite the older version (If there is an older version).

Microsoft.Office.Interop.Outlook.Items item = (Outlook.Items)source.Items;

Source is the folder with the emails in it that I wanted sorted

I have tried these four ways:

            items.Sort("ReceivedTime", false);
            items.Sort("[ReceivedTime]", Outlook.OlSortOrder.olAscending);
            items.Sort("ReceivedTime", Outlook.OlSortOrder.olSortNone);
            items.Sort("[ReceivedTime]");

Which does not seem to sort it as It still puts the oldest into the database second, overwriting the newest submission.

Any Ideas?

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

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

发布评论

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

评论(4

同尘 2024-12-10 12:26:43

如果你希望它们降序,它应该是

items.Sort("[ReceivedTime]", false);

true

it should be

items.Sort("[ReceivedTime]", false);

or true if you want them descending

喜你已久 2024-12-10 12:26:43

我花了很多时间试图解决同样的问题。

Microsoft Interop.outlook 中似乎存在某种错误,如果您直接尝试从文件夹中排序,它根本不起作用。

Microsoft.Office.Interop.Outlook.Application 应用程序 = new Microsoft.Office.Interop.Outlook.Application(); Microsoft.Office.Interop.Outlook._NameSpace ns = app.GetNamespace("MAPI"); Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null; inboxFolder = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);

        inboxFolder.Items.Sort("[ReceivedTime]", false);
        foreach (var item in inboxFolder.Items)
        {
            // ITEMS ARE NOT SORTED
        }

为了使其工作,您必须将它们复制到不同的列表中,然后进行排序。下面的例子将会起作用。

Outlook.Application app = new Outlook.Application();
Outlook.NameSpace outlookNs = app.GetNamespace("MAPI");
Outlook.MAPIFolder emailFolder = outlookNs.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);

Outlook.Items myItems = emailFolder.Items;

myItems.Sort("[ReceivedTime]", false);

foreach (var item in myItems)
{
var itemObj = item as MailItem;
if (itemObj != null)
{
// This time it will work
}
}

I spent so much time trying to figure out the same problem.

It appears there is some sort of bug in Microsoft Interop.outlook that if you directly try to sort from folder it does not work at all.

Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application(); Microsoft.Office.Interop.Outlook._NameSpace ns = app.GetNamespace("MAPI"); Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null; inboxFolder = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);

        inboxFolder.Items.Sort("[ReceivedTime]", false);
        foreach (var item in inboxFolder.Items)
        {
            // ITEMS ARE NOT SORTED
        }

To make it work you must copy them in different list and then sort. The example below will work.

Outlook.Application app = new Outlook.Application();
Outlook.NameSpace outlookNs = app.GetNamespace("MAPI");
Outlook.MAPIFolder emailFolder = outlookNs.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);

Outlook.Items myItems = emailFolder.Items;

myItems.Sort("[ReceivedTime]", false);

foreach (var item in myItems)
{
var itemObj = item as MailItem;
if (itemObj != null)
{
// This time it will work
}
}

命硬 2024-12-10 12:26:43

现在我不知道你的项目对象是什么类,但也许“排序”方法没有返回类型“void”,但它本身返回一个新列表。

因此,您应该像这样分配您的列表:

items = items.Sort();

然后您可以尝试四种方法中的哪一种适合您的需求。
我希望这有帮助!

Now I don't know what class your item-Object is, but maybe the "Sort"-Method does not have return type "void", but it returns a new list itself.

So you should assign your list like so:

items = items.Sort();

You could then try, which of your four approaches fits your needs.
I hope this helps!

野味少女 2024-12-10 12:26:43
Sub SortByDueDate() 
 Dim myNameSpace As Outlook.NameSpace 
 Dim myFolder As Outlook.Folder 
 Dim myItem As Outlook.TaskItem 
 Dim myItems As Outlook.Items 

 Set myNameSpace = Application.GetNamespace("MAPI") 
 Set myFolder = myNameSpace.GetDefaultFolder(olFolderTasks) 
 Set myItems = myFolder.Items 
 myItems.Sort "[DueDate]", False 
 For Each myItem In myItems 
 MsgBox myItem.Subject &; "-- " &; myItem.DueDate 
 Next myItem 
End Sub

这段代码来自MSDN。我很困惑为什么一开始它就设置了myItmes=myFolder.Items。经过多次尝试,我知道这是一个陷阱。如果直接使用myFolder.Items.sort...,排序功能不起作用。

Sub SortByDueDate() 
 Dim myNameSpace As Outlook.NameSpace 
 Dim myFolder As Outlook.Folder 
 Dim myItem As Outlook.TaskItem 
 Dim myItems As Outlook.Items 

 Set myNameSpace = Application.GetNamespace("MAPI") 
 Set myFolder = myNameSpace.GetDefaultFolder(olFolderTasks) 
 Set myItems = myFolder.Items 
 myItems.Sort "[DueDate]", False 
 For Each myItem In myItems 
 MsgBox myItem.Subject &; "-- " &; myItem.DueDate 
 Next myItem 
End Sub

This code is come from MSDN. I am confused as to why at beginning it sets myItmes=myFolder.Items. After trying many times, I know this is a trap. If you use myFolder.Items.sort... directly, the sort function does not work.

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