2

Our business application serves many organisations and some of those have apostrophes in their email address (local part) which is technically valid.

However, I cannot send out emails if the FROM or TO email address contains an apostrophe in them (local part) as per this C# example:

    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}");
        }
    }

Exception details when to email address contains an apostrophe:

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

Exception details when from email address contains an apostrophe:

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

Emails are fine when neither contain an apostrophe. Any advise would be appreciated.

EDIT: @JamesThorpe commented below about testing with telnet. I have two production servers which use hmailserver and mdaemon and a dev environment that uses a roll-your-own smtp bin. All three succeeded in accepting and relaying emails containing apostrophes and my roll-your-own one (in the debugger) never received the command (MAIL FROM/RCPT TO) that contained an email address with an apostrophe when using SMTPClient so I am sure this is a .NET SMTPClient issue.

Here are example telnets that worked (test.com is a placeholder for what I actually used):

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)

Thanks, Chris.

Chris Walsh
  • 3,423
  • 2
  • 42
  • 62
  • Looks like the issue is on whatever mailserver you're using rather than your code itself. You could [send an email using telnet](https://technet.microsoft.com/en-us/library/aa995718(v=exchg.65).aspx) to verify that and make sure .NET isn't mangling the addresses internally. – James Thorpe Nov 13 '17 at 16:44
  • On our two production environments we are using *mdaemon* and *hmailserver* and my development environment is using my own "SMTP Dummy Server" which receive the `MAIL FROM` and `RCPT TO` commands but only if they don't contains an apostrophe (it receives neither if an apostrophe-based email is present) so I don't think it's SMTP Server side. – Chris Walsh Nov 13 '17 at 16:53
  • I'll try the telnet approach, didn't think of that... So, on my development environment, I do indeed get an error response: `MAIL FROM: chris.o'connor@test.com 501 Error in parameters. Syntax:{MAIL FROM:
    [SIZE=msgSize] [BODY=8BITMIME]}`. Surely most mail servers should accept an apostrophe email address if it is part of RFC and not one of the more obscure email formats at that?
    – Chris Walsh Nov 13 '17 at 16:53
  • Does it not work on your production environments either then? You mention your dev one is _my own "SMTP Dummy Server"_ - is the issue confined to that only? – James Thorpe Nov 13 '17 at 17:04
  • Both production servers were able to push out an email with apostrophes via telnet. (They ended up in our spam filter due to minimal email header info but I pushed them through there and they reached my mailbox). I'm sure this is something to do with `SMTPClient` as all three environments have worked to a point via `telnet`. I'll add my telnet findings to the bottom of my question. – Chris Walsh Nov 13 '17 at 17:35
  • I posted this problem about 18 months ago and I am still getting the problem (production and dev) - and for clarify, the exception IS raised before any communication with the MTA so it must be internal to .NET Framework. – Chris Walsh May 30 '19 at 14:32

1 Answers1

0

If you set delivery format to international it should work. Something like:

    SmtpClient client = new SmtpClient(SMTP_HOST) {
        Credentials = CredentialCache.DefaultNetworkCredentials,
        DeliveryFormat = SmtpDeliveryFormat.International
    };
N. Little
  • 69
  • 9