添加新评论时发送电子邮件 [英] Send email when a new comment is add

查看:80
本文介绍了添加新评论时发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!



我在网上找到了这个代码片段:



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



我可以进去以任何方式更改此代码,当用户在特定帖子上发表评论时,我会收到一封主题已撰写评论的电子邮件?





也许是一个可以告诉我该怎么做的教程?



/ Tina

解决方案

< blockquote>假设你给用户2个字段,subject(textbox1)和一个注释(textbox2),一个按钮提交comment.on点击该按钮的事件,你可以获取textbox1和textbox2的值,将这些值附加到你的邮件正文,发送到所需的电子邮件地址,就是这样。


我今天晚上一直在玩你的代码,但无法让它正常工作。



我想我理解代码,但不太确定,因为还有很多东西我还没学过。但是你有一个名为clsMail.cs的CS文件,用于控制发送电子邮件时会发生什么。



在你的SQL字符串中你将你的信息作为用户在评论框中输入并粘贴到数据库中并发送电子邮件。



但是我不知道我是否正确地将它添加到我的代码。



但这里是我的代码隐藏:



 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;
使用 System.Data.SqlClient;
使用 System.Data;
使用 System.Configuration;
使用 System.IO;
public partial class VisBlognyhed :System.Web.UI.Page
{
private int kommentar_ID = < span class =code-digit> 1 ;
private int indlaeg_ID = 1 ;
private int myindent = 0 ;
受保护 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;
如果(img 图片)
{

img.Visible =(img.ImageUrl!= ); // 如果ImageUrl为空,则将图片设置为false
}


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

string mUserName = 石英;
string mUserDato = quartz @ msn。 COM;
string mDescription = 测试说明< /跨度>;
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;
}






}
}





和班级的代码:



 使用系统; 
使用 System.Web.Mail;
命名空间 JumpyForum
{
/// < 摘要 >
/// clsMail
的摘要说明 /// < / summary >
public class clsMail
{
public bool SendMail( string ToM, string FromM, string CcM, string MSubject, string MBody) // 打开数据库SQL SERVER中与Granth的连接
{
try
{
MailMessage objMM = new MailMessage();
// '设置属性
objMM.To = ToM; // razesh@hotmail.com;
objMM.From = FromM; // connectrajesh@hotmail.com;

// '如果您想将此电子邮件发送给其他人......
objMM.Cc = CcM; // flytorajesh@someaddress.com;

// '如果您想将此电子邮件发送给其他人...
// objMM.Bcc =studyrajesh@hotmail.com;

// '以文本格式发送电子邮件
objMM.BodyFormat = Ma ilFormat.Html;

// '(发送HTML格式,将MailFormat.Text更改为MailFormat.Html)

// '设置优先级 - 选项为高,低和正常

objMM.Priority = MailPriority.Normal;

// '设置主题
objMM.Subject = MSubject; // Hello there testing!;

// '设置正文 - 使用VbCrLf插入回车
objMM.Body = MBody; < span class =code-comment> // 嗨!你好吗?;
SmtpMail.SmtpServer = localhost;
SmtpMail.Send(objMM);

return true ;
}
catch
{
return ;
}
最后
{

}

}
}
}





在这一个MailPriority中有一个失败的



希望你能引导我一些


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

解决方案

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.


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:

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:

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


这篇关于添加新评论时发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