Click here to Skip to main content
15,919,500 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!

I have found this codesnippet on the internet:

http://hozefam.com/post/2012/08/21/Code-snippet-to-send-Email-from-C-code.aspx[^]

Can I in any way change this code for when a user leaves a comment on a particular post, I will receive an email with which topics have written a comment on?


And maybe a tutorial that can show me how to do?

/Tina
Posted

Suppose,you give user 2 fields,subject(textbox1) and a comment(textbox2),one button to submit the comment.on click event of that button you can grab the values of textbox1 and textbox2,attach that values to your mail body,send it to the required email address,thats it.
 
Share this answer
 
Comments
tina_overgaard 30-Apr-13 6:44am    
But will it give me a clue off what particular post the comment is writing on?
Thanks7872 30-Apr-13 6:46am    
where do you want to apply this?i mean have you made any application or what?
tina_overgaard 30-Apr-13 6:49am    
http://www.william-overgaard.dk/VisBlognyhed.aspx?indlaeg_ID=13

This is the website I want to have it on. The codebeind file for this site is

protected void Page_Load(object sender, EventArgs e)
{
recaptcha.Validate();
if (Request.QueryString["indlaeg_ID"] != null)
{
int q = Convert.ToInt32(Request.QueryString["indlaeg_ID"]);
//hentProdukt(q);
}

}
protected void Button_kommentar_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
Label_kommentar.Text = "Korrekt";
Label_kommentar.ForeColor = System.Drawing.Color.Green;
SqlConnection conn = new SqlConnection();
conn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();

SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "INSERT INTO kommentar (dato, navn, kommentar, fk_indlaeg_ID) VALUES(@dato, @navn, @kommentar, @indlaeg)";
cmd.Parameters.Add("@dato", SqlDbType.DateTime).Value = TextBox_dato.Text;
cmd.Parameters.Add("@navn", SqlDbType.VarChar).Value = TextBox_navn.Text;
cmd.Parameters.Add("@kommentar", SqlDbType.Text).Value = TextBox_kommentar.Text;
cmd.Parameters.Add("@indlaeg", SqlDbType.Int).Value = Request.QueryString["indlaeg_ID"];

conn.Open();
cmd.ExecuteNonQuery();
conn.Close();

TextBox_dato.Text = "";
TextBox_navn.Text = "";
TextBox_kommentar.Text = "";


Label_kommentar.Text = "Din kommentar er modtaget.";


}
else
{
Label_kommentar.Text = "Du skal indtaste Captcha eller du har indtastet forkert";
Label_kommentar.ForeColor = System.Drawing.Color.Red;
}






}
Thanks7872 30-Apr-13 6:59am    
Consider this question which you have asked here as a post.then you can generate one random number for each post. on every comment on that post you can assign the code you created for that post to the comments.while sending the mail,you can check for that code and have the post to which user has commented on,include the corresponding post to the mail body with comment details.
Thanks7872 30-Apr-13 7:07am    
give your mail id i can send you sample codes
I've been playing with your code this evening, but could not really get it to work correctly.

I think I understand the code, but am not quite sure as there are many things I have not learned yet. But you have a CS file is called clsMail.cs, which is to control what happens when sending an email.

And in your SQL string you take your info as the user types in the comment box and paste in the database and send an email.

But I do not know if I'm doing it right to add it to my code.

But here's my codebehind:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.IO;
public partial class VisBlognyhed : System.Web.UI.Page
{
    private int kommentar_ID = 1;
    private int indlaeg_ID = 1;
    private int myindent = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        recaptcha.Validate();
        if (Request.QueryString["indlaeg_ID"] != null)
        {
            int q = Convert.ToInt32(Request.QueryString["indlaeg_ID"]);
            //hentProdukt(q);
        }

    }
    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {

        Image img = e.Item.FindControl("imgBlog") as Image;
        if (img is Image)
        {

            img.Visible = (img.ImageUrl != "");  // will set image visible to false if ImageUrl is empty string
        }
       

    }
//   
    protected void Button_kommentar_Click(object sender, EventArgs e)
    {
        int mParentId = kommentar_ID;
        int mArticleId = indlaeg_ID;

        string mUserName = "quartz";
        string mUserDato = "quartz@msn.com";
        string mDescription = "Test Description";
        int mIndent = myindent;
        

        if (Page.IsValid)
        {
            mUserName = TextBox_navn.Text;
            mUserDato = TextBox_dato.Text;
            mDescription = TextBox_kommentar.Text;
            


            Label_kommentar.Text = "Korrekt";
            Label_kommentar.ForeColor = System.Drawing.Color.Green;
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();

            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = "INSERT INTO kommentar (dato, navn, kommentar, fk_indlaeg_ID) VALUES(@dato, @navn, @kommentar, @indlaeg)";
            cmd.Parameters.Add("@dato", SqlDbType.DateTime).Value = TextBox_dato.Text;
            cmd.Parameters.Add("@navn", SqlDbType.VarChar).Value = TextBox_navn.Text;
            cmd.Parameters.Add("@kommentar", SqlDbType.Text).Value = TextBox_kommentar.Text;
            cmd.Parameters.Add("@indlaeg", SqlDbType.Int).Value = Request.QueryString["indlaeg_ID"];
            
cmd.CommandText = "INSERT INTO kommentar (dato, navn, kommentar, fk_indlaeg_ID) VALUES('" +mParentId + "','" + mArticleId +  "','" + mUserName +  "','" + mUserDato +  "','" + mDescription + "','" + "')";

            




            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();

            TextBox_dato.Text = "";
            TextBox_navn.Text = "";
            TextBox_kommentar.Text = "";


            Label_kommentar.Text = "Din kommentar er modtaget.";


        }
        else
        {
            Label_kommentar.Text = "Du skal indtaste Captcha eller du har indtastet forkert";
            Label_kommentar.ForeColor = System.Drawing.Color.Red;
        }



            

        
    }
}


