Unable to send Bcc using System.Net.Mail when specifying a Pickup Directory(Exchange 2007/Exchange 2010) in code

Not sure developers using System.Net.Mail are not complaining about this issue? The issue is very easy to reproduce, all you need to do is create a new mail with someone on Bcc and drop it to the Pickup folder In Exchange. Most likely not to many people use Bcc or use Port instead of Pickup.

 MailMessage message = new MailMessage("User1@mycompany.com", "User2@mycompany.com", "Sent Using Pickup", "This Message was sent using Pickup");
message.Bcc.Add(new MailAddress("User3@mycompany.com"));
SmtpClient client = new SmtpClient();
client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
client.PickupDirectoryLocation = @"C:\Program Files\Microsoft\Exchange Server\TransportRoles\Pickup";
client.Send(message);

If we send a mail using the above code we would expect that the mail gets delivered to the Bcc user i.e User3@mycompany.com, but that does not happen! Why would that happen? I then tried to use CDOSYS to create a mail and added Bcc to it and this time the mail was delivered to User3@mycompany.com.

What could the difference be? I stopped the Transport service on my Exchange 2007(installed on Windows 2003) so that the mail do not get delivered by the transport and then examined the .eml to check what the difference could be. Below is what I find:

System.Net.Mail uses X-Headers to add the Recipient information to the mail.Below is what the MIME look like on my Windows 2003 box:

 X-Sender: User1@mycompany.com
X-Receiver: User2@mycompany.com
X-Receiver: User3@mycompany.com <- This is the BCC
MIME-Version: 1.0
From: User1@mycompany.com
To: User2@mycompany.com
Date: 19 Jul 2012 13:53:05 -0400
Subject: Sent Using Pickup
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: quoted-printable

This Message was sent using Pickup

Below is what the MIME looks like when I used CDOSYS on my Windows 2003 box:

 thread-index: Ac1l181oBFqzycOSRFaCD7mjVftgqw==
Thread-Topic: This is a test CDOSYS message (Sent via Pickup)
From: <User1@mycompany.com>
To: <User2@mycompany.com>
Bcc: <User3@mycompany.com>
Subject: This is a test CDOSYS message (Sent via Pickup)
Date: Thu, 19 Jul 2012 13:56:20 -0400
Message-ID: <D76CF8D778AE43A096B10F2281A9914F@mycompany.com>
MIME-Version: 1.0
...

If you notice, the above MIME does not use X-Headers, it uses the Bcc header and it works. Unfortunately, this behavior of CDOSYS could be different on different environments. When I tested on a Windows 7 box, the MIME looks very similar to what we get from System.Net.Mail(uses X-Header).

Based on my discussion with the Exchange guys, it turns out that when the mail is put in the Pickup folder, it does not look at the X-Headers and uses the From, To, Bcc headers if present.

 

The simplest solution was to drop the mail in the Replay folder instead of the Pickup folder.

 MailMessage message = new MailMessage("User1@mycompany.com", "User2@mycompany.com", "Sent Using Pickup", "This Message was sent using Pickup");
message.Bcc.Add(new MailAddress("User3@mycompany.com"));
SmtpClient client = new SmtpClient();
client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
client.PickupDirectoryLocation = @"C:\Program Files\Microsoft\Exchange Server\TransportRoles\Replay";
client.Send(message);

 

Below is an article that talks about both the directories, although it does not mention that X-Headers are ignored in the pickup, it does explicitly say that Replay makes use of the X-Headers.

Understanding the Pickup and Replay Directories

https://technet.microsoft.com/en-us/library/bb124230.aspx

I have tested this on Exchange 2007 installed on Windows 2003 and it works!

Enjoy!