电子邮件地址中的撇号导致SmtpException

时间:2017-11-13 16:41:12

标签: .net smtpclient apostrophe mailaddress

我们的业务应用程序为许多组织提供服务,其中一些组织在其电子邮件地址(本地部分)中具有撇号,这在技术上是有效的。

但是,如果FROM或TO电子邮件地址中包含撇号(本地部分),我就无法发送电子邮件,如下所示:

    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)

谢谢, 克里斯。

1 个答案:

答案 0 :(得分:0)

如果您将投放格式设置为国际格式,则可以使用。像这样:

    SmtpClient client = new SmtpClient(SMTP_HOST) {
        Credentials = CredentialCache.DefaultNetworkCredentials,
        DeliveryFormat = SmtpDeliveryFormat.International
    };