将非 ASCII 域转换为 SMTP 兼容域

发布于 2024-08-08 18:21:53 字数 153 浏览 6 评论 0原文

当客户输入带有非 ASCII 字符(例如 äüö)的电子邮件地址时,我们的 SMTP 会拒绝处理它们。

所以我认为可能有一个解决方案可以自己处理这些域并将它们转换为 punyocode。

有没有使用 C# 的简单方法?

无论如何,这会起作用吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

讽刺将军 2024-08-15 18:21:53

您可以使用 Uri.DnsSafeHost 转换为 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;
    }
}

在 app.config 中:

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

结果:

xn--caf-dma.com

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
染墨丶若流云 2024-08-15 18:21:53

这种方法的问题是您将更改电子邮件地址。

电子邮件地址 [email protected] 和 < code>bevä[email protected] 不同 电子邮件地址,无论它们看起来多么相似。

进行您建议的更改将破坏电子邮件 - 人们可能会收到邮件,但无法回复这些邮件。

不处理重音字符的 SMTP 服务器听起来就像恐龙一样。尽管这可能是众所周知的痛苦,但更换和/或升级可能是最好的解决方案。

您可能能够在 ServerFault 上获得更合适的帮助。

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.

枕花眠 2024-08-15 18:21:53

将电子邮件地址域名中的 Unicode 字符转换为 Punycode 可以解析正确的 DNS 记录,因此不会被 SMTP 拒绝。只需使用 System.Uri 即可完成,无需修改 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);
}

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文