And the code for the Class:

C#
using System;
using System.Web.Mail;
namespace JumpyForum
{
/// <summary>
/// Summary description for clsMail
/// </summary>
public class clsMail
{
	public bool SendMail(string ToM, string FromM, string CcM, string MSubject, string MBody ) // Opens database connection with Granth in SQL SERVER
		{
			try
			{
				MailMessage objMM = new MailMessage();
				//'Set the properties
				objMM.To = ToM;//"razesh@hotmail.com";
				objMM.From = FromM;//"connectrajesh@hotmail.com";

				//'If you want to CC this email to someone else...
				objMM.Cc = CcM;//"flytorajesh@someaddress.com";

				//'If you want to BCC this email to someone else...
				//objMM.Bcc = "studyrajesh@hotmail.com";

				//'Send the email in text format
				objMM.BodyFormat = MailFormat.Html ;

				//'(to send HTML format, change MailFormat.Text to MailFormat.Html)

				//'Set the priority - options are High, Low, and Normal

				objMM.Priority = MailPriority.Normal;

				//'Set the subject
				objMM.Subject = MSubject;//"Hello there testing!";

				//'Set the body - use VbCrLf to insert a carriage return
				objMM.Body = MBody;//"Hi! How are you doing?";
				SmtpMail.SmtpServer = "localhost"; 
				SmtpMail.Send(objMM);

				return true;
			}
			catch
			{
				return false;
			}
			finally
			{
				
			}

		}
	}
}


And in this one MailPriority there is a failed on

Hope you can guide me some more
 
Share this answer
 
Comments
Thanks7872 1-May-13 0:15am    
remove objMM.Priority = MailPriority.Normal; from your code.
Sergey Alexandrovich Kryukov 5-Nov-13 10:29am    
How it can be an answer? And why did you self-accepted it? This is the abuse.
—SA
tina_overgaard 1-May-13 1:40am    
What about SmtpMail.Send(objMM);

And do I need to have something in my web.config for smtp?

And I get a server error, when I try to Insert a comment, so something is wrong about my code in the insert.
Here is the error:

There are fewer columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement.

Line 77:
Line 78: conn.Open();
Line 79: cmd.ExecuteNonQuery();
Line 80: conn.Close();
Line 81:
Thanks7872 1-May-13 1:52am    
if possible post your function in which you are performing this insert and name,type of the columns in the database.
tina_overgaard 1-May-13 2:27am    
The function when you click the button

protected void Button_kommentar_Click(object sender, EventArgs e)
{
int mParentId = kommentar_ID;
int mArticleId = indlaeg_ID;

string mUserName = "quartz";
string mUserDato = "quartz@msn.com";
string mDescription = "Test Description";
int mIndent = myindent;


if (Page.IsValid)
{
mUserName = TextBox_navn.Text;
mUserDato = TextBox_dato.Text;
mDescription = TextBox_kommentar.Text;



Label_kommentar.Text = "Korrekt";
Label_kommentar.ForeColor = System.Drawing.Color.Green;
SqlConnection conn = new SqlConnection();
conn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();

SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "INSERT INTO kommentar (dato, navn, kommentar, fk_indlaeg_ID) VALUES(@dato, @navn, @kommentar, @indlaeg)";
cmd.Parameters.Add("@dato", SqlDbType.DateTime).Value = TextBox_dato.Text;
cmd.Parameters.Add("@navn", SqlDbType.VarChar).Value = TextBox_navn.Text;
cmd.Parameters.Add("@kommentar", SqlDbType.Text).Value = TextBox_kommentar.Text;
cmd.Parameters.Add("@indlaeg", SqlDbType.Int).Value = Request.QueryString["indlaeg_ID"];

cmd.CommandText = "INSERT INTO kommentar (dato, navn, kommentar, fk_indlaeg_ID) VALUES('" +mParentId + "','" + mArticleId + "','" + mUserName + "','" + mUserDato + "','" + mDescription + "','" + "')";






conn.Open();
cmd.ExecuteNonQuery();
conn.Close();

TextBox_dato.Text = "";
TextBox_navn.Text = "";
TextBox_kommentar.Text = "";


Label_kommentar.Text = "Din kommentar er modtaget.";


}
else
{
Label_kommentar.Text = "Du skal indtaste Captcha eller du har indtastet forkert";
Label_kommentar.ForeColor = System.Drawing.Color.Red;
}






}

The database for article is:

CREATE TABLE [dbo].[indlaeg] (
[indlaeg_ID] INT IDENTITY (1, 1) NOT NULL,
[fk_katogori_ID] INT NOT NULL,
[overskrift] VARCHAR (50) NOT NULL,
[navn] VARCHAR (50) NOT NULL,
[tekst] TEXT NOT NULL,
[dato_indlaeg] DATETIME NOT NULL,
[billede] TEXT NULL,
CONSTRAINT [PK_indlaeg_indlaeg_ID] PRIMARY KEY CLUSTERED ([indlaeg_ID] ASC)
);

For the comment:

CREATE TABLE [dbo].[kommentar] (
[kommentar_ID] INT IDENTITY (1, 1) NOT NULL,
[fk_indlaeg_ID] INT NOT NULL,
[dato] DATETIME NOT NULL,
[kommentar] TEXT NOT NULL,
[navn] VARCHAR (50) NOT NULL,
CONSTRAINT [PK_kommentar_kommentar_ID] PRIMARY KEY CLUSTERED ([kommentar_ID] ASC)
);

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