无法发送电子邮件与斯堪的纳维亚的字符地址 [英] Cannot send emails to addresses with Scandinavian characters

查看:357
本文介绍了无法发送电子邮件与斯堪的纳维亚的字符地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 SmtpClient MAILMESSAGE MailAddress 类,我不能发送到电子邮件地址,如åbc.def@domain.se。我得到的错误/异常如下图所示:

Using SmtpClient, MailMessage and MailAddress classes, I cannot send to email addresses such as åbc.def@domain.se. I get the error/exceptions as shown below:

这是无效字符在邮件标题中发现:以'

An invalid character was found in the mail header: 'å'.

---------------------------错误发送电子邮件
----- ---------------------- System.Net.Mail.SmtpException:客户端或服务器仅配置了E-mail地址与ASCII
局域部分:åbc.def@domain.se

--------------------------- Error sending email --------------------------- System.Net.Mail.SmtpException: The client or server is only configured for E-mail addresses with ASCII local-parts: åbc.def@domain.se.

在System.Net.Mail.MailAddress.GetUser(布尔allowUnicode)

at System.Net.Mail.MailAddress.GetUser(Boolean allowUnicode)

在System.Net.Mail.MailAddress.GetAddress(布尔allowUnicode)

at System.Net.Mail.MailAddress.GetAddress(Boolean allowUnicode)

在System.Net.Mail.MailAddress.Encode(的Int32 charsConsumed,布尔
allowUnicode)

at System.Net.Mail.MailAddress.Encode(Int32 charsConsumed, Boolean allowUnicode)

在System.Net.Mail.MailAddressCollection.Encode(的Int32
charsConsumed,布尔allowUnicode)
在System.Net.Mail .Message.PrepareHeaders(布尔sendEnvelope,
布尔allowUnicode)
在System.Net.Mail.Message.Send(BaseWriter作家,布尔
sendEnvelope,布尔allowUnicode)
的系统。 Net.Mail.SmtpClient.Send(消息MAILMESSAGE)

at System.Net.Mail.MailAddressCollection.Encode(Int32 charsConsumed, Boolean allowUnicode) at System.Net.Mail.Message.PrepareHeaders(Boolean sendEnvelope, Boolean allowUnicode) at System.Net.Mail.Message.Send(BaseWriter writer, Boolean sendEnvelope, Boolean allowUnicode) at System.Net.Mail.SmtpClient.Send(MailMessage message)

在我的主题/这些字符都很好,但不是在电子邮件地址。

These characters in my subject/body are fine, but not in email addresses.

我试过设置 SmtpClient.DeliveryMethod = SmtpDeliveryFormat.International MAILMESSAGE。 HeadersEncoding = Encoding.Unicode (或 UTF8 ),它似乎并没有改变任何东西。这些都是我们需要所以这是一个有点问题进行沟通的人真正的电子邮件地址。

I've tried setting SmtpClient.DeliveryMethod = SmtpDeliveryFormat.International, or MailMessage.HeadersEncoding = Encoding.Unicode (or UTF8) and it doesn't seem to change anything. These are real email addresses of people we need to communicate with so it's a bit of a problem.

