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 hos
You can do this either with multiple System.Net.Mail.MailAddress
objects or you can provide a single string containing all of the addresses separated by commas
You could try putting the e-mails into a comma-delimited string ("person1@domain.com, person2@domain.com"
):
C#:
ArrayList arEmails = new ArrayList();
arEmails.Add("person1@domain.com");
arEmails.Add("person2@domain.com");
string strEmails = string.Join(", ", arEmails);
VB.NET if you're interested:
Dim arEmails As New ArrayList
arEmails.Add("person1@domain.com")
arEmails.Add("person2@domain.com")
Dim strEmails As String = String.Join(", ", arEmails)
I wasn't able to replicate your bug:
var message = new MailMessage();
message.To.Add("user@example.com");
message.To.Add("user2@example.com");
message.From = new MailAddress("test@example.com");
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 Addressuser example.com user@example.com
user2 example.com user2@example.com
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: test@example.com
To: user@example.com, user2@example.com
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?
private string FormatMultipleEmailAddresses(string emailAddresses)
{
var delimiters = new[] { ',', ';' };
var addresses = emailAddresses.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
return string.Join(",", addresses);
}
Now you can use it like
var mailMessage = new MailMessage();
mailMessage.To.Add(FormatMultipleEmailAddresses("test@gmail.com;john@rediff.com,prashant@mail.com"));
Thanks for spotting this I was about to add strings thinking the same as you that they'd get added to end of collection. It appears the params are:
msg.to.Add(<MailAddress>) adds MailAddress to the end of the collection
msg.to.Add(<string>) add a list of emails to the collection
Slightly different actions depending on param type, I think this is pretty bad form i'd have prefered split methods AddStringList of something.
Add multiple System.MailAdress object to get what you want.