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.