locked
MailMessage HeaderCollection RRS feed

  • Question

  • Hi,

    I have created a program that sending email until now it working perfect,
    But after i add the following line before calling the send method :
    mailMessage.Headers.Add("khalilKey", "khalilVal");

    Sometime it sent the mail with no errors and sometime i accept the following error :
    Failure sending mail.
    An invalid character was found in the mail header.

    What causing this error and how can i resolve it ?

    Thnx,
    khalil

    Thursday, May 26, 2011 7:35 AM

Answers

  • When i accept the exception i found that only one key added to the header and it is :

    MIME-Version

    The exception doesn't contain the offending character but the inner exception gives that it failed at the header.Set method.

     

    Thnx,
    khalil

     


    • Marked as answer by khalil salhi Tuesday, February 7, 2012 8:58 AM
    Wednesday, June 1, 2011 9:46 AM

All replies

  • I presume that you are not always sending the same key/value pair, or at least not the one shown here.

    The main problem with e-mail is that not all the servers and clients enforce the rules as strictly as they should. This makes it possible to send an (otherwise invalid) email on some route and have it failing on another. My advice here is to be as strict as possible to make sure that your message (and your headers) go through as intended.

    RFC 2822 mandates that headers should only contain ASCII characters (range 33-126 for field names, 0-126 except CR-LF for values). As far as I know, SmtpClient will automatically encode your header values to Base64 for you if the string contains spurious characters, but no such treatment is allowed, nor performed, for field names.

    Make sure that your header key is always comprised in the ASCII range 33-126. Incidentally, when you add your own headers to an e-mail, it's always a good idea to prefix them with "X-" as the RFC suggests.

    HTH
    --mc

    Friday, May 27, 2011 2:42 PM
  • This is the code with the real values that i use :

     

    SmtpClient oSmtpClient = new SmtpClient();
    oSmtpClient.Host = sHost;
    oSmtpClient.Port = 25;
    oSmtpClient.UseDefaultCredentials = true;
    oSmtpClient.Timeout = iConnectTimeOut; 
    
    MailMessage oMailMessage = new MailMessage();
    oMailMessage.From = new MailAddress("khalil@rashim.co.il", "חליל");
    oMailMessage.Sender = new MailAddress("khalil@rashim.co.il", "חליל");
    oMailMessage.BodyEncoding = Encoding.UTF8;
    oMailMessage.SubjectEncoding = Encoding.UTF8;
    oMailMessage.Subject = "הודעה";
    oMailMessage.IsBodyHtml = true;
    oMailMessage.Body = "<p>This is simple html message</p>";
    oMailMessage.Headers.Add("RashimMsgId", "123");
    oMailMessage.Headers.Add("RashimReceiptID", "13");
    oMailMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.Delay |
    DeliveryNotificationOptions.OnFailure |
    DeliveryNotificationOptions.OnSuccess;
    oMailMessage.Bcc.Add("khalil@rashim.co.il");
    oMailMessage.To.Add("khalil@rashim.co.il");
    oSmtpClient.Send(oMailMessage);
    
     
    I did add to the header just these tow keys.
    After the send method completed i check the header and i saw that there is 5 additional keys added automatically and they are :
    MIME-Version
    From
    Sender
    Date
    Subject 
    Thnx,
    khalil


    Sunday, May 29, 2011 6:41 AM
  • I don't know what might be causing your issue... I think I traced the exception back to the BufferBuilder used to write the headers, but this doesn't give much of a clue as my understanding is that any character > 255 would already have triggered some sort of additional encoding (either Base64 or quoted printable) and that would guarantee that all the characters are in the ASCII range.

    The good news is that the exception should contain the offending character. If you make sure that each character only appears in one header, when you get the exception you should be able to determine which header is causing the problem and we may further examine the whole issue.

    HTH
    --mc

    Tuesday, May 31, 2011 5:13 PM
  • When i accept the exception i found that only one key added to the header and it is :

    MIME-Version

    The exception doesn't contain the offending character but the inner exception gives that it failed at the header.Set method.

     

    Thnx,
    khalil

     


    • Marked as answer by khalil salhi Tuesday, February 7, 2012 8:58 AM
    Wednesday, June 1, 2011 9:46 AM