我正在使用 Microsoft.Office.Interop.Outlook 来提取电子邮件附件:
var MAPI = new Application().GetNamespace("MAPI");
var ExampleFolder = MAPI.GetDefaultFolder(OlDefaultFolders.olFolderSentMail)
foreach (dynamic i in ExampleFolder.Items)
if (i.Attachments.Count > 0)
; // DoSomething();
不幸的是,非常慢。
答案 0 :(得分:3)
当然,您可以使用Items.Sort对集合进行排序。
您还可以使用Items.Find / FindNext或Items.Restrict来查找仅包含附件的项目。您需要的属性是PR_HASATTACH(DASL名称http://schemas.microsoft.com/mapi/proptag/0x0E1B000B
)
答案 1 :(得分:0)
@Kiquenet(我不能在您的评论下方添加评论),这是从Items.Restrict
获取带有附件的项目的代码:
//fanti's code
var MAPI = new Application().GetNamespace("MAPI");
var ExampleFolder = MAPI.GetDefaultFolder(OlDefaultFolders.olFolderSentMail)
Urn
方式(已测试,确定->来源https://social.msdn.microsoft.com/Forums/windowsapps/en-US/b6fef244-756c-4ab0-a22b-78137cfb4349/datereceived-filter-nor-happeinig?forum=outlookdev):
var itemsWithAttachment = ExampleFolder.Items.Restrict("@SQL= urn:schemas:httpmail:hasattachment = True");
DASL
方式(经过测试, ko ->“应该工作”来源https://docs.microsoft.com/en-us/office/client-developer/outlook/pia/how-to-filter-and-efficiently-enumerate-items-in-a-folder):
const string PR_HAS_ATTACH = "https://schemas.microsoft.com/mapi/proptag/0x0E1B000B";
var itemsWithAttachment = ExampleFolder.Items.Restrict("@SQL=\"" + PR_HAS_ATTACH + "\" = 1");
要按日期过滤,只需添加“ AND”或“ OR”即可( Urn ):
var itemsWithAttachmentAndDate = ExampleFolder.Items.Restrict("@SQL= urn:schemas:httpmail:hasattachment = True"
+ " AND urn:schemas:httpmail:datereceived <= '" + DateTime.Now.AddMonths(-3) + "'");
仅循环显示最后n个项目:
int n = 3;
for (int i = itemsWithAttachmentAndDate.Count - 1; i > n; i--)
{
//current item: itemsWithAttachmentAndDate[i] //Beware: "dynamic" typed!
; //DoSomething();
}