Search code examples
c#asciidiacriticsemail-addresspunycode

Convert non-ascii domain to SMTP compatible


When customers enter email addresses with non-ascii chars like äüö our SMTP rejects to process them.

So I think might be there is a solution to handle those domains myself and convert them to punyocode.

Is there a simple way of doing so using c#?

Would this work anyway?


Solution

  • You can use Uri.DnsSafeHost to convert to Punycode:

    using System;
    
    class Test
    {
        static void Main()
        {
            Console.WriteLine(ConvertToPunycode("caf\u00e9.com"));
        }
    
        static string ConvertToPunycode(string domain)
        {
            Uri uri = new Uri("http://"+domain);
            return uri.DnsSafeHost;
        }
    }
    

    In app.config:

    <configuration>
      <uri>
        <idn enabled="All" />
      </uri>
    </configuration>
    

    Result:

    xn--caf-dma.com