Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

how to send email in c# website

$
0
0
I want to send email to user when fire is detected. however the codes below is not working. i didn't receive any emails after clicking send.

    protected void btnSend_Click(object sender, EventArgs e)
    {
        using (var connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
        using (var command = connection.CreateCommand())
        {
            command.CommandText = "SELECT * FROM [table] WHERE date = (SELECT MAX(date) FROM Indoor) ";
            connection.Open();
            using (var reader = command.ExecuteReader())
            {
                if (reader.Read())
                {
                    tbFire.Text = reader["fire"].ToString();
                }

                string  fireCheck = "";

                fireCheck = tbFire.Text;

                if (fireCheck == "Fire Detected")
                {
                    btnSend.Visible = true;
                    sendemail();
                }

                //else
                //    ImageFire.Visible = false;

            }
        }
    }

    void sendemail()
    {
        string str = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlConnection connect = new SqlConnection(str);
        string strCT = "SELECT email FROM User WHERE email=@email ";
        SqlCommand cmd = new SqlCommand(strCT, connect);

        cmd.Parameters.AddWithValue("@email", tbemail.Text);

        NetworkCredential cred = new NetworkCredential("firealert@gmail.com", "password");

        //creating an email
        MailMessage msg = new MailMessage();

        //add details to the email created
        msg.To.Add("email"); //add a new recipient to our msg
        msg.From = new MailAddress("firealert@gmail.com");
        msg.Subject = "Danger! Fire Alert!";  // Assign the subject of our message.
        msg.Body = "There's a fire detected. Please proceed to the safety area as soon as possible."; // Create the content(body) of our message.

        //sending the email
        SmtpClient client = new SmtpClient("smtp.gmail.com", 25);

        client.Credentials = cred; // Send our account login details to the client.
        client.EnableSsl = true;   // Read below.
        client.Send(msg);                 // Send our email.

    }


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>