Getting started with Exchange Web Service (EWS)
Firstly you might want to configure your security groups as per my article titled “Exchange Web Service (EWS) configuration” then return and continue.
EWS is a powerful way to access Microsoft Exchange over https. Doing this via .NET is a doddle.
I am going to assume you know how to add a web reference to your .net project.
your exchange server will be publishing EWS on the following URL
https://<DomainName|IPAddress|Servaername>/EWS/Services.wsdl
All you need to do is add a webreference to your .NET project by pointing to the above url. The important thing to remember is that you will actually be using a different URL when it comes to binding in code. The url shape will be like this
https://<DomainaName|IPAddress|Servaername>/EWS/exchange.asmx
Once you have your web reference in place, and in this example we will be calling the reference EWS, the first thing you are going to need to do is declare a new instance like so
1 |
Dim binding As EWS.ExchangeServiceBinding = GetNewEWSBinding() |
The GetNewEWSBinding() returns a binding. The binding is on one of the mailboxes within the saEWSImpersonatable security group mentioned in the article previously mentioned at the begining of this one.
The following version of the function has a default binding mailbox defined in a variable at the package level of the SSIS package this script task resides in; it also checks for an InTest switch to swap between two mailboxes when in test or production. Binding credentials can be configured manually for a specific user, this user must be a member of the security group sgEWSImpersonate as described in the article mentioned at the beginning of this one. Finally there is an override optional parameter to provide if you wish to specify a specific mailbox to bind to instead of the default.
You can amend this to your requirements
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
Function GetNewEWSBinding(Optional ByVal ImpersonateMailAddress As String = "") As ExchangeServiceBinding Dim binding As local.swisscantouk.exch01.ExchangeServiceBinding = _ New local.swisscantouk.exch01.ExchangeServiceBinding() With binding .Url = Dts.Variables("ExchangeEWS").Value 'specify binding credentials like so 'binding.Credentials = New Net.NetworkCredential("username", "password", "domain") 'or use the default credentials the code is executing under .UseDefaultCredentials = True .PreAuthenticate = True 'set the version on the request .RequestServerVersionValue = New RequestServerVersion With {.Version = ExchangeVersionType.Exchange2010_SP1} 'setup the default mailbox for this binding .ExchangeImpersonation = New ExchangeImpersonationType With {.ConnectingSID = New ConnectingSIDType With {.ItemElementName = ItemChoiceType.PrimarySmtpAddress}} 'connect to the package level mailbox defined in the variables of this package If Dts.Variables("InTest").Value Then .ExchangeImpersonation.ConnectingSID.Item = emailImpersonationTEST '"main SMTP email address of account someone@contoso.co.uk" Else .ExchangeImpersonation.ConnectingSID.Item = emailImpersonationLIVE '"main SMTP email address of account someone@contoso.co.uk" End If 'connect to a specified mailbox defined specifically in the program sequence 'overriding the default If Not String.IsNullOrEmpty(ImpersonateMailAddress) Then .ExchangeImpersonation.ConnectingSID.Item = ImpersonateMailAddress End With Return binding End Function |
As soon as you have your binding you are ready to start working with the mailbox.
In my next article on EWS, I will discuss how to declare your EWS binding objects using object initialisers, you will be able to declare and run requests against EWS using less code.
ie Dim x as object With {.parameter = value, .parameter = value}
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
'construct the request Dim reqFindFolder As FindFolderType = New FindFolderType 'setup the properties of the FindFolder request object With reqFindFolder .Traversal = FolderQueryTraversalType.Deep 'change to deep to include subfolders .ParentFolderIds = New DistinguishedFolderIdType() {New DistinguishedFolderIdType With {.Id = DistinguishedFolderIdNameType.msgfolderroot}} .FolderShape = New FolderResponseShapeType With {.BaseShape = DefaultShapeNamesType.IdOnly} .Restriction = New RestrictionType .Restriction.Item = New ContainsExpressionType With { _ .ContainmentMode = ContainmentModeType.FullString _ , .ContainmentModeSpecified = True _ , .ContainmentComparison = ContainmentComparisonType.Exact _ , .ContainmentComparisonSpecified = True _ , .Constant = New ConstantValueType With {.Value = FolderName} _ , .Item = New PathToUnindexedFieldType With {.FieldURI = UnindexedFieldURIType.folderDisplayName} _ } End With |