locked
Silly question about a foreach and emails.... RRS feed

  • Question

  • User692055318 posted

    Hi there,

    In my seemingly never-ending quest to get the notifications app to work....now the specs have changed again and the admins want me to use the Exchange API instead. Ok thats fine. I got that to work but I have been playing with this thing for so long I can't even tell how to make a foreach work anymore!

    It is taking the test data emails and just incrementing them the TO list and sending an email. So email1 gets one email, then email1 and email2 get an email then email1, email2 and email3 get an email.

    Can someone take a quick peek and tell me where the sMessage.send  should be in this foreach statement?

    var sCount = 0;
    
                     foreach(var item in VendorEmails)
                     {  
                         sCount = counter + 1;
                         sMessage.ToRecipients.Add(item.Key);
                            if (sCount == 1) {
                                sMessage.CcRecipients.Add(primaryTo);  // Note that we are only sending to the primaryTo address for the first batch, not every one
                                } 
                         sMessage.Subject = subjectLine;
                                
                            // Began building your message
                            var messageBuilder = new StringBuilder(customerRequest + "<br /><br />" + message1 + "<br /><br />");
                            
                            // Add the items
                               foreach (var product in item.Where(p => p.email == item.Key)) 
                                  {      
                                   // Add the product numbers to the message
                                   messageBuilder.AppendFormat("{0}<br />",product.customername +": "+ product.servicenumber);
    
                                   //Insert the emails that are sent into the database so they can see who it was sent to.
                                   var insertCommand = "INSERT INTO NotificationSent (Email,DateSent) Values(@0, @1)";
                                   dbLocal.Execute(insertCommand,item.Key, currentDateTime);
                                   }   
                         
                  
                         // Set the body content and send the message
                         sMessage.Body = messageBuilder.ToString();
    
                         sMessage.Send();
                         System.Threading.Thread.Sleep(2000); // Wait for 2 seconds
                       } 

    Thanks for any help.

    Laura

    Monday, April 27, 2015 2:17 PM

Answers

  • User1526116210 posted

    Can someone take a quick peek and tell me where the sMessage.send  should be in this foreach statement?

    The problem is that you are sending the email on each iteration of the loop. To answer your question, it shouldn't be in the loop but after the loop. Like this:

    var sCount = 0;
    foreach(var item in VendorEmails)
    {  
        sCount = counter + 1;
        sMessage.ToRecipients.Add(item.Key);
        if (sCount == 1) {
            sMessage.CcRecipients.Add(primaryTo);  // Note that we are only sending to the primaryTo address for the first batch, not every one
        } 
        sMessage.Subject = subjectLine;
    
        // Began building your message
        var messageBuilder = new StringBuilder(customerRequest + "<br /><br />" + message1 + "<br /><br />");
    
        // Add the items
        foreach (var product in item.Where(p => p.email == item.Key)) 
        {      
            // Add the product numbers to the message
            messageBuilder.AppendFormat("{0}<br />",product.customername +": "+ product.servicenumber);
    
            //Insert the emails that are sent into the database so they can see who it was sent to.
            var insertCommand = "INSERT INTO NotificationSent (Email,DateSent) Values(@0, @1)";
            dbLocal.Execute(insertCommand,item.Key, currentDateTime);
        }   
                         
                  
        // Set the body content and send the message
        sMessage.Body = messageBuilder.ToString();
    
        System.Threading.Thread.Sleep(2000); // Wait for 2 seconds
    }
    sMessage.Send();

    But you should really be sending a different email to everyone instead of having every recipient added to the same email.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 19, 2015 11:42 PM

All replies

  • User879242426 posted

    Can someone take a quick peek and tell me where the sMessage.send  should be in this foreach statement?

    It seems that this is fine using the code above, I wonder why you have doubt with sMessage.send, can you give some explanation? thanks

    Thursday, April 30, 2015 3:21 AM
  • User692055318 posted

    It is not. What happens is it does not send one email per person it keeps incrementing the emails.  So if I have 3 emails:

    laura1@myemail.com

    laura2@myemail.com

    laura3@myemail.com

    Laura1 gets an email then the second time around Laura1 and Laura2 get an email and the third time through Laura1, Laura2 and Laura3 get an email. So its obviously not right.

    Thursday, April 30, 2015 8:20 AM
  • User753101303 posted

    Hi,

    Your loop keeps working on the same message object again and again. As you change the Subject and Body properties this is not visible but as you only *ADD* to current recipients then more and more users are getting the mail.

    Either use a sMessage=new YourMessageClass(); inside the loop rather than before the loop to work on a new message each time or you likely have a ToRecipients.Clear() and CcRecipients.Clear() method to clear the current recipients before adding other recipients (not sure but it seems a custom or 3rd party message class rather than the usual MailMessage class).

    Monday, May 18, 2015 9:26 AM
  • User1526116210 posted

    Can someone take a quick peek and tell me where the sMessage.send  should be in this foreach statement?

    The problem is that you are sending the email on each iteration of the loop. To answer your question, it shouldn't be in the loop but after the loop. Like this:

    var sCount = 0;
    foreach(var item in VendorEmails)
    {  
        sCount = counter + 1;
        sMessage.ToRecipients.Add(item.Key);
        if (sCount == 1) {
            sMessage.CcRecipients.Add(primaryTo);  // Note that we are only sending to the primaryTo address for the first batch, not every one
        } 
        sMessage.Subject = subjectLine;
    
        // Began building your message
        var messageBuilder = new StringBuilder(customerRequest + "<br /><br />" + message1 + "<br /><br />");
    
        // Add the items
        foreach (var product in item.Where(p => p.email == item.Key)) 
        {      
            // Add the product numbers to the message
            messageBuilder.AppendFormat("{0}<br />",product.customername +": "+ product.servicenumber);
    
            //Insert the emails that are sent into the database so they can see who it was sent to.
            var insertCommand = "INSERT INTO NotificationSent (Email,DateSent) Values(@0, @1)";
            dbLocal.Execute(insertCommand,item.Key, currentDateTime);
        }   
                         
                  
        // Set the body content and send the message
        sMessage.Body = messageBuilder.ToString();
    
        System.Threading.Thread.Sleep(2000); // Wait for 2 seconds
    }
    sMessage.Send();

    But you should really be sending a different email to everyone instead of having every recipient added to the same email.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 19, 2015 11:42 PM