Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi team

I want to implement sending email to multiple users and currently i have tried the following logic and its not working.

What I have tried:

ASP.NET
<pre>
        // Sending email notification to a user.

        public static class EmailNotificationToUser
        {
            public static void SendEmail(string htmlString,string emailGrp, string ToEmail)
            {
                try
                {
                    MailMessage message = new MailMessage();
                    SmtpClient smtp = new SmtpClient();
                    message.From = new MailAddress("gcobani.mkontwana@s4.co.za");
                    List<MailAddress> addresses = new List<MailAddress>();
                    string[] emilGrp = ToEmail.Split(',');
                    foreach(string Multiemail in emailGrp)
                    {
                        message.To.Add(new MailAddress(Multiemail));
                    }

                   
                    message.To.Add(emailGrp);
                    message.Subject = "ShopFloor AMS Incident Report";
                    message.IsBodyHtml = true;
                    message.Body = htmlString;
                    smtp.Port = 587;
                    smtp.Host = "smtp.gmail.com";
                    smtp.EnableSsl = true;
                    smtp.UseDefaultCredentials = false;
                    smtp.Credentials = new NetworkCredential("entsagcobza2020@gmail.com", "Telkom800@@");
                    smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                    smtp.Send(message);


                }catch(Exception ex)
                {

                }
            }
        }
Posted
Updated 21-Nov-21 22:12pm
Comments
CHill60 22-Nov-21 4:05am    
"Not working" is not helpful information - describe what happens (or does not happen)
Richard MacCutchan 22-Nov-21 4:20am    
You add the individual addresses to the To set, but then you try to add the array of addresses emailGrp, which may break the message.

1 solution

Don't do it like that: it's probably in breach of GDPR regulations, where an email address is "personal information" and must be safeguarded under penalty of huge fines.

When you use multiple "To" addresses, you send the email address of everyone in the collection to everyone in the list: all recipients can read everybody else's email address. That's a bad practice, and can literally bankrupt even large companies.

Instead, either send individual messages (slow and painful) or send it to a "dummy address" and add all recipients as BCC (Blind Carbon Copy) addresses. Either way ensures data protection.

In your code however, you attempt to add each address twice: once in the loop, and a second time immediately after. The latter is what is causing your compilation problem: there is no overload of the Add method which accepts an array of strings.
If you look at the documentation, the Add method accepts a single sting which can contain multiple recipients separated by commas anyway ... so your whole "Split and loop" code is redundant ... MailAddressCollection.Add(String) Method (System.Net.Mail) | Microsoft Docs[^]
 
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