Microsoft Exchange allows a user to access another User’s mailbox in 2 ways, Delegation and Impersonation (Check this for more details). If you have used Outlook or Outlook web Access (OWA) and shared mailboxes with other users, chances are that you used Exchange delegation for mailbox sharing.

For our Exchange email client for corporates, we were using delegation to enable users to share Mailboxes, when we faced an important constraint. If you (the delegate) are accessing someone else’s (the delegator’s) mailbox via Exchange delegation, any mails sent on behalf of the delegator would appear in your (delegate’s) mailbox’s Sent Items. Ideally, it should have shown in the delegator’s Sent Items.

This is a documented (see this) Exchange constraint. However, we were surprised to see that Outlook and OWA did not follow this constraint and mails sent through them on behalf of the delegator actually appeared in the delegator’s mailbox only (which was desired).

We thought there must be some trick to accomplish this for our own mail client (which uses Exchange Web Services Managed API for Exchange interaction).

After some testing, I got the following code to work and save the email sent to delegator’s sent items folder when sent by a delegate:

 

message.Send();
message.Save(new FolderId(WellKnownFolderName.SentItems, new Mailbox("[email protected]")));

Exchange delegation allows you to save Mails to delegator’s account. So, what I did was to send the mail without saving it to the delegate’s mailbox, and then save it in the delegator’s mailbox after it was sent.

The above approach worked fine, until it was reported that it failed if the Mail being sent had attachments. The error reported by Exchange was that you cannot call Save() for items that have already been saved (but instead should call Update()). This made sense, because attachments were automatically saved on Exchange when Send() was called. But we could not call Update() as the attachments were saved to delegate’s mailbox, but we wanted to save in delegator’s mailbox.

Again a quick check in OWA established that it saved the mail with attachments to delegator’s mailbox only, providing sufficient clue that this should be possible.

Some more tweaking of the code, and this worked for mails with attachments:

 

message.Save(new FolderId(WellKnownFolderName.Drafts, new Mailbox("[email protected]")));
message.SendAndSaveCopy(new FolderId(WellKnownFolderName.SentItems, new Mailbox("[email protected]")));

This time, we saved the message first to delegator’s Drafts folder, and then sent it saving it to the delegator’s SentItems folder after the mail was sent.

The combination of above approaches with the first being used if no attachments are present, and the second used for mails sent on behalf of delegator with attachments resolved all scenarios.

Now, I really wonder, why MSDN says “Messages that are sent by delegates are saved in the delegate’s Sent Items folder“, and does not provide this simple work-around to save messages sent to delegator’s mailbox.

UPDATE:

  • Jan 7, 2011 – During my later testing, I found that there are issues with the first approach when forwarding/replying to mails for a delegated mailbox. So, I now instead use the second code above only, whether or not the mail has attachments. To eliminate any confusions, here is what I use now, no matter the mail has attachments or not:

    {syntaxhighlighter brush: csharp;fontsize: 100; first-line: 1; }if (!savedAlready)
    {
    message.Save(new FolderId(WellKnownFolderName.Drafts, new Mailbox(“[email protected]”)));
    }
    message.SendAndSaveCopy(new FolderId(WellKnownFolderName.SentItems, new Mailbox(“[email protected]”)));{/syntaxhighlighter}
    While forwarding/replying to mails for the delegated mailbox, I set a flag which indicates that the Mail is already saved. You cannot call Save() on the mail MailMessage twice (it would give an exception, instead you need to use Update() after the initial Save() for the EmailMessage). The check for !savedAlready ensures that no exception occurs is the mail is already saved.