locked
Sending email from select staement-C#.NET RRS feed

  • Question

  • User-1578974752 posted

    Hi

     I have a gridview with 150+ email

    how can I send email to all this 150 person at one button click. I could send one person with out any issue.How could I select all the emailsid from the grid view and send email. Even showing it in grid view is also not needed just need to send mail to all the emails from the select statement. How can I retrieve it in below email to=

    string to = "myemail.com"; //To address

    code as below

    DataSet ds = ORSQLSelectRecord("SELECT EmailID FROM HEADERS_ALL ");

    GridView1.DataSource = ds;

    GridView1.DataBind();

    Session["dse"] = ds;

    public static DataSet ORSQLSelectRecord(string strselect)

    {

    OracleConnection Ocon = new OracleConnection();

    Ocon.ConnectionString = "";

    try

    {

    Ocon.Open();

    OracleCommand ocmd = new OracleCommand();

    ocmd.Connection = Ocon;

    ocmd.CommandType = CommandType.Text;

    ocmd.CommandText = strselect;//

    OracleDataAdapter oda = new OracleDataAdapter(ocmd);

    DataSet ds = new DataSet();

    oda.Fill(ds);

    return ds;

    }

    catch

    {

    return null;

    }

    finally

    {

    Ocon.Close();

    Ocon.Dispose();

    }

    }

    Thanks

    Thursday, February 18, 2021 9:41 AM

Answers

  • User-1330468790 posted

    Hi shsu,

     

    What does "num" mean in the first line of method "SendMail_SMT"?

    You might need to add parameter like "public static void SendMail_SMT(string toEmail, string num)".

     

    The second thing to do is to assign the "toEmail" to "string to".

     

    Regarding Smtp server, could you please confirm that you have already configured a SMTP server in "00.000.10.0", port "00"?

    Looks like it is not correct.

     

    If you don't configure the SMTP server, you could use gmail server to send the email.

    public static void SendMail_SMT(string toEmail, string num)
            {
    
                string body = " Number-" + "\n" + "\n" + num + "\n" + "\n" + "has been approved" + "< br /> ";
    
                string to = toEmail; //To address  
    
                string from = "admin@mil.com"; //From address
    
                MailMessage mail = new MailMessage();
    
                mail.To.Add(to);
                mail.From = new MailAddress(from, "admin", System.Text.Encoding.UTF8);
    
                mail.Subject = "This mail is send from admin@mil.com";
                mail.SubjectEncoding = System.Text.Encoding.UTF8;
                mail.Body = body;
                mail.BodyEncoding = System.Text.Encoding.UTF8;
                mail.IsBodyHtml = true;
                mail.Priority = MailPriority.High;
    
                SmtpClient client = new SmtpClient();
                client.Credentials = new System.Net.NetworkCredential("your gmail account", "gmail password");
                client.Port = 587;
                client.Host = "smtp.gmail.com";
    
                try
                {
                    client.Send(mail);
                }
                catch (Exception ex)
                {
                    // Catch error to find out the reason of the error
                    Exception ex2 = ex;
                    string errorMessage = string.Empty;
                    while (ex2 != null)
                    {
                        errorMessage += ex2.ToString();
                        ex2 = ex2.InnerException;
                    }
                }
            }

     

    Hope helps.

    Best regards,

    Sean
     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 19, 2021 1:58 PM

