As you said, if it is goine to end less loop, it might send those many emails and that's why you are keep getting emails, eventhough you close the page, what ever the mails sent before will be transfered or sent to the corresponding email through SMTP!
ok, here is my code: (i finally stopped getting emails after receiving about 35,000, literally.)
public void TestGroupEmail()
{
SqlDataReader objDR;SqlConnection conds = new SqlConnection (ConfigurationSettings.AppSettings["con"]);
SqlCommand cmdNewsletter = new SqlCommand( "Select Email From EmailTest", conds); //there should be about 690 email addresses
conds.Open();
objDR = cmdNewsletter.ExecuteReader(System.Data.CommandBehavior.CloseConnection);int intSIZE = 50;
int intBLOCK = 0;
Hashtable ht = new Hashtable();while (objDR.Read())
{
for (int POS = 0; POS <= intSIZE; POS++)
{
intBLOCK += 1;
string strBLOCK = (objDR ["Email"] + ";");
strBLOCK = strBLOCK.Substring(0, strBLOCK.Length -1);
strBLOCK.Trim();ht.Add(intBLOCK.ToString(), strBLOCK );
}
}for (int i = 0; i <= ht.Count; i++)
{
System.Web.Mail.MailMessage objEmail = new System.Web.Mail.MailMessage();
objEmail.To = "************";
objEmail.From = "**********";
objEmail.Bcc = ht.Values.ToString();
objEmail.Subject = "This is the Test Email Subject." + ht.Keys.ToString(); //This actually doesnt gove me the value of the keys, either
objEmail.Body = "This is the test email body.";
objEmail.BodyFormat = System.Web.Mail.MailFormat.Html;
System.Web.Mail.SmtpMail.SmtpServer = "localhost";
System.Web.Mail.SmtpMail.Send(objEmail);
objEmail = null;
}}
I took off the emails and just ran a response.write, ht.Count and it gave me 34,782
Thats a huge number of values/ I cant figure out why its like that...
Hint: Approximately 690 possible EMail addresses * 50 = 34,500
Solution: Look VERY carefully at whats happening during the inner loop on POS. Rather than getting up to fifty different EMails on the BCC line of each EMail, you are getting 50 duplicates of each and every EMail added to the hashtable. Note also that your string concatination of multiple EMail addresses (which are actually all the same) separated by semi-colons is not working either either as a new strBlock is created each time through the inner loop.
As far as code added to the EMail subject for debugging, I'm not sure that Keys.ToString is even defined as a valid method. It most likely does not produce a string containing a list of the hashtable keys.
How many records are in the database?
Also for each record you are looping through 51 times, means you migh have more than 682 records in DB, each record you are counting 51 times, that's why you are getting 34,782 as count!!!
I was not sure why you are adding each record 51 times to the ht table?
If you have 682 records in that table, then that's why you are getting that much count!!!
0 comments:
Post a Comment