Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send Email to multiple Recipients with MailMessage?

Tags:

c#

mailmessage

I have multiple email recipients stored in SQL Server. When I click send in the webpage it should send email to all recipients. I have separated emails using ;.

Following is the single recipient code.

MailMessage Msg = new MailMessage(); MailAddress fromMail = new MailAddress(fromEmail); Msg.From = fromMail; Msg.To.Add(new MailAddress(toEmail));  if (ccEmail != "" && bccEmail != "") {     Msg.CC.Add(new MailAddress(ccEmail));     Msg.Bcc.Add(new MailAddress(bccEmail)); }  SmtpClient a = new SmtpClient("smtp server name"); a.Send(Msg); sreader.Dispose(); 
like image 970
Chetan Goenka Avatar asked May 06 '14 01:05

Chetan Goenka


People also ask

How do I send an email to multiple recipients?

Click the compose box, after composing your message, click on BCC and add all your recipients. This will send the emails to the recipients keeping email addresses hidden from each other.

How to send Mail to multiple recipients in asp net c#?

In the preceding class file I used a foreach loop to add the multiple recipient's Email Ids but before that we are using the comma (,) separated input email ids from user in one string "Tomail" then we are splitting the input sting with (,) commas and added to one string array named "Multi" then using a foreach loop we ...

How do I send multiple emails 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. Message.


1 Answers

Easy!

Just split the incoming address list on the ";" character, and add them to the mail message:

foreach (var address in addresses.Split(new [] {";"}, StringSplitOptions.RemoveEmptyEntries)) {     mailMessage.To.Add(address);     } 

In this example, addresses contains "[email protected];[email protected]".

like image 117
Brendan Green Avatar answered Sep 21 '22 08:09

Brendan Green