A long time ago, I wrote an article on How to get the Global Address List programatically. The theory behind that article is still valid, but it only features a VBScript example. Since someone from the Microsoft Exchange Forum had trouble converting it to C#, I fired up Visual Studio and hacked something together. The result is somewhat more complete than the VBScript example, because it allows access to all address lists, not just the default global address list (GAL).

I’ve moved to Office 365 for my private mails a while ago and. Up to now, the provider hosting this website was responsible for the DNS entries. But now I moved the entire DNS authority to Microsoft for my primary domain. This lead to the unfortunate incident that I was unable to send mails to GMX (one of the largest free mailer in Germany). Thanks to a tip from Daniel Melanchthon I was able to resolve the issue fairly quickly.

The problem seems to be that GMX checks for the existence of a DNA A record on the sender domains. My primary domain is henningkrause.eu. When I send an email to GMX, they check for an A record for henningkrause.eu. I’m not sure why they do that - I know of no RFC which makes such a record mandatory. The A record is normally only used as a fallback method in case no MX record can be found. The solution was to create such a DNS record using the Office 365 DNS management interface:

The entry in question is the one highlighted in red. The host name "@” specifies that the A record is to be created for the domain itself. The interesting thing is that it is not relevant to which IP address the A record points. It just needs to be there. If you experience the same issues and don’t have your own website, just point it to any IP address.

After I created the domain, I was able to send mails to GMX again almost immediately.

A while ago, Glen posted an article on his blog on how to set homepage of a folder using ADO and later he posted a version of that script which uses the EWS managed API to do this in this MSDN forum thread. However, he wrote it for the first version of the API, and the EWS Managed API 1.1 has a slightly different object model. Since someone on the MSDN forums had difficulties to update the script to work with the EWS Managed API 1.1, I thought I just post an updated version here:

Don’t do that! A query using this ItemView will put a severe burden on the Exchange server if the folder being queried contains many items. The pageSize parameter is there for a reason.

Instead of querying all items at once, one should do a proper paging: Query, say, 512 items and process them. Then, query the next 512 items. Thanks to iterators in C#, the process of retrieving the items can be completely separated away from the processing.

If you are not familiar with C# iterators, have a look at this article. In essence, the yield operator allows for deferred execution, something you might heard from with regards to LINQ.

Here is a helper method which uses this technique to iterate over the contents of an Exchange folder, requesting 512 items per batch and returning them to the caller. When used properly, this method uses very little resources on the Exchange server as well as on the client. Additionally, it allows for querying the body of an item. In this case, instead of executing a FindItems request, it executes a FindItems request and a LoadPropertiesForItems request per batch.

Due to the nature of the method, you should not put the items returned from the methods in a List, as this would consume a lot of memory on the client. Instead process the items one by one.

As you can see, this method uses several .NET 4.0 features: Optional parameters and extension methods. So, if you are still using .NET 2.0, you’ll need to modify the code a little bit.

To use these methods, insert this code into your solution. In the class where you want to use the methods, add a using to the namespace containing the ExchangeServiceExtension. Both methods will then appear in the code completion window on instances of the ExchangeService class:

In the following example, the method is used to iterate over all items in the Inbox folder and printing the items to the console:

Every now and then people in the Exchange development forum asks whether the ItemId returned by a FindItems call is unique or not, because they allegedly got duplicate ids for one folder. The short answer to this question is: Yes, ItemIds are unique. And you are seeing duplicate ids because you are probably performing an incase-sensitive comparison.

Long answer:

In the following example, I executed a FindItems call on one of my mailbox folders and got multiple of such “duplicates”. One example:

These ids differ only in the 5th char from the right. The first one has a capital ‘A’ and the other has a small ‘a’ at that position. This can happen because the item id is base64 encoded. Base64 is way to represent binary data as ASCII text. A numerical value of 5 is encoded as a capital “F” and a value of 31 is also represented by an “f” - albeit a lower-case one.

If you store an item id in a database (SQL Server for example) you’ll most likely have run into the problem that a select statement with a WHERE clause on the item id returns multiple hits. This is due to the fact that the SQL Server by default uses a incase-sensitive collation for columns. To change this, open the table in question with the designer, select the column containing the item id. The designer will then display the column properties, like in this example:

Click on the Collation row and then on the button with the three full-stops. This will open the collation selection dialog:

