Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net System.Mail.Message adding multiple "To" addresses

EDIT: This question is pointless, except as an exercise in red herrings. The issue turned out to be a combination of my idiocy (NO ONE was being emailed as the host was not being specified and was incorrect in web.config) and the users telling me that they sometimes got the emails and sometimes didn't, when in reality they were NEVER getting the emails.

So, instead of taking proper steps to reproduce the problem in a controlled setting, I relied on user information and the "it works on my machine" mentality. Good reminder to myself and anyone else out there who is sometimes an idiot.


I just hit something I think is inconsistent, and wanted to see if I'm doing something wrong, if I'm an idiot, or...

MailMessage msg = new MailMessage(); msg.To.Add("[email protected]"); msg.To.Add("[email protected]"); msg.To.Add("[email protected]"); msg.To.Add("[email protected]"); 

Really only sends this email to 1 person, the last one.

To add multiples I have to do this:

msg.To.Add("[email protected],[email protected],[email protected],[email protected]"); 

I don't get it. I thought I was adding multiple people to the To address collection, but what I was doing was replacing it.

I think I just realized my error -- to add one item to the collection, use .To.Add(new MailAddress("[email protected]"))

If you use just a string, it replaces everything it had in its list. EDIT: Other people have tested and are not seeing this behavior. This is either a bug in my particular version of the framework, or more likely, an idiot maneuver by me.

Ugh. I'd consider this a rather large gotcha! Since I answered my own question, but I think this is of value to have in the stackoverflow archive, I'll still ask it. Maybe someone even has an idea of other traps that you can fall into.

like image 568
Matt Dawdy Avatar asked Mar 08 '10 20:03

Matt Dawdy


People also ask

How do I send one message to multiple addresses at the same time?

The BCC method The BCC (Blind Carbon Copy) method is the most common approach to send emails to multiple recipients at the same time. Emailing to multiple recipients using the BCC feature hides other recipients from the recipient, making it look like he is the sole recipient of the email.

How do I send to multiple email addresses in SMTP?

If you want to use smtplib to send email to multiple recipients, use email. Message. add_header('To', eachRecipientAsString) to add them, and then when you invoke the sendmail method, use email.

How send email to multiple recipients in MVC?

If you are adding multiple recipient Email Ids then add the multiple Email Ids separated by commas (,) because in the email function we are splitting the Email Ids with commas (,), If you want some other logic to add or split Email Ids then make the relevant changes in the Email function.


1 Answers

I wasn't able to replicate your bug:

var message = new MailMessage();  message.To.Add("[email protected]"); message.To.Add("[email protected]");  message.From = new MailAddress("[email protected]"); message.Subject = "Test"; message.Body = "Test";  var client = new SmtpClient("localhost", 25); client.Send(message); 

Dumping the contents of the To: MailAddressCollection:

MailAddressCollection (2 items)
DisplayName User Host Address

user example.com [email protected]
user2 example.com [email protected]

And the resulting e-mail as caught by smtp4dev:

Received: from mycomputername (mycomputername [127.0.0.1])      by localhost (Eric Daugherty's C# Email Server)      3/8/2010 12:50:28 PM MIME-Version: 1.0 From: [email protected] To: [email protected], [email protected] Date: 8 Mar 2010 12:50:28 -0800 Subject: Test Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable  Test 

Are you sure there's not some other issue going on with your code or SMTP server?

like image 136
Lance McNearney Avatar answered Sep 29 '22 20:09

Lance McNearney