Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello ,,


I want to know how we can send mail to all the user's email id in the database table using button click .

when admin click on the emailsendbutton than the mail sent to all the user
Posted

For sending mail, you can use System.Net.Mail
Sample is given over here[^].

Now all you have to do is, get all the users from the database, store it in DataTable and then,
C#
foreach(DataRow dr in dt.Rows)   // dt is your datatable
{
    SendMailToClient(dr["Email"].ToString());   // SendMailToClient is a method containing logic of sending mail, and takes email address as a parameter, that can easily fetched from DataTable.
}


Hope the flow is clear to you.
There is a another way for doing this, but once you got the idea, you'll have that as well, for sure :)

-KR
 
Share this answer
 
Comments
Swinky_Talwar 20-Feb-14 6:43am    
Thanx
Select All user and then write this code in that rdr.read()

VB
Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
        Dim m As MailMessage = New MailMessage()
        Dim sc As SmtpClient = New SmtpClient()

        Try

            m.From = New MailAddress("astikajadhav@gmail.com", "Display name")
            m.To.Add(New MailAddress("astikajadhav@gmail.com", "Display name To")) 'to whom mail have to send
            m.Subject = " This is a Test Mail"
            m.IsBodyHtml = True
            m.Body = "Phone number - "
            sc.Host = "smtp.gmail.com"
            sc.Port = 587
            sc.Credentials = New System.Net.NetworkCredential("astikajadhav@gmail.com", "datta-sudhir")
            sc.EnableSsl = True
            sc.Send(m)


            Response.Write("Email Send sucessfully")
        Catch ex As Exception

            Response.Write(ex.Message)

        End Try


Will work definitely
Any question then ask
 
Share this answer
 
Comments
Tom Marvolo Riddle 18-Feb-14 1:43am    
Hi astika,OP ask question in C#. Please convert it into c# and update it
Swinky_Talwar 20-Feb-14 4:53am    
Convert it into C#
Try this code:


C#
NetworkCredential cred = new NetworkCredential("gmail@domail.com", "Password");
MailMessage msg = new MailMessage();

msg.To.Add("name@domail.com");


            using (SqlDataReader dr = cmd.ExecuteReader())
            {
                while (dr.Read())
                {
                    msg.CC.Add((string)dr["user_email"]);
                }
            }


msg.Subject = "Welcome ME";

msg.Body = "You Have Successfully Entered to Sera's World!!!";
msg.From = new MailAddress("mail@domail.com"); // Your Email Id
SmtpClient client = new SmtpClient("smtp.gmail.com", 587); //SMTP Google
SmtpClient client1 = new SmtpClient("smtp.mail.yahoo.com", 465); //SMTP Yahoo
client.Credentials = cred;
client.EnableSsl = true;
client.Send(msg);
 
Share this answer
 
Comments
Swinky_Talwar 20-Feb-14 6:42am    
Thankyou So Much Jubayer Ahmed
Jubayer Ahmed 20-Feb-14 6:52am    
you are welcome. :)
C#
 protected void SendMail_Click(object sender, EventArgs e)
{
    cmd3 = new SqlCommand("select email from tbluser", con);
        string email;
        con.Open();
       SqlDataReader dr=cmd3.ExecuteReader();
       while(dr.Read())
       { 
           email=dr[0].ToString();
           MailMessage myMsg = new MailMessage();
           myMsg.From = new MailAddress("*******@gmail.com");
           myMsg.To.Add(email);
           myMsg.Subject = "IPPTA E-library ";
           myMsg.IsBodyHtml = true;
           string currentyear = DateTime.Now.Year.ToString();


           myMsg.Body = "Hello Thanks Your welcome in this World";



           myMsg.CC.Add("******@gmail.com");
           // your remote SMTP server IP.
           SmtpClient smtp = new SmtpClient();
           smtp.Host = "smtp.gmail.com";
           smtp.Port = 587;
           smtp.Credentials = new System.Net.NetworkCredential("*****@gmail.com", "Password");
           smtp.EnableSsl = true;
           smtp.Send(myMsg);
       }
}
 
Share this answer
 
v4
try to read this. . . .

How-To-Send-eMail[__]
 
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