All replies

  • User-1330468790 posted

    Hi shsu, 

     

    So, from your description, I understand that your purpose is to retrieve all of values in the "EamilID" column (spelling mistake?), correct?

     

    My suggestion is to traverse the result datatable for the "EamilID" values one by one and then use them to send the email.

    For example,

     string to = ""; //To address
    
                DataSet ds = ORSQLSelectRecord("SELECT EamilID FROM HEADERS_ALL ");
    
                // Get datatable from data set, which should be the first table inside the data set according to your codes
                DataTable dt = ds.Tables[0];
                
                foreach(DataRow dr in dt.Rows)
                {
                    to = dr["EamilID"].ToString();
                    SendEmailTo(to);
                }

      SendEmailTo(string toEmail) is a method that you will take to send the email.

    public static bool SendEmailTo(string toEmail)
            {
                // return true if the email is sent successfully
                // otherwise, return false
            } 

    Regarding how you send the email, you could refer to below links:

    SmtpClient:  https://stackoverflow.com/a/18326814/12871232

    SendGrid API: https://stackoverflow.com/a/61526222/12871232

     

    Besides, I think you could directly get DataTable from the method "ORSQLSelectRecord" as below:

    public static DataTable ORSQLSelectRecord(string strselect)
            {
                OracleConnection Ocon = new OracleConnection();
    
                Ocon.ConnectionString = "";
    
                try
                {
    
                    Ocon.Open();
    
                    OracleCommand ocmd = new OracleCommand();
    
                    ocmd.Connection = Ocon;
    
                    ocmd.CommandType = CommandType.Text;
    
                    ocmd.CommandText = strselect;//
    
                    OracleDataAdapter oda = new OracleDataAdapter(ocmd);
    
                    DataTable dt = new DataTable();
    
                    oda.Fill(dt);
    
                    return dt;
    
                }
                catch
                {
                    return null;
                }
                finally
                {
                    Ocon.Close();
                    Ocon.Dispose();
                }
            }

     

    Hope helps.

    Best regards,

    Sean

    Thursday, February 18, 2021 12:44 PM
  • User-1578974752 posted

    How to change the publicstaticvoid SendMail_SMT to make it work

    foreach (DataRow dr in dt.Rows)

    {

    to = dr["EamilID"].ToString();

    SendMail_SMT(to);

    }

    publicstaticvoid SendMail_SMT(string toEmail)

           {

               string body = " Number-" + "\n" + "\n" + num + "\n" + "\n" + "has been approved” + "<br/>";

              

              

     

               string to = ""; //To address  

     

               string from = "admin@mil.com"; //From address

     

               System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(from, to);

     

               message.Body = body;//body;

               message.BodyEncoding = System.Text.Encoding.UTF8;

               message.IsBodyHtml = true;

               System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("00.000.10.0", 00);

               client.EnableSsl = false;

               client.UseDefaultCredentials = false;

            

               client.Send(message);

     

     

     

           }

     

    Friday, February 19, 2021 9:08 AM
  • User-1330468790 posted

    Hi shsu,

     

    What does "num" mean in the first line of method "SendMail_SMT"?

    You might need to add parameter like "public static void SendMail_SMT(string toEmail, string num)".

     

    The second thing to do is to assign the "toEmail" to "string to".

     

    Regarding Smtp server, could you please confirm that you have already configured a SMTP server in "00.000.10.0", port "00"?

    Looks like it is not correct.

     

    If you don't configure the SMTP server, you could use gmail server to send the email.

    public static void SendMail_SMT(string toEmail, string num)
            {
    
                string body = " Number-" + "\n" + "\n" + num + "\n" + "\n" + "has been approved" + "< br /> ";
    
                string to = toEmail; //To address  
    
                string from = "admin@mil.com"; //From address
    
                MailMessage mail = new MailMessage();
    
                mail.To.Add(to);
                mail.From = new MailAddress(from, "admin", System.Text.Encoding.UTF8);
    
                mail.Subject = "This mail is send from admin@mil.com";
                mail.SubjectEncoding = System.Text.Encoding.UTF8;
                mail.Body = body;
                mail.BodyEncoding = System.Text.Encoding.UTF8;
                mail.IsBodyHtml = true;
                mail.Priority = MailPriority.High;
    
                SmtpClient client = new SmtpClient();
                client.Credentials = new System.Net.NetworkCredential("your gmail account", "gmail password");
                client.Port = 587;
                client.Host = "smtp.gmail.com";
    
                try
                {
                    client.Send(mail);
                }
                catch (Exception ex)
                {
                    // Catch error to find out the reason of the error
                    Exception ex2 = ex;
                    string errorMessage = string.Empty;
                    while (ex2 != null)
                    {
                        errorMessage += ex2.ToString();
                        ex2 = ex2.InnerException;
                    }
                }
            }

     

    Hope helps.

    Best regards,

    Sean
     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 19, 2021 1:58 PM