Click here to Skip to main content
15,917,968 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
protected void btnSend_Click(object sender, EventArgs e)
{
toEmail = txtToMail.Text;
EmailSubj = Convert.ToString(txtSub.Text);
EmailMsg = Convert.ToString(txtMes.Text);

SendEmail(toEmail, EmailSubj, EmailMsg);
}


I am sending 3 mails from a form with the following text boxes

1. txtaToMail.Text;
2. txtbToMail.Text;
3. txtcToMail.Text;

If any of the test mails is empty it should be ignored.

toEmail = txtToMail.Text;


How do I organise the mail system in a loop to cater for the 3 mail adresses.

The message is the same.

The subject the same.


Please assist.


Thanks

What I have tried:

Reviewed a number of works . All are for one mail.
Posted
Updated 26-Sep-16 18:48pm
v2

try this

C#
protected void btnSend_Click(object sender, EventArgs e)
       {

           EmailSubj = Convert.ToString(txtSub.Text);
           EmailMsg = Convert.ToString(txtMes.Text);

            string[] emails = new string[] { txtaToMail.Text, txtbToMail.Text, txtcToMail.Text };
           foreach (var email in emails)
           {
               if(!string.IsNullOrWhiteSpace(email))
                   SendEmail(email, EmailSubj, EmailMsg);
           }

       }
 
Share this answer
 
Comments
Garth J Lancaster 27-Sep-16 0:45am    
nice :-)
Karthik_Mahalingam 27-Sep-16 0:52am    
:-)
If you are able to send one email to many different addresses, you can skip the loop and just send a single email.

The below should work. It would create a send to address of something like

myemail1@aol.com;myemail2@aol.com

Which is the format for sending a single email to multiple people.

C#
protected void btnSend_Click(object sender, EventArgs e)
           {
 
               EmailSubj = Convert.ToString(txtSub.Text);
               EmailMsg = Convert.ToString(txtMes.Text);
 
                var emails = new string[] { txtaToMail.Text, txtbToMail.Text, txtcToMail.Text };
                var sanitizedEmails = emails.Where(m => !string.IsNullOrEmpty(m));
                var singleEmail = string.Join(";", sanitizedEmails);

                SendEmail(singleEmail, EmailSubj, EmailMsg);
           }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900