Appframe Knowledge Base


1 hits
0

How to connect to exchange via webservice

'List of functions: http://www.independentsoft.de/exchangewebservices/tutorial/index.html

Public ReadOnly Property ExService As ExchangeService
        Get
            If gExService Is Nothing Then
                gExService = New ExchangeService(ExchangeVersion.Exchange2010_SP1)
                gExService.Timeout = 10000
                gExService.Url = New Uri("https://.../EWS/Exchange.asmx")
                gExService.Credentials = New WebCredentials("", "")
            End If

            Return gExService
        End Get
    End Property

    <Serializable()>
    Public Class InboxItem
        Public Property UniqueID As String
        Public Property ChangeKey As String
        Public Property Subject As String
        Public Property Body As String
        Public Property FromName As String
        Public Property Received As DateTime
        Public Property ReceivedString As String
        Public Property IsRead As Boolean
    End Class

    Public Function GetMessageList(pMaxRecords As Integer, pFolder As String) As List(Of InboxItem)
        Dim vItemsList As New List(Of InboxItem)
        Dim vFolder As WellKnownFolderName
        Dim vMessage As EmailMessage
        Dim vItemIndex As Integer = 0

        Select Case pFolder.ToUpper()
            Case "INBOX"
                vFolder = WellKnownFolderName.Inbox
            Case "SENT ITEMS"
                vFolder = WellKnownFolderName.SentItems
            Case Else
                Throw New Exception("Invalid Folder")
        End Select

        Dim vResults As FindItemsResults(Of Item) = ExService.FindItems(vFolder, New ItemView(pMaxRecords))

        If vResults.Items.Count = 0 Then
            Return vItemsList
        End If

        Dim vPropertySet As New PropertySet(BasePropertySet.IdOnly, ItemSchema.Subject, EmailMessageSchema.From, EmailMessageSchema.DateTimeCreated, EmailMessageSchema.IsRead)
        Dim vResponses As ServiceResponseCollection(Of ServiceResponse) = ExService.LoadPropertiesForItems(vResults, vPropertySet)

        For Each vResponse As ServiceResponse In vResponses
            If vResponse.Result = ServiceResult.Success AndAlso TypeOf vResults.Items(vItemIndex) Is EmailMessage Then
                vMessage = DirectCast(vResults.Items(vItemIndex), EmailMessage)

                vItemsList.Add(New InboxItem With {.UniqueID = vMessage.Id.UniqueId,
                                                   .ChangeKey = vMessage.Id.ChangeKey,
                                                   .Subject = vMessage.Subject,
                                                   .FromName = vMessage.From.Name,
                                                   .Received = vMessage.DateTimeCreated,
                                                   .ReceivedString = vMessage.DateTimeCreated.ToShortDateString(),
                                                   .IsRead = vMessage.IsRead})
            End If
            vItemIndex += 1
        Next
        Return vItemsList
    End Function

win · Perma link post comment Posted by: Peter Øren (26-mar-2012)