ASP.Net如何计算Exchange 2013上收件箱中未读电子邮件的数量?

时间:2014-01-28 19:08:33

标签: asp.net graph exchange-server exchangewebservices

我试图创建一个ASP.Net网站,该网站将趋势图并显示转到某个电子邮件地址的每日电子邮件总数。我不需要下载或处理邮件,只需每天在收件箱中返回总数量的电子邮件。

我看到的所有编程问题都是针对WinForms并使用Outlook来计算本地PST文件,ASP.Net使用交换机时没有那么多。

我找到了EWS XML元素> UnreadCount但很难使用该元素。 http://msdn.microsoft.com/en-us/library/office/aa580965(v=exchg.150).aspx

我开始玩的程序代码可以在桌面上运行,但是我不想把它当作桌面服务而浪费资源并放弃这个挑战。

' Create Application class and get namespace
Dim outlook As Outlook.Application = New Outlook.ApplicationClass()
Dim ns As Outlook.NameSpace = outlook.GetNamespace("Mapi")

Dim _missing As Object = Type.Missing
ns.Logon(_missing, _missing, False, True)

' Get Inbox information in objec of type MAPIFolder
Dim inbox As Outlook.MAPIFolder = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)

' Unread emails
Dim unread As Integer = inbox.UnReadItemCount

' Display the subject of emails in the Inbox folder
For Each mail As Outlook.MailItem In inbox.Items
    Console.WriteLine(Wmail.Subject)
Next mail

1 个答案:

答案 0 :(得分:2)

Outlook命名空间需要Outlook,请改用EWS Managed API

以下代码将收集今天收件箱中收到的电子邮件列表。

Using Service = New ExchangeService()
    Dim View As New ItemView(Integer.MaxValue)
    Dim sfc As New SearchFilter.SearchFilterCollection()

    ' Return only emails received from midnight this morning
    sfc.Add(New SearchFilter.IsGreaterThanOrEqualTo(EmailMessageSchema.DateTimeReceived, DateTime.Now.Day))

    ' Get the count 
    Dim totalCount = Service.FindItems(f.Id, sfc, View).TotalCount
End Using

您可能需要查看EWS Impersonation才能阅读每个邮箱。