Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have done email notification system in my website in which user get email when they reach deadline..
but i encountered one problem that first user is receiving email that intended for him but second user is receiving 2 mails one that intended for him and another one is of previous user same way for 3rd user he will receive 3 mails and so on...
so for 1 user it will work perfect but when user increase it cause problem..
can anyone help me for finding error in below code because i'm unable to figure out what is wrong with code...



C#
public partial class Default2 : System.Web.UI.Page
{
 dbconnection db = new dbconnection();
    SqlDataReader dr;
    String senderAddress = string.Empty;
    String receiveraddress = string.Empty;
    String emailSubject = string.Empty;
    String emailMessageText = string.Empty;
    MailMessage mail = new MailMessage();
    SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
    int Count = 0;
 protected void  Button1_Click(object sender, EventArgs e)
 {
           db.CheckStatus();

        DateTime ti= DateTime.Now;
        string format = "dd-MM-yyyy";
       
        string cmd = "select email,name,bname from book,student where book.stuid=student.enrollid AND book.duedate=('" + ti.ToString(format) + "')";    
       
 dr = db.RetriveData(cmd);                      //this works perfect
        
        while(dr.read())
        {

            Count++;
           
            senderAddress = "mymail@gmail.com";
            receiveraddress = dr[0].toString();
            emailSubject = " Book Duedate Reminder";
            emailMessageText = "Dear Mr " + dr[1].toString() + ". Today is duedate for book " + dr[2].toString() + ". So please return back to library otherwise you have to pay fine for late returning.Thank You.";

            mail.From = new MailAddress(senderAddress, "LibraryAdmin", System.Text.Encoding.UTF8);
            mail.To.Add(new MailAddress(receiveraddress));
            mail.Subject = emailSubject;
            mail.Body = emailMessageText;
           
            client.UseDefaultCredentials = false;
            client.Credentials = new NetworkCredential("mymail@gmail.com", "mypwd");
            client.EnableSsl = true;
            client.Send(mail);
           
            Label1.Text = "Mail Sent " + Convert.ToString(Count);


            TextBox1.Text += dr[0].toString() + "  " + dr[1].toString() + "  " + dr[2].toString() + "\n";           //here also it shows perfect 

            
        }
   }


[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 7-Mar-14 21:44pm
v3

Try creating a new mail message for each user instead of reusing the old one:
C#
...
        while(dr.read())
        {
           mail = new MailMessage(); // add this line
...
 
Share this answer
 
C#
MailMessage emailmsg = new MailMessage();
        emailmsg.From = new MailAddress("abc@inc.com", "abc");
        emailmsg.To.Add(new MailAddress("cdf@inc.com", "cdf"));
        emailmsg.Subject = "Regarding Some information";
        emailmsg.Body = "stest bodcy";
        emailmsg.IsBodyHtml = true;
        emailmsg.Priority = MailPriority.Normal;
        SmtpClient mailclient = new SmtpClient("smtp.bizmail.yahoo.com");
        mailclient.Credentials = new System.Net.NetworkCredential("careers@inc.com", "123");
                
        mailclient.Send(emailmsg);
 
Share this answer
 
 
Share this answer
 

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