4

When attempting to send an e-mail using Exchange Web Services Managed API against Exchange Server 2010 SP1, a ServiceException is thrown with ErrorCode ErrorInvalidRecipients when the recipient e-mail address (left of @) contains, in this case, danish letters æøå.

Are these characters just not allowed in the local part of e-mail adresses, or should I somehow encode the address?

Update: Here's the test source code and trace data:

var service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
service.AutodiscoverUrl("my@email.com");
EmailMessage email = new EmailMessage(service);
email.Body = "Test";
email.Subject = "Test";
email.ToRecipients.Add("æøå@domain.com");
email.Send();

Trace data from EWS shows the following:

Request headers:

POST /EWS/Exchange.asmx HTTP/1.1
Content-Type: text/xml; charset=utf-8
Accept: text/xml
User-Agent: ExchangeServicesClient/14.03.0032.000
Accept-Encoding: gzip,deflate

Request:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <t:RequestServerVersion Version="Exchange2010_SP1" />
  </soap:Header>
  <soap:Body>
    <m:CreateItem MessageDisposition="SendOnly">
      <m:Items>
        <t:Message>
          <t:Subject>Test</t:Subject>
          <t:Body BodyType="HTML">Test</t:Body>
          <t:ToRecipients>
            <t:Mailbox>
              <t:EmailAddress>æøå@domain.com</t:EmailAddress>
            </t:Mailbox>
          </t:ToRecipients>
        </t:Message>
      </m:Items>
    </m:CreateItem>
  </soap:Body>
</soap:Envelope>

Response:

<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header>
    <h:ServerVersionInfo MajorVersion="14" MinorVersion="1" MajorBuildNumber="438" MinorBuildNumber="0" Version="Exchange2010_SP1" xmlns:h="http://schemas.microsoft.com/exchange/services/2006/types" xmlns="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" />
  </s:Header>
  <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <m:CreateItemResponse xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
      <m:ResponseMessages>
        <m:CreateItemResponseMessage ResponseClass="Error">
          <m:MessageText>One or more recipients are invalid.</m:MessageText>
          <m:ResponseCode>ErrorInvalidRecipients</m:ResponseCode>
          <m:DescriptiveLinkKey>0</m:DescriptiveLinkKey>
          <m:Items />
        </m:CreateItemResponseMessage>
      </m:ResponseMessages>
    </m:CreateItemResponse>
  </s:Body>
</s:Envelope>

Finally, creating an e-mail in Outlook to such an address gives this message:

We wont be able to deliver this message to ___ because the email address is no longer valid

bernhof
  • 5,936
  • 2
  • 40
  • 66
  • can you show request/response http dump? We support danish and don't have that problem – Ivan G. Apr 28 '14 at 12:49
  • Are you encoding left part of @ to UTF8? is also the domain a non-ASCII string? if so, don't forget the punycode for that second part. – Jportelas Apr 28 '14 at 20:44
  • What version of Exchange are you using? I seem to remember that some of the service packs and update rollups for Exchange 2010 included fixes for EWS handling special characters. – BateTech Apr 29 '14 at 16:45
  • @BateTech 2010 SP1. Updated the question. So an upgrade to SP2 might solve the problem? – bernhof Apr 29 '14 at 18:06
  • @Jportelas how should I encode? I'm using the Managed API: `email.ToRecipients.Add("æøå@domain.com")`. The domain name is not non-ASCII. – bernhof Apr 29 '14 at 18:08
  • @aloneguid Please see updated question - also see screen dump at the bottom. You support danish, so I suppose you don't get that message in Outlook? – bernhof Apr 29 '14 at 21:44
  • @mbjdev I looked through the Exchange service packs and updates and couldn't find anything that specifically addressed issues with special characters in the recipient address, so I may have been remembering wrong in my comment above. I did find one mention in Update Rollup 4 for Exchange Server 2010 Service Pack 1 that says "An update enables range 0x-0x1F characters in the display name of an Exchange Server 2010 user account" so that might've been what i was thinking of, but that doesn't help you b/c the characters you mentioned are not in that range. http://support.microsoft.com/kb/2579150 – BateTech Apr 29 '14 at 22:17
  • For the domain portion, since it is non-ASCII, you might need to check out the `System.Globalization.GetAscii` method to encode it. http://msdn.microsoft.com/en-us/library/vstudio/3dcsw2h8 – BateTech Apr 29 '14 at 22:27