我已经挖通过.NET源代码,但任何地方没有真正得到。我找到了一个 ServerSupportsEai 属性,谷歌告诉我EAI代表邮箱地址国际化(的 https://tools.ietf.org/html/rfc5336 ),但它是相当不清楚,如果这是在我的代码的限制,或者如果我说的是特定的服务器,只是不支持这个.. !因为我使用的是测试服务器,以避免发送电子邮件到不知情的瑞典人民

I have been digging through the .Net source code but not really gotten anywhere. I tracked down a ServerSupportsEai property, and Google tells me EAI stands for Email Address Internationalisation (https://tools.ietf.org/html/rfc5336) but it's fairly unclear if this is a limitation in my code, or if the specific server I'm talking to just doesn't support this... since I'm using a test server to avoid sending emails to unsuspecting Swedish people!

有人可以帮助我清楚这件事 - 不支持.NET这一点,如果有啥如果有什么要我的客户端代码就启用它?

Can someone help me clear this up - does .Net support this, if so what if anything should my client code do to enable it?

推荐答案

要能够根据发送UTF-8字符<一HREF =https://tools.ietf.org/html/rfc6531相对=nofollow> RFC6531 客户端和服务器需要支持它。

To be able to to send UTF-8 characters according to RFC6531 both client and server need to support it.

如果您使用 SmtpClient 您需要将目标框架4.5版本,因为这实现支持SMTPUTF8扩展。

If you use the .NET implementation of SmtpClient you'll need to target framework version 4.5 because that implementation supports the SMTPUTF8 extension.

如果您设置的 DeliveryFormat 属性,你做了什么是需要你的客户端支持UTF8字符:

If you set the DeliveryFormat property you have done what is needed for your client to support UTF8 characters:

using (var smtp = new SmtpClient())
{
    smtp.DeliveryFormat = SmtpDeliveryFormat.International;

    var message = new MailMessage(
        "my.email@gmail.com",
        "ëçïƒÖ@example.com",
        "UTF8",
        "Is UTF8 supported?");

    smtp.Send(message);
}

如果我们反向工程发送方法,我们可以很容易地遵循你的问题的堆栈跟踪。你会发现这个实现:!

If we reverse-engineer the Send method we can easily follow the stack-trace from your question. You'll find this implementation:

if (!allowUnicode && !MimeBasePart.IsAscii(this.userName, true))
{
    throw new SmtpException(SR.GetString("SmtpNonAsciiUserNotSupported", new object[]
    {
        this.Address
    }));
}

allowUnicode 布尔从发送方法被提供:

if (this.DeliveryMethod == SmtpDeliveryMethod.Network)
{
    return this.ServerSupportsEai && this.DeliveryFormat == SmtpDeliveryFormat.International;
}



现在,这里是在图片中的服务器。什么时候私人布尔 ServerSupportsEai 为真?事实证明,在发送 EHLO 命令 SmptConnection 要求 ParseExtensions

Now here comes the server in the picture. When does the private boolean ServerSupportsEai becomes true? It turns out that on sending the EHLO command the SmptConnection calls ParseExtensions:

if (string.Compare(text, 0, "SMTPUTF8", 0, 8, StringComparison.OrdinalIgnoreCase) == 0)
{
    ((SmtpPooledStream)this.pooledStream).serverSupportsEai = true;
}

如果你想知道的前期,如果你的邮件服务器支持该扩展可以只需用Telnet客户端(我用的腻子)连接到您的SMTP服务器并发送 EHLO somename 命令和检查结果。

If you want to know upfront if your mail server is supporting that extension you can simply connect with a telnet client (I use putty) to your smtp server and send the EHLO somename command and inspect the results.

连接到 smtp.gmail.com 在端口587上给了我这样的输出:

Connecting to smtp.gmail.comon port 587 gives me this output:

220 smtp.gmail.com ESMTP hx10sm19521922wjb.25 - gsmtp
EHLO fubar
250-smtp.gmail.com at your service, [2001:FFF:8bef:1:34d6:a247:FFFF:4620]
250-SIZE 35882577
250-8BITMIME
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-CHUNKING
250 SMTPUTF8

注意最后一行:它拥有我们所需的SMTPUTF8

Notice the last line: it holds our required SMTPUTF8

TL;博士

要能够使用国际字符在emailaddres请务必将 DeliveryFormat SmtpDeliveryFormat.International ,并使用支持SMTPUTF8扩展,通告在其 EHLO 命令。

To be able to use international characters in the emailaddres make sure to set the DeliveryFormatto SmtpDeliveryFormat.International and use an smtp server that supports the SMTPUTF8 extension and advertises it in its EHLO command.

这篇关于无法发送电子邮件与斯堪的纳维亚的字符地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