Independentsoft
- any library, any programming language
Home
Purchase
Support
Company
Contact
WebDAV .NET for Exchange
>
Tutorial
> Search for unread messages
The following example shows you how to search for unread messages in user's Inbox folder.
C# example
using System; using System.Net; 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.Read, Operator.Equals, false); 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; Console.WriteLine("Subject=" + property[1].Value); Console.WriteLine("Body=" + property[2].Value); Console.WriteLine("FromEmail=" + property[3].Value); Console.WriteLine("DateReceived=" + property[4].Value); //If you want to set message as read uncomment this part //Property readProperty = new Property(MessageProperty.Read.Name, MessageProperty.Read.Namespace, "1"); //resource.SetProperty(messageUrl, readProperty); } Console.Read(); } } }
VB example
Imports System Imports System.Net 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.Read, [Operator].Equals, False) 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 Console.WriteLine("Subject=" & properties(1).Value) Console.WriteLine("Body=" & properties(2).Value) Console.WriteLine("FromEmail=" & properties(3).Value) Console.WriteLine("DateReceived=" & properties(4).Value) 'If you want to set message as read uncomment this part 'Dim readProperty As Independentsoft.Webdav.Exchange.Property = New Independentsoft.Webdav.Exchange.Property(MessageProperty.Read.Name, MessageProperty.Read.Namespace, "1") 'resource.SetProperty(messageUrl, readProperty) Next Console.Read() End Sub End Module