none
send email to the user once RRS feed

  • Question

  • Hi,

    I am using the following code to lop through contracts and send email to concern people. Same contract will have multiple people like a hardware contract should have IT and purchase emails in it. I am able to get the emails but the problem if there are two emails in the contract (john & smith) it will send two emails to john and two emails to smith.

    John will receive the email saying Dear John and anther email saying Dear Smith.. similarly with Smith!

    here is my code:

    while (sql_reader.Read())
    {
        contract_id = Convert.ToInt32(sql_reader["contract_id"]);
    
        mail.To.Add(sql_reader["reminder_email"].ToString());
        mail.Subject = sql_reader["contract_name"].ToString();
        mail.Body = email_body.ToString();
    
        SmtpServer.Port = 587;
        SmtpServer.Credentials = new System.Net.NetworkCredential("it@mydomain.com", "mypassword");
        SmtpServer.EnableSsl = true;
    
        SmtpServer.Send(mail);
    }
    

    Monday, August 25, 2014 10:03 AM

Answers

  • Clearly there is some missing code, but what you have shown us above appears to be using the same "mail" object each time through the loop. So the second time through the loop you are adding a second person to "mail.To" so the mail will be sent to the first person and the second person.
    Monday, August 25, 2014 4:20 PM

All replies

  • You could keep track of which addresses you have already sent en email to using a List<string>:

                List<string> alreadyProcessedEmails = new List<string>();
                while (sql_reader.Read())
                {
                    contract_id = Convert.ToInt32(sql_reader["contract_id"]);
    
                    string email = sql_reader["reminder_email"].ToString();
                    if (!alreadyProcessedEmails.Contains(email))
                    {
    
                        mail.To.Add(sql_reader["reminder_email"].ToString());
                        mail.Subject = sql_reader["contract_name"].ToString();
                        mail.Body = email_body.ToString();
    
                        SmtpServer.Port = 587;
                        SmtpServer.Credentials = new System.Net.NetworkCredential("it@mydomain.com", "mypassword");
                        SmtpServer.EnableSsl = true;
    
                        SmtpServer.Send(mail);
    
                        alreadyProcessedEmails.Add(email);
                    }
                }
    

    The other option is to make sure that you only select unique email addresses from the database in the first place.

    Monday, August 25, 2014 10:22 AM
  • I tried it but still have emails sent to the same user more than one time

    here is the code:

    List<string> alreadyProcessedEmails = new List<string>();

    while (sql_reader.Read())
    {
        contract_id = Convert.ToInt32(sql_reader["contract_id"]);
    
        email = sql_reader["reminder_email"].ToString();
    
        if (!alreadyProcessedEmails.Contains(email))
        {
            StringBuilder email_body = new StringBuilder();
    
            mail.To.Add(sql_reader["reminder_email"].ToString());
            // mail.To.Add("jassim@meskholdings.com");
            mail.Subject = sql_reader["contract_name"].ToString();
            mail.Body = email_body.ToString();
    
            SmtpServer.Port = 587;
            SmtpServer.Credentials = new System.Net.NetworkCredential("it@web.com", "pass");
            SmtpServer.EnableSsl = true;
    
            SmtpServer.Send(mail);
    
            alreadyProcessedEmails.Add(email);
        }
    }
    

    Monday, August 25, 2014 12:56 PM
  • Clearly there is some missing code, but what you have shown us above appears to be using the same "mail" object each time through the loop. So the second time through the loop you are adding a second person to "mail.To" so the mail will be sent to the first person and the second person.
    Monday, August 25, 2014 4:20 PM