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.