【发布时间】:2017-11-13 16:41:12
【问题描述】:
我们的业务应用程序为许多组织提供服务,其中一些组织的电子邮件地址(本地部分)中有撇号,这在技术上是有效的。
但是,如果 FROM 或 TO 电子邮件地址中包含撇号(本地部分),则根据此 C# 示例,我无法发送电子邮件:
private const string SMTP_HOST = "localhost";
public static void CreateCopyMessage2()
{
// var from = new MailAddress("john.o'connor@contoso.com", "John O'Connor");
var from = new MailAddress("john.smith@contoso.com", "John Smith");
var to = new MailAddress("jane.o'leary@contoso.com", "Jane O'Leary");
// var to = new MailAddress("jane.smith@contoso.com", "Jane Smith");
var message = new MailMessage(from, to) {
Subject = "Email Subject",
Body = "Email Body Text"
};
SmtpClient client = new SmtpClient(SMTP_HOST) {
Credentials = CredentialCache.DefaultNetworkCredentials
};
try
{
client.Send(message);
}
catch (SmtpException ex)
{
Console.WriteLine($"Exception caught: {ex}");
}
}
to 电子邮件地址包含撇号时的异常详情:
System.Net.Mail.SmtpException: Syntax error in parameters or arguments. The server response was: Error in parameters. Syntax:{RCPT TO:<address> [SIZE=msgSize]}
at System.Net.Mail.RecipientCommand.CheckResponse(SmtpStatusCode statusCode, String response)
at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception)
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at EmailWithApostropheTest.Program.Main() in ....\Program.cs:line 65
from 电子邮件地址包含撇号时的异常详情:
System.Net.Mail.SmtpException: Syntax error in parameters or arguments. The server response was: Error in parameters. Syntax:{MAIL FROM:<address> [SIZE=msgSize] [BODY=8BITMIME]}
at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response)
at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, MailAddress from, Boolean allowUnicode)
at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception)
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at EmailWithApostropheTest.Program.Main() in ....\Program.cs:line 65
电子邮件都可以不包含撇号。 任何建议将不胜感激。
编辑:@JamesThorpe 在下面评论了使用 telnet 进行测试。我有两个使用 hmailserver 和 mdaemon 的生产服务器和一个使用自己滚动的 smtp bin 的开发环境。这三个人都成功地接受和转发了包含撇号的电子邮件,而我自己滚动的电子邮件(在调试器中)在使用SMTPClient 时从未收到包含带有撇号的电子邮件地址的命令(MAIL FROM/RCPT TO),所以我确定这是 .NET SMTPClient 问题。
以下是有效的 telnet 示例(test.com 是我实际使用的占位符):
220 test.com ESMTP Mon, 13 Nov 2017 17:30:29 +0000
HELO test.com
250 test.com Hello test.com [127.0.0.1], pleased to meet you
MAIL FROM:noreply.o'connor@myorg.com
250 2.1.0 Sender OK
RCPT TO:chris.walsh@real-sense.com
250 2.1.5 Recipient OK
DATA
354 Enter mail, end with <CRLF>.<CRLF>
test 123 test 123 from elms
.
250 2.6.0 Ok, message saved
220 test2.net ESMTP
HELO test2.net
250 Hello.
MAIL FROM:chris.walsh@test.net
250 OK
RCPT TO:john.o'connor@test.com
250 OK
DATA
354 OK, send.
Test 123
.
250 Queued (4.531 seconds)
MAIL FROM:test.o'connor@activbase.net
250 OK
RCPT TO:my.real.name@my.real.org.com
250 OK
DATA
354 OK, send.
test123
test 123
.
250 Queued (3.203 seconds)
谢谢, 克里斯。
【问题讨论】:
-
看起来问题出在您使用的任何邮件服务器上,而不是您的代码本身。您可以send an email using telnet 进行验证并确保 .NET 不会在内部破坏地址。
-
在我们的两个生产环境中,我们使用 mdaemon 和 hmailserver 并且我的开发环境使用我自己的“SMTP 虚拟服务器”,它接收 @ 987654329@ 和
RCPT TO命令,但前提是它们不包含撇号(如果存在基于撇号的电子邮件,它也不会收到)所以我认为它不是 SMTP 服务器端。 -
我会尝试 telnet 方法,没想到...所以,在我的开发环境中,我确实收到了错误响应:
MAIL FROM: chris.o'connor@test.com 501 Error in parameters. Syntax:{MAIL FROM:<address> [SIZE=msgSize] [BODY=8BITMIME]}。当然,如果撇号电子邮件地址是 RFC 的一部分,而不是更晦涩的电子邮件格式之一,那么大多数邮件服务器肯定应该接受撇号电子邮件地址? -
那么它在您的生产环境中也不起作用吗?您提到您的开发者是我自己的“SMTP 虚拟服务器” - 问题仅限于此吗?
-
两台生产服务器都能够通过 telnet 推送带有撇号的电子邮件。 (由于电子邮件标题信息最少,它们最终进入了我们的垃圾邮件过滤器,但我将它们推到了那里,它们到达了我的邮箱)。我确信这与
SMTPClient有关,因为所有三个环境都通过telnet工作到一定程度。我会将我的 telnet 发现添加到问题的底部。
标签: .net smtpclient apostrophe mailaddress