Ensure that the checkbox labeled “Case Sensitive” is checked. Click ok and then save the table.

Note: If you do this via the SQL Server Management Studio, it will recreate the entire table (including data, indexes and constraints). Depending on the size of the table, this can take quite some time.

Todays post is a small detour from the regular Exchange related posts. The other day I was troubleshooting an issue with a program. It did not behave as expected in production and so I fired up WinDbg to inspect the value of some objects. I attached WindDbg to the process in question and load psscor4, a replacement from Microsoft for the standard SOS extension (Note that if you want to use psscor, you need the right version: Psscor2 is for .NET 2.0-3.5 software. Psscor4 is only for .NET 4 programs). The WinDbg results I present in this article are not from the process I actually examined. I hacked together a small sample program instead.

The sample class creates instances of a class named Person. A Person class has two properties: A name (string) and an age (int). The object instance I’m looking for is named Mallory. The goal is to get a look at the object instance with a call to !DumpObj. So all I need is the memory address of the Person instance named Alice. This should be easy.

So here we go. First, load the SOS extension. Since I ‘m examining an x64 process, I ‘m using the X64 version of psscor.

.load C:\temp\Psscor4\amd64\amd64\psscor4.dll

The first thing to do is to get an overview about the target class. How many instances of the Person class are currently lying around? A call to Dumpheap reveals this:

The interesting lines here are the last two. They describe the details and the values of the properties. Since I used auto-properties instead of traditional ones they are named <Name>k__BackingField instead something like _Name. Since the name if of type System.String, the value is a reference. So, the next step is to issue a !DumpObj on the value of that field:

The nice thing about !DumpObj is that it prints string values directly to the console. In this case, the value is Bob. Ok, so this is not the instance I’m searching for. Hmm, just to check one instance for its name, I have to issue two !DumpObj commands and do a lot of copy-and-paste to copy the addresses from the console to the command line. And in this example, there are only 20 instances. In the process I debugged, there were much more. There must be a better way to do this. A colleague of mine jumped to help and together we indeed came up with one.

The idea is the following:

Get a list of the addresses of all Person instances.

For each entry

Given the address of the Perons instance, get the value of the <Name>k__BackingField

Print out the address of the Person instance along with the string value of the <Name>k__BackingField.

The first step is easy: Just execute this line:

!DumpHeap -type Person -short

This command will just dump the addresses of each instance to the console. Iterating through this list is also trivial, thanks to the foreach command:

For each entry in the list returned by the command between the first pair curly braces, the variable entry is populated with the value and the command sequence between the second pair of curly braces is executed. The example above just prints out the value itself.

Now comes the harder part. We need to figure out the value of the backing field for the Name property. A closer inspection of the !DumpObj result above reveals that the field is stored at memory offset 8 inside of the object. To read that memory directly, the dq command can be used (dq because I’m debugging a 64bit process. Use dd for a 32bit process). Dq prints the memory at the specified address in the form of 64bit hex values to the screen. Since we are only interested in the 64-bit pointer at offset 8 of that object, we issue this command:

0:007> dq 00000000027f2830+8 L1
00000000`027f2838 00000000`027f22e8

The 00000000027f2830+8 specifies the memory address we are interested in. And the L1 specifies that we only want one quad-word. You can see that the result from the dq operation equals the vlaue of the backing field: 00000000027f22e8. We now need a way to dereference that memory address and print the value of the string at that point. At this point it’s good to know how .NET strings are stored in memory: A length indicator (32bit) followed by an unicode-char-array. The result of the DumpObj command on the string instance “Bob” above reveled that the character array starts at offset 0x0c within the string instance. To print the value of the string, the dq command cannot be used because we are interested in the string value itself. For this, the du command can be used. du means: Dump unicode string. To dereference the pointer provided by the dq command, the poi operator can be used. All combined, this resolves to this command:

