Sunday, October 26, 2008

Vb.NET sample for getting the number of Unread messages from an inbox using Impersonation with Exchange Web Services

Although im not a great user of VB any more there does seem to be a bit of a dirth of samples for those people who haven't made the leap to using C#. So here's something that might fill the void for a few people I've put a donwload of this code here the code itself looks like

Imports ewsvbsamp.ews
Imports System.Net
Imports System.Net.Security
Imports System.Security.Cryptography.X509Certificates
Module Module1

Sub Main()
End Sub
Public Function GetUnreadEmailCount(ByVal emailaddress As String) As Integer
Dim UnreadCount As Integer = 0
ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(AddressOf ValidateCertificate)
Dim esb As New ExchangeServiceBinding
esb.RequestServerVersionValue = New RequestServerVersion
esb.RequestServerVersionValue.Version = ExchangeVersionType.Exchange2007_SP1
esb.Credentials = New NetworkCredential("user", "password", "domain")
esb.Url = "https://servername/EWS/Exchange.asmx"

Dim csConSid As New ConnectingSIDType
csConSid.PrimarySmtpAddress = emailaddress
Dim exImpersonate As New ExchangeImpersonationType
exImpersonate.ConnectingSID = csConSid
esb.ExchangeImpersonation = exImpersonate

Dim biArray(1) As BaseFolderIdType
Dim dfFolder As New DistinguishedFolderIdType
dfFolder.Id = DistinguishedFolderIdNameType.inbox
biArray(0) = dfFolder
Dim geGetFolder As New GetFolderType
geGetFolder.FolderIds = biArray
geGetFolder.FolderShape = New FolderResponseShapeType
geGetFolder.FolderShape.BaseShape = DefaultShapeNamesType.AllProperties
Dim gfResponse As GetFolderResponseType = esb.GetFolder(geGetFolder)
Dim rmta As ResponseMessageType() = gfResponse.ResponseMessages.Items
Dim rmt As FolderInfoResponseMessageType = DirectCast(rmta(0), FolderInfoResponseMessageType)
If rmt.ResponseClass = ResponseClassType.Success Then
Dim folder As FolderType = DirectCast(rmt.Folders(0), FolderType)
UnreadCount = folder.UnreadCount
End If
Return UnreadCount
End Function

Private Function ValidateCertificate(ByVal sender As Object, ByVal certificate As X509Certificate, ByVal chain As X509Chain, ByVal sslPolicyErrors As SslPolicyErrors) As Boolean
'Return True to force the certificate to be accepted.
Return True
End Function
End Module


chrisli said...

i dont have a "wesvbsamp.ews" to Import. Where can i get it?

Thank you.


Glen said...

That's just my referance to EWS to use this code you need to add a referance to EWS on your exchange server. Eg Add Web Referance or if your using 2008 add Service referance and the click advanced and you will see add web referance in the compadability section. Then just import that namespace relative to whatever you called your project.


Dain said...

All I get is:
A first chance exception of type 'System.Web.Services.Protocols.SoapException' occurred in System.Web.Services.dll

Glen said...

Thats really not enough information for me to tell you whats going wrong you need to look more deply at the error message and exception to extract more detail. My thought is you haven't set the CASURL eg have you changed the lines

esb.Credentials = New NetworkCredential("user", "password", "domain")
esb.Url = "https://servername/EWS/Exchange.asmx"

to match your servername and logon details ?


Ravi said...

hi all,

i am facing a problem with Exchange web service.

I am trying to create a message with large size. I am using binding.createItem() of web method.
small messages are creating perfectly. but when i tried to create large size messages morethan 60 mb mail,
Exchange web service is returning "302 object moved" error.

i checked the EWS application logs in exchange server, there it is telling ews is not responding.
why this is happening for large size messages? Also i increased httpruntime value in web.config file
ews in exchange server.

Can any one know solution please help...i need help urgently..

Thanks in advance,

Ravi Nalla

wake2 said...

Hi Glen,
A few hours in to my development for the very first time your post helped me a lot.

I had some issues as I am using Exchange 2010, below are the changes I had to make to get your code up and running.



esb.RequestServerVersionValue.Version = ExchangeVersionType.Exchange2010

replaces reference to "Exchange2007_SP1"

csConSid.ItemElementName = ItemChoiceType.PrimarySmtpAddress
csConSid.Item() = emailaddress

replaces your "csConSid.PrimarySmtpAddress = emailaddress"

Finally, I had to comment out the 3 lines relating to impersonation as i havent investigated how to do this yet...