locked
group emails together RRS feed

  • Question

  • User692055318 posted

    Hello,

    I need to be creative about how I use System.Net.Mail. I have it working now, meaning for a set of conditions it sets up the To, CC and Bcc fields and determines the body structure of the message. I do have it working but I would like it not to add the To, three times if I have 3 values in the database.

    So what happens now is I have a test database with 2 vendors with 3 values each and 2 customers. What I would like it to do it only append one email to the To, or Bcc field. So right now when I open the email I see:

    To: Laura Smith; Laura Smith; Laura Smith
    CC: Customer Service; Customer Service; Customer Service

    I only want it to enter it once. I know it has everything to do with the foreach loop I created. And as I said it does work, I'm just trying to be neater about it. 

    Thanks

    Laura

    message.Subject = subjectLine;
    
    //set up the To, CC and Bcc fields based on whether it is vendor or customer foreach (var product in item.Where(p => p.email == item.Key)) { if (product.vendors == "None") { message.Bcc.Add(new MailAddress(product.email)); // Note that we are only sending to the primaryTo address for the first batch, not every one if (i == 0) {message.To.Add(primaryTo);} } else { message.To.Add(new MailAddress(product.email)); message.CC.Add(primaryTo); } }
    Tuesday, March 3, 2015 10:37 AM

Answers

  • User1508394307 posted

    I only want it to enter it once. I know it has everything to do with the foreach loop I created. And as I said it does work, I'm just trying to be neater about it. 

    To avoid duplicates you either need to check if address was already added or do not add addresses in the loop, but use a list or other collection which you could sort/distinct after that and use to send the destinct addresses.

    1) To check if address was already added 

    instead

    if (i == 0) {message.To.Add(primaryTo);}               
                 }

    do

    if (i == 0 && !message.To.Contains(new MailAddress(primaryTo)))
        message.To.Add(primaryTo);                
    

    Demo: https://dotnetfiddle.net/AGqx4p 

    2) Do not add addresses in the loop but use a list

    instead 

    if (i == 0) {message.To.Add(primaryTo);}               
                 }

    do

    List<string> ToList = new List<string>();
    List<string> CcList = new List<string>();
    List<string> BccList = new List<string>();
    
    ...
    
    foreach (var product in item.Where(p => p.email == item.Key)) 
    {
        ...
        ToList.Add(primaryTo); // add to list
        ...
    }		
    
    ...
    
    ToList = ToList.Distinct().ToList(); // create distinct list 
    
    foreach(var i in ToList)
       ToList.Add(i);  // add only unique items

    Demo: https://dotnetfiddle.net/3mXpQd 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 17, 2015 4:54 AM

All replies

  • User-1087493963 posted

    message.Subject = subjectLine;
    //set up the To, CC and Bcc fields based on whether it is vendor or customer foreach (var product in item.Where(p => p.email == item.Key)) { if (product.vendors == "None") { message.Bcc.Add(new MailAddress(product.email)); // Note that we are only sending to the primaryTo address for the first batch, not every one if (i == 0) {message.To.Add(primaryTo);} } else { message.To.Add(new MailAddress(product.email)); message.CC.Add(primaryTo); } }

    This is very neater code, I don't think that there is better way to do that.

    Saturday, March 7, 2015 1:33 AM
  • User692055318 posted

    Not neater for the code. I only want to send one email to the To address not 5.

    Wednesday, March 11, 2015 10:49 AM
  • User-166373564 posted

    if (product.vendors == "None") { message.Bcc.Add(new MailAddress(product.email)); // Note that we are only sending to the primaryTo address for the first batch, not every one if (i == 0) {message.To.Add(primaryTo);} } else { message.To.Add(new MailAddress(product.email)); message.CC.Add(primaryTo); } }

    According to your description above, you can use code snippet like below to send email using system.net.net:

                // To address collection of MailAddress
                message.To.Add("admin1@yoursite.com");
                message.Subject = "Feedback";

                // CC and BCC optional
                // MailAddressCollection class is used to send the email to various users
                // You can specify Address as new MailAddress("admin1@yoursite.com")
                message.CC.Add("admin1@yoursite.com");
                message.CC.Add("admin2@yoursite.com");
    Hope it helps.

    Regards,

    Angie

    Tuesday, March 17, 2015 3:51 AM
  • User1508394307 posted

    I only want it to enter it once. I know it has everything to do with the foreach loop I created. And as I said it does work, I'm just trying to be neater about it. 

    To avoid duplicates you either need to check if address was already added or do not add addresses in the loop, but use a list or other collection which you could sort/distinct after that and use to send the destinct addresses.

    1) To check if address was already added 

    instead

    if (i == 0) {message.To.Add(primaryTo);}               
                 }

    do

    if (i == 0 && !message.To.Contains(new MailAddress(primaryTo)))
        message.To.Add(primaryTo);                
    

    Demo: https://dotnetfiddle.net/AGqx4p 

    2) Do not add addresses in the loop but use a list

    instead 

    if (i == 0) {message.To.Add(primaryTo);}               
                 }

    do

    List<string> ToList = new List<string>();
    List<string> CcList = new List<string>();
    List<string> BccList = new List<string>();
    
    ...
    
    foreach (var product in item.Where(p => p.email == item.Key)) 
    {
        ...
        ToList.Add(primaryTo); // add to list
        ...
    }		
    
    ...
    
    ToList = ToList.Distinct().ToList(); // create distinct list 
    
    foreach(var i in ToList)
       ToList.Add(i);  // add only unique items

    Demo: https://dotnetfiddle.net/3mXpQd 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 17, 2015 4:54 AM
  • User692055318 posted

    Thanks so much. Just what I was looking for.

    Laura

    Tuesday, March 17, 2015 8:36 AM