Archive for the ‘Exchange’ Category.

Tribal Knowledge: EWS C# Extract Alternate Email Address’ Mailbox

stockxpertcom_id260320_jpg_7150e45f4e023f0d664c8903434549ef This is what I call a Tribal Knowledge (TK) type of  information. TK is something that is obvious to those who work with it, or have been using it for a long time yet to the new person coming along its a mystery. Accessing a different mailbox using Exchange Web Services (EWS) was such an event for me. I had actually tapped Microsoft Premier Support to get this answer but was able to divine it at the last moment.

Why Is This Confusing?

The reason behind this confusion is how one registers a service with Exchange Web Services. Here is a snippet below:

ExchangeService service = new ExchangeService( ExchangeVersion.Exchange2007_SP1 );

service.AutodiscoverUrl( "Omega.Man@MyCompany.com" );

When we pass the email address to Autodiscover we believe that it is registering the account we want to access. Usually when one is developing against EWS they use their mailbox as the target first and will have no problems. (See my article entitled C#: Getting All Emails From Exchange using Exchange Web Services for how process emails from the inbox.)

Once the developer access the inbox and retrieves emails, the next step is to attempt access to a shared mailbox. That inbox is shown in Outlook and the user’s account has access rights to it. So the developer does this and erroneously uses Autodiscover again:

service.AutodiscoverUrl( "SharedMailboxNotPrimaryAccount@MyCompany.com" );

Well it actually works, sort of… the service doesn’t throw an exception, but when on tries to get email, its the primary email inbox that is gotten and not the specified email! The problem is that autodiscover could frankly care less about the who, its only concerned with the @xxxx.com to facilitate its processing. Very disingenuous eh…

Solution

Get the service with the email address as shown above (either one will work actually), but before EWS mail box processing add these lines:

Mailbox mb = new Mailbox( "SharedMailboxNotPrimaryAddress@MyCompany.com" );

FolderId fid1 = new FolderId( WellKnownFolderName.Inbox, mb );

FindFoldersResults findResults = service.FindFolders(
                fid1, // was WellKnownFolderName.Inbox,
                new FolderView( int.MaxValue ) );

That lets EWS know about which mailbox we want to use. It handles the getting of the folder ID (different from the ID you might use in Outlook Interop programming if you got it that way; btw) and all is well.

HTH

Share

You won’t Believe what the Three Biggest Gotchas are in Installing Exchange 2007 SP1 on Server 2008 R2 are; At Least For me…

FrustrationHere are the three biggest hurdles in an almost impossible battle I had in trying to install Exchange on Server 2008 R2. Did I expect to turn off UAC and run in Vista compatibility mode…nope. Did expect to do twenty questions with an install on exactly what file can’t be read…no on that one as well. Even if you are not installing Exchange…read what I had to do…it may amuse you. Odysseus would be proud…

UAC Not Just for Vista Anymore

One doesn’t think that the User Account Control comes into play on a server…wrong. If you don’t have UAC set to minimums you won’t even be able to get past the prerequisites check! Here is the nasty little error you will get:

Setup failed due to insufficient permissions.  Please make sure that the current user has local administrator permissions. Error: The source was not found, but some or all event logs could not be searched.  Inaccessible logs: Security.

Search for UAC in the search bar and change accordingly.

What Missing File? Are you going to tell me? No???

Ok you have now made it to the actual install and it fails immediately on the Organization Preparation section. A winform message box comes up “File Not Found”. But it won’t tell you what that file is, where it exists or anything else even pertinent. The only clue is a flashed windows mentioning Active Directory (if you are lucky).

Looking in the logs you see this:

[WARNING] An unexpected error has occurred and debug information is being generated: The system cannot find the file specified
[10/12/2009 1:03:46 PM] [1] [ERROR] The system cannot find the file specified
[10/12/2009 1:03:46 PM] [1] [ERROR] The system cannot find the file specified

Hey Exchange Install, thanks for telling me three times! I wouldn’t have gotten the first time!

I would love to tell you that I figured this one out on my own but thanks to the magic of the internet, someone posted the solution (Installation error when installing Exchange 2007 SP1 on Windows Server 2008 R2) exactly on the same day I was searching! Here is the advice given for Server 2008 R2 (the one to run Exchange not the domain controller)  type this into PowerShell:

Import-Module Servermanager
Add-WindowsFeature RSAT-ADDS

Install Is a Dish Best Served Cold

Hey…its working its installing…oh, the last step failed! How prophetic, how ironic! It fails on MailBox Role….the very last step!:


An error occurred. The error code was 3221684229. The message was Access is denied..

So you are thinking what was missing? Was it  from my features/roles for setting up the server? Did I have a missing permission…naw I was foolish enough to not run the setup in Vista Compatibility mode. Heck what was I thinking.

Yes highlight the install and specify in the Compatibility tab as “Vista”.

If you are starting out in installing Exchange, this guide will be of use…more likely you have one of the errors mentioned above and are here. HTH.

Share

C#: Getting All Emails From Exchange using Exchange Web Services

Email_OpsThis article demonstrates how to use Exchange Webservices (EWS), as found on Exchange 2007 SP1 and going forward, to extract email body, headers and other email related items without using or needed Outlook installed. Note this assumes you are using Visual Studio 2008 and C#.

Steps

  1. Download and install the EWS assemblies from Exchange Web Services Managed API and install the target version either 32 or 64. Please remember to note the directory which it gets installed to for it is needed later. Of note is that the install directory contains an excellent document Getting Started which has examples that show the capabilities of the web services.
  2. Create your project in Studio, the below example C# code targets a Console Application.
  3. Add a reference to Microsoft.Exchange.WebServices by browsing to the directory loaded in step 1 and selecting the  Microsoft.Exchange.WebServices.dll. Remember to add using Microsoft.Exchange.WebServices.Data; in your code.
  4. Add these lines of code to initialize the web service:
    ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
    
    //service.Credentials = new NetworkCredential( "{Active Directory ID}", "{Password}", "{Domain Name}" );
    
    service.AutodiscoverUrl( "First.Last@MyCompany.com" );

     

    • Line 1 : Create an Exchange Web Service instance specifying that we are targeting a specific version of Exchange.
    • Line 3 :   You may need to have a service account do your dirty work and this line uncommented out is your key to doing that.
    • Line 5 : We don’t specify an Exchange server, but auto discover it. The server may be clustered and this will get the best one for the job. We also specify the account’s mailbox to use.
  5. Once the service is up and running we can now get the emails found in the box. Here is how we do it:
    FindItemsResults<Item> findResults = service.FindItems(
       WellKnownFolderName.Inbox,
       new ItemView( 10 ) );
    
    foreach ( Item item in findResults.Items )
       Console.WriteLine( item.Subject );

     

    • Line 1 :  We call the service to find out about the mailbox.
    • Line 2 :  We are interested in the inbox only, but we could be interested in the calendar. This is where to specify those items.
    • Line 3 :  We only want 10 items…change that number for different results
    • Line 5:  Print out the items, we are only going to show the subject but there are other header items we could show.
  6. Run the program. If everything goes alright then we have gotten the top ten items of the mailbox.

That should get you started. Again check the Getting Started document which the install dropped into the install folder for more examples!

Share