0

I have a web app that needs to send automated emails to a group email address within our company. The address they've chosen happens to have an ampersand character in it -- the format is something like X-Y&Z@email.com.

C# does not want to send the email. Or, rather, it seems that it sends the email but it never arrives at the recipient's inbox. I assume the ampersand is causing the problem.

I have tried to handle it a number of ways:

"X-Y&Z@email.com"
@"X-Y&Z@email.com"
"X-Y%26Z@email.com"
"X-Y&Z@email.com"   // I didn't expect this one to work, and it threw an exception

We are using MailMessage and SmtpClient to handle automated emails.

Is there something different I should be doing to escape the ampersand character?

EDIT: The code:

public static void GenerateEmail(string from, List<string> to, string subject, string htmlBody) {        
    MailMessage message = new MailMessage(from, to.Aggregate((i, j) => i + "," + j));
    message.Subject = subject;

    [some code to prettify htmlBody and set message.Body]

    SmtpClient smtp = new SmtpClient([our company SMTP address]);
    smtp.Send(message);
}

List<string> to = new List<string> { "X-Y&Z@email.com" };
GenerateEmail("from@email.com", to, "Subject", "Body")

The code is pretty standard. I didn't write it, I'm just calling GenerateEmail. Our team uses it for a bunch of other things, and those other things work.

Furthermore, the group that uses this weird email with the ampersand has been using it for a while and hasn't had any problems in the past. I think this is the first time we're trying to send automated emails to the address, though -- usually it's just other users sending things via Outlook.

The SMTP server is our company server, hosted and run by our IT team. I have no access to the server or logs. Everything we use at this company is Microsoft -- Outlook/ActiveDirectory/Exchange.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
anthony_mcdougle
  • 276
  • 3
  • 14
  • 2
    Sorry but but you are saying that some ones worked and some ones did not? – Khalil Khalaf Aug 05 '16 at 14:50
  • 1
    Related http://stackoverflow.com/questions/2049502/what-characters-are-allowed-in-email-address –  Aug 05 '16 at 14:52
  • 1
    If even *one* such address worked, the problem is not C# but the the SMTP servers the messages passes through. If the message is sent from the application you should check the *server's* logs to find out what happened – Panagiotis Kanavos Aug 05 '16 at 14:52
  • How are you actually sending these? Show your code – Liam Aug 05 '16 at 14:53
  • 1
    Have you checked the *server's* logs? Does your code throw or do you hide the exception? – Panagiotis Kanavos Aug 05 '16 at 14:54
  • What SMTP server are you using also? – Liam Aug 05 '16 at 14:54
  • have you tried message.HeadersEncoding = System.Text.Encoding.UTF8; – Ashkan S Aug 05 '16 at 15:00
  • I have no access to the SMTP server. This is a piece of a web application for a large corporation, we have a whole division that handles the IT infrastructure, but it's all Microsoft (so Exchange). None of those examples that I used worked. The `&` example was the only one that threw an exception, though. I've updated the question with more information & code. – anthony_mcdougle Aug 05 '16 at 15:08
  • @anthony_mcdougle the addreses do *not* matter if the *server* stops those messages. That's why you *have* to ask what the IT guys allow or not. They may prevent certain characters for whatever reason. As for using `&` SMTP isn't a URL and `;` is *not* allowed. You can't escape characters as if this was a URL or HTML. Finally, *post your code* – Panagiotis Kanavos Aug 05 '16 at 15:22
  • Have you tried to send to individual addresses instead of putting all addreses in the same string ? BTW a single `String.Join(",", to)` would perform better (it uses a StringBuilder instead of generating temporary strings) and would look cleaner. – Panagiotis Kanavos Aug 05 '16 at 15:28
  • Finally, consider using Wireshark to check the conversation betwen your client and the server, [as shown in this question](http://stackoverflow.com/questions/27420321/how-can-we-log-the-smtp-conversation-with-smtpclient). You can enable tracing at the System.Net level as well but that is much more verbose – Panagiotis Kanavos Aug 05 '16 at 15:30

0 Answers0