0:007> du poi(00000000027f2830+8)+c
00000000`027f22f4 "Walter"

Nice, exactly what we wanted. Now we can combine this with the foreach command above:

Commenter Mike had questions on some details of the article I wrote about Exchange streaming notifications (See the comments in this blog post). In the MSDN article, I wrote this:

Streaming notifications do not work this way. Instead, you can use a combination of notifications and the ExchangeService.SyncFolderItems method. The SyncFolderItems method gives you a snapshot of the changes that occurred in a particular folder. This method also returns a small cookie that contains information about the synchronization state. You can pass this synchronization state to subsequent calls of the SyncFolderItems method to get all changes that have occurred since that synchronization cookie was first issued. You should persist this synchronization state somewhere (either on disk or in a database) so that the application does not need to process all of the items in the folder gain.

I further outlined the steps an application should follow when using streaming subscriptions:

After your application starts, use the SyncFolderItems method to process all unprocessed items in the folder.

Create a streaming notification and connection as specified earlier in this article.

Use the SyncFolderItems method again to process all events that occurred since the last synchronization and before the subscription was activated.

On each OnNotification event, use the GetItem operation to get additional properties of the specified items.

Call the SyncFolderItems method periodically to update the local synchronization cookie.

This makes the application seemingly more complex than it needs to be because your application must be able to deal with different change notifications: Those from the streaming subscription and those from SyncFolderItems. Additionally, once a notification is received through the streaming subscription, the synchronization state from the last call to SyncFolderItems is stale. The application must now deal with the situation that the initial call of SyncFolderItems on an application restart might return changes which already have been processed by the application. The application needs to deal with this situation.

Mike asks whether it’s ok to ignore the item ids specified in a notification and just use the SyncFolderItems method on each notification. Using this approach, the synchronization state returned by the SyncFolderItems method would always be current and the whole application design would be more simple.

The answer is: It depends. If you have a low-volume application and your Exchange server is not used at full capacity, this might be ok. If you are writing an in-house application where you know everything about your infrastructure, this might be acceptable. But keep in mind that an application might run several years and the server load of your Exchange Server might go through the roof during the lifetime of your application. And if you are a software developer who sells his application to (hopefully) many customers, you don’t know anything about the performance situation on these servers. So it is best to conform to best practice and use the rules I outlined in the article.

The mail headers are exposed on the EmailMessage as first class property: InternetMessageHeaders. However, Exchange also stores each mail header in its own property and defines a unique property set for it: PS_INTERNET_HEADERS.

This query will return the first ten messages which have said header set to “Yes”.

Note that Exchange has a limit for named properties which are the result of custom mail headers. You may get one of these entries in your EventLog at some point:

Event ID: 9666
Type: Warning
Category: General
Source: msgidNamedPropsQuotaWarning
Description: The number of named properties created for database "<database name>" is close to quota limit. Current number of named properties: <number of named properties>. Quota limit for named properties: <configured quota>. User attempting to create the named property: <user name>. Named property GUID: <GUID of named property>. Named property name/id: <name of named property>.

Event ID: 9667
Type: Error
Category: General
Source: msgidNamedPropsQuotaError
Description: Failed to create a new named property for database "<database name>" because the number of named properties reached the quota limit (<configured quota>). User attempting to create the named property: <user name>. Named property GUID: <GUID of named property>. Named property name/id: <name of named property>.

The only interesting line in the sample above is line 4. The call to MasterCategoryList.Bind() loads the MasterCategoryList from the users Exchange Mailbox. After that, the name of each console is written to the console.

So how does this work? The Master Category List is stored in a hidden message in the calendar folder of a mailbox. EWS in 2010 provides simple access to this message with the UserConfiguration class. This code show how the Master Category List is loaded:

Interesting question in the Exchange development forum today: How do I get the number of unread messages in the inbox folder from a number of mailboxes? The questioner wants to send a text message to each owner of a mailbox notifying him of the number of unread mails in his inbox. The requirement was to do this with PowerShell. So, here is a quick solution for this problem:

The script mainly consists of two function: GetUnreadInboxMails and ProcessResult. The former method retrieves the number of unread mails in the inbox of the mailbox of the specified user. The number of unread mails can be retrieved from a Folder instance: The UnreadCount property. The method returns the number of unread mails along with the mail address of the current mailbox. The other important function is the ProcessResult method. In this script it merely dumps the result to the console. The questioner in the post linked above can use this function to call his HTTP service to send a text message.

The script has two parameters:

The name of a CSV file containing the list of mailboxes to process. The first line should contain the column name “MailAddress” somewhere. The following lines should contain the email address of the mailbox in this column.

A PSCredential object containing valid credentials to use for the Exchange access.

The credential used for the script needs read access to the inbox folder of each mailbox specified in the file.