Independentsoft
- any library, any programming language
Home
Purchase
Support
Company
Contact
WebDAV .NET for Exchange
>
Tutorial
> Search for messages received in last 10 minutes
The following example shows you how to search for messages received in last 10 minutes.
C# example
using System; using System.Net; using System.Web; using Independentsoft.Webdav.Exchange; using Independentsoft.Webdav.Exchange.ContentClass; using Independentsoft.Webdav.Exchange.Properties; using Independentsoft.Webdav.Exchange.Sql; namespace Sample { class Program { static void Main(string[] args) { NetworkCredential credential = new NetworkCredential("username", "password"); WebdavSession session = new WebdavSession(credential); session.UserMailbox = "https://myserver/exchange/emailaddress"; Resource resource = new Resource(session); Mailbox myMailbox = resource.Mailbox; PropertyName[] propertyName = new PropertyName[6]; propertyName[0] = MessageProperty.ContentClass; propertyName[1] = MessageProperty.Subject; propertyName[2] = MessageProperty.Body; propertyName[3] = MessageProperty.FromEmail; propertyName[4] = MessageProperty.DateReceived; propertyName[5] = MessageProperty.Read; Select select = new Select(propertyName); From from = new From(myMailbox.Inbox); Where where = new Where(); Condition condition1 = new Condition(MessageProperty.ContentClass, Operator.Equals, ContentClassType.Message); Condition condition2 = new Condition(MessageProperty.DateReceived, Operator.GreatThenOrEquals, DateTime.Now.AddMinutes(-10)); where.Add(condition1); where.Add(LogicalOperator.AND); where.Add(condition2); SqlQuery sqlQuery = new SqlQuery(select, from, where); MultiStatus multiStatus = resource.Search(sqlQuery); SearchResult searchResult = new SearchResult(multiStatus, propertyName); SearchResultRecord[] allRecords = searchResult.Record; for (int i = 0; i < allRecords.Length; i++) { //message URL string messageUrl = allRecords[i].Address; Console.WriteLine("messageUrl=" + messageUrl); //message's properties Property[] property = allRecords[i].Property; string subject = HttpUtility.HtmlDecode(property[1].Value); string body = HttpUtility.HtmlDecode(property[2].Value); string fromEmail = HttpUtility.HtmlDecode(property[3].Value); string dateReceived = property[4].Value; Console.WriteLine("Subject=" + subject); Console.WriteLine("Body=" + body); Console.WriteLine("FromEmail=" + fromEmail); Console.WriteLine("DateReceived=" + dateReceived); } Console.Read(); } } }
VB example
Imports System Imports System.Net Imports System.Web Imports Independentsoft.Webdav.Exchange Imports Independentsoft.Webdav.Exchange.ContentClass Imports Independentsoft.Webdav.Exchange.Properties Imports Independentsoft.Webdav.Exchange.Sql Module Module1 Sub Main(ByVal args() As String) Dim credential As NetworkCredential = New NetworkCredential("username", "password") Dim session As WebdavSession = New WebdavSession(credential) session.UserMailbox = "https://myserver/exchange/emailaddress" Dim resource As Resource = New Resource(session) Dim myMailbox As Mailbox = resource.Mailbox Dim propertyName() As PropertyName = New PropertyName(5) {} propertyName(0) = MessageProperty.ContentClass propertyName(1) = MessageProperty.Subject propertyName(2) = MessageProperty.Body propertyName(3) = MessageProperty.FromEmail propertyName(4) = MessageProperty.DateReceived propertyName(5) = MessageProperty.Read Dim selectStatement As Independentsoft.Webdav.Exchange.Sql.Select = New Independentsoft.Webdav.Exchange.Sql.Select(propertyName) Dim from As From = New From(myMailbox.Inbox) Dim where As Where = New Where Dim condition1 As Condition = New Condition(MessageProperty.ContentClass, [Operator].Equals, ContentClassType.Message) Dim condition2 As Condition = New Condition(MessageProperty.DateReceived, [Operator].GreatThenOrEquals, DateTime.Now.AddMinutes(-10)) where.Add(condition1) where.Add(LogicalOperator.AND) where.Add(condition2) Dim query As SqlQuery = New SqlQuery(selectStatement, from, where) Dim multiStatus As MultiStatus = resource.Search(query) Dim searchResult As SearchResult = New SearchResult(multiStatus, propertyName) Dim allRecords() As SearchResultRecord = searchResult.Record Dim i As Integer For i = 0 To allRecords.Length - 1 'message URL Dim messageUrl As String = allRecords(i).Address Console.WriteLine("messageUrl=" & messageUrl) 'message's properties Dim properties() As Independentsoft.Webdav.Exchange.Property = allRecords(i).Property Dim subject As String = HttpUtility.HtmlDecode(properties(1).Value) Dim body As String = HttpUtility.HtmlDecode(properties(2).Value) Dim fromEmail As String = HttpUtility.HtmlDecode(properties(3).Value) Dim dateReceived As String = properties(4).Value Console.WriteLine("Subject=" & subject) Console.WriteLine("Body=" & body) Console.WriteLine("FromEmail=" & fromEmail) Console.WriteLine("DateReceived=" & dateReceived) Next Console.Read() End Sub End Module