1 Answers1

1

According to this article dealing with Exchange recipient resolution, you may need to encode these characters using this method:

Alphanumeric characters, the equal sign (=) and the hyphen (-) don't require encoding. Other characters use the following encoding syntax: A forward slash (/) is replaced by an underscore (_). Other US-ASCII characters are replaced by a plus sign (+) and the two digits of its ASCII value are written in hexadecimal. For example, the space character has the encoded value +20.

http://technet.microsoft.com/en-us/library/bb430743(v=exchg.141).aspx

So your example of "æøå@domain.com" would become "+e6+f8+e5@domain.com"

EDIT:

Also the "domain" portion of the address will need encoded using a different method, if it uses special characters.

Here is a GetExchangeEncodedRecipient helper method I put together to encode the email address using the RFC 5321 and RFC 3461 specs.

    public static string GetExchangeEncodedRecipient(string recipient) {
        int atIdx = recipient.LastIndexOf('@');

        if (atIdx < 0) {
            throw new ArgumentException("Unable to parse domain portion of \"recipient\" email address.");
        }

        string namepart = recipient.Substring(0, atIdx);
        string domainPart = recipient.Substring(atIdx + 1);
        //need to encode any special characters in the domain name
        System.Globalization.IdnMapping punycode = new System.Globalization.IdnMapping();
        domainPart = "@" + punycode.GetAscii(domainPart);

        return String.Concat(namepart.Select(c => GetExchangeEncodedChar(c))) + domainPart;
    }
    private static string GetExchangeEncodedChar(char c) {
        string encodedChar = c.ToString();
        int charASCIICode = (int)c;
        //Encode according to RFC5321, https://tools.ietf.org/html/rfc5321#section-4.1.2
        //  which references rfc3461 "xtext" format. https://tools.ietf.org/html/rfc3461#section-4 
        if(charASCIICode >= 33 && charASCIICode <= 126 && c != '+' && c != '=')
        {
            //This is a character in the valid 33-126 ASCII range for email addresses, which does not need encoded.
            return c.ToString();
        }
        else if(c == '/'){
            //A forward slash (/) is replaced by an underscore (_).
            return "_";
        }
        else { 
            return "+" + ((int)c).ToString("x2").ToUpper();
        }
    }

Then you can use GetExchangeEncodedRecipient to encode the address. Here is an example:

string recipient = @"user1æøå@dømain.com";
string encodedAddress = GetExchangeEncodedRecipient(recipient);
Console.WriteLine("Original: {0}, Encoded: {1}", recipient, encodedAddress);

Which for the example recipient above, will output:

Original: user1æøå@dømain.com, Encoded: user1+E6+F8+E5@xn--dmain-vua.com

And if you use with a "normal" email address which does not contain any characters out of the "normal" ASCII range, then it return the same string.

Original: jsmith01@gmail.com, Encoded: jsmith01@gmail.com

BateTech
  • 5,047
  • 3
  • 18
  • 31
  • Hmm, in this case it doesn't complain, but the e-mail doesn't arrive (I'm testing with an @mailinator.com address). I tried creating a message in Outlook to the same address, and here it also fails. See screen dump added to question (bottom)... – bernhof Apr 29 '14 at 21:41
  • @mbjdev I updated the answer with a code sample for the address encoding based on some of the RFCs I could find. Can you try using the encoded address from that method and see if it works? I don't a valid email address that meets this criteria in our environment to test against. – BateTech Apr 30 '14 at 02:37
  • Great work, I will test this as soon as I can and get back to you. I really appreciate your efforts. – bernhof Apr 30 '14 at 05:48