CDO and Custom Headers

Posted by

Seems there's another problem with CDO (see my previous post on the first one I found :) ). This one has to do with custom headers.

Now, according to RFC 822, CRLFs are illegal as part of a header (which makes sense, of course, because the CRLF is the delimiter for headers!). Strangely, bare CR and bare LF are OK, but that doesn't really matter right now. The problem is that if you set the value of a header via one of the short-cut properties (like Subject, To, From, etc) then it replaces any CRs with spaces. But if you set a property visa the Fields property, then it doesn't. This can be a real problem if you have code like this:


static void Main(string[] args)
{
  CDO.Message message = new CDO.MessageClass();
  message.From = "from@example.com";
  message.To = "to@example.com";
  message.Subject = "Hello World!";

  // This is the fateful line here:
  message.Fields["urn:schemas:mailheader:X-MyCustomHeader"]
    .Value = "Something with a new-line at the end:\r\n";
  message.Fields.Update();

  message.TextBody = "Some text goes in the body, too.";

  message.Configuration.Fields
    [CDO.CdoConfiguration.cdoSendUsingMethod].Value
      = CDO.CdoSendUsing.cdoSendUsingPickup;
  message.Configuration.Fields
    [CDO.CdoConfiguration.cdoSMTPServerPickupDirectory].Value
      = Environment.CurrentDirectory;
  message.Configuration.Fields.Update();;

  message.Send();
}

If you have something like this, then any headers that appear after X-MyCustomHeader will look as though they're part of the message body.

Now, obviously, the example above is rather contrived. But it's not always so obvious. There's also a really simple workaround: just strip off all the CRLFs. However, CDOEX that comes with Exchange 2000, doesn't have the same bug - it replaces any CRLFs with spaces, no matter how you put them in there.

blog comments powered by Disqus