kcode
kcode

Reputation: 1280

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?

Upvotes: 3

Views: 2548

Answers (3)

Dustin Cyca
Dustin Cyca

Reputation: 1

Converting Unicode characters in your email address' domain name to Punycode resolves the correct DNS records so it won't be rejected by your SMTP. It can be done simply using System.Uri without modifying app.config:

/// <summary>
/// Try passing example@äüö.com
/// </summary>
/// <param name="email"></param>
/// <returns></returns>
internal string GetSafeEmail(string email)
{
    const char at = '@';
    string[] split = email.Split(at);
    string safeHost = new Uri("http://" + split[1]).IdnHost;
    // Don't convert the username to Punycode
    return string.Join(at, split[0], safeHost);
}

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1502036

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

Upvotes: 8

Bevan
Bevan

Reputation: 44307

The problem with this approach is that you'll be changing the email addresses.

The email addresses [email protected] and bevä[email protected] are different email addresses, however much they appear the same.

Making the change you suggest will break email - people might receive the messages, but they won't be able to reply to them.

Your SMTP server that doesn't handle accented characters sounds like a dinosaur. Much as it might be a pain in the proverbial, replacement and/or upgrade is likely the best solution.

You'll likely be able to get more appropriate assistance over on ServerFault.

Upvotes: 0

Related Questions