4 Replies - 1453 Views - Last Post: 25 January 2013 - 08:42 AM Rate Topic: -----

#1 newbieJY  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 25-January 13

how to send email in c# website

Posted 25 January 2013 - 07:05 AM

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.

    }



Is This A Good Question/Topic? 0
  • +

Replies To: how to send email in c# website

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5631
  • View blog
  • Posts: 12,078
  • Joined: 02-June 10

Re: how to send email in c# website

Posted 25 January 2013 - 07:15 AM

We can't help with "Its not working". We're not your debug monkeys. Have you even tried to do some debugging? What was the response from the server when you sent the email? Do you have an actual error or did you just not get the email?

There could be a dozen reasons the email never arrived: SPAM folder on the receiving end, bad credentials when you sent it,
LIne 47 "email" is not a valid email address and it isn't a variable from the earlier code

What this shows us is that you aren't familiar with breakpoints and how to debug your own code.


tlhIn`toq's FAQ list

Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute.

Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated Jan 2013
Spoiler



Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3650
  • View blog
  • Posts: 11,420
  • Joined: 05-May 12

Re: how to send email in c# website

Posted 25 January 2013 - 07:20 AM

First ensure that your code works correctly as a desktop program: console, WPF or WinForms.

If that is succeeding, but when sending out from a website fails, ensure that the user that the .NET app pools are running under in IIS has rights to access the Internet and send email.
Was This Post Helpful? 0
  • +
  • -

#4 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3730
  • View blog
  • Posts: 6,017
  • Joined: 08-June 10

Re: how to send email in c# website

Posted 25 January 2013 - 07:45 AM

I've merged your three duplicate threads into this one thread. Please do not open multiple threads to ask the same question.
Was This Post Helpful? 0
  • +
  • -

#5 kienkiatteo  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 16-May 12

Re: how to send email in c# website

Posted 25 January 2013 - 08:42 AM

Hi, as mentioned. Add breakpoints and do debugging to see where the error is. Let us know the error here.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1