ASP.NET emailing to multiple emails
-
13-06-2021 - |
Question
I have this code here...
MailAddress from = new MailAddress("noreply@fakeemail.com", "IPC Orders");
MailAddress to = new MailAddress("email1@fakeemail.com.com");
MailMessage mail = new MailMessage(from, to);
mail.To.Add("email2@fakeemail.com");
mail.To.Add("email3@fakeemail.com");
Obviously this is not the full code, but when I try to send an email to multiple email address is doesnt send, if I comment out these two lines...
mail.To.Add("email2@fakeemail.com");
mail.To.Add("email3@fakeemail.com");
It works and will send it to the first email MailAddress to = new MailAddress("email1@fakeemail.com.com");
Whats wrong with my code
Solution
USE AddressCollection
FOR ADDING MULTIPLE TO
ADDRESSES
LIKE
mail.To = new AddressCollection( "email2@fakeemail.com, email3@fakeemail.com");
OTHER TIPS
you can try to add add all your email addresses to a list, then just iterate over that list and send a mail at each element
List<string> emailAddress = new List<string>();
emailAddress.add("email1@em.com");
emailAddress.add("email2@em.com"); // ... etc
foreach (string email in emailAddress)
{
MailMessage mail = new MailMessage(from, email);
//+ more stuff
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow