如何在asp.net中发送确认邮件 [英] how to send confirmation mail in asp.net

查看:113
本文介绍了如何在asp.net中发送确认邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码进行注册..

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings [ttConnectionString]。ConnectionString);



protected void Page_Load(object sender,EventArgs e)

{



}

protected void Button1_Click(对象发送者,EventArgs e)

{

MailMessage消息;

SqlCommand cmd = new SqlCommand();

string activationurl = string.Empty;

string emailid = string.Empty;

try

{



cmd =新的SqlCommand(插入触摸(名字,姓名,电子邮件,密码)值(@ firstname,@ secondname,@ email,@ password),con);

cmd.Parameters.AddWithValue(@ firstname,TextBox1.Text.Trim());

cmd.Parameters.AddWithValue(@ secondname,TextBox2.Text.Trim( ));

cmd.Parameters.AddWithValue(@ emailid,TextBox3.Text.Trim());

cmd.Parameters.AddWithValue(@ password, TextBox4.Text.Trim());

con.Open();

cmd.ExecuteNonQuery();

con.Close() ;

msg = new MailMessage();

SmtpClient smtp = new SmtpClient();

emailid = TextBox3.Text;

msg.From = new MailAddress(touchsoft@gmail.com);

msg.To.Add(emailid);

msg.Subject =帐户激活;

activationurl = Server.HtmlEncode(http:// localhost:36038 / TOUCHSOFT11 / activation.aspx? userid =+ fetchuserid(email)+& emailid=+ emailid);

msg.Body =hi+ TextBox1.Text +!\ n+感谢在 webcodeexpert.com 中显示兴趣和注册表+

点击此处激活您的帐户并享受我们的服务);

msg.IsBodyHtml = true;

smtp.Credentials = new NetworkCredential( yourid@gmail.com,你的密码);

smtp.Port = 587;

smtp.Host =smtp.gmail.com;

smtp.EnableSsl = true;

smtp.Send(msg);

clearcontrol();

ScriptManager.RegisterStartupScript (此,此.GetType(),消息,提醒('确认链接以激活您的帐户已发送到您的电子邮件地址'),真实);



}



c atch(Exception ex)

{

ScriptManager.RegisterStartupScript(this,this.GetType(),Message,alert('error occurred ::+ ex.Message .ToString()+');,true);

返回;



}

}



public void clearcontrol()

{

TextBox1.Text = string.Empty;

TextBox2.Text = string.Empty;

TextBox3.Text = string.Empty;

TextBox4.Text = string.Empty;

TextBox1.Focus();

}





私有字符串fetchuserid(字符串电子邮件)

{

SqlCommand cmd1 = new SqlCommand();

cmd1 = new SqlCommand(select touchid from email where email = @email,con) ;

cmd1.Parameters.AddWithValue(@ email,email);

con.Open();

string userid = Convert。 ToStrin g(cmd1.ExecuteScalar());

con.Close();

cmd1.Dispose();

返回用户ID;





}

}





以及用户点击激活链接时的代码..





protected void Page_Load(object sender,EventArgs e )

{

if(!Page.IsPostBack)

{

ActivateMyAccount();

}

}



private void ActivateMyAccount()

{

SqlConnection con = new SqlConnection();

SqlCommand cmd = new SqlCommand();

try

{

con = new SqlConnection(ConfigurationManager.ConnectionStrings [conStr]。ConnectionString);



if((!string.IsNullOrEmpty(Request.QueryString [UserID ]))& (!string.IsNullOrEmpty(Request.QueryString [EmailId])))

{//通过将Is_Approved设置为1来批准帐户,即在sql server表中为True

cmd = new SqlCommand(UPDATE Tb_Registration SET Is_Approved = 1 WHERE UserID = @ UserID AND EmailId = @ EmailId,con);

cmd.Parameters.AddWithValue(@ UserID,Request.QueryString [ UserID]);

cmd.Parameters.AddWithValue(@ EmailId,Request.QueryString [EmailId]);

if(con.State == ConnectionState.Closed)

{

con.Open();

}

cmd.ExecuteNonQuery();

Response.Write(您的帐户已被激活。您现在可以登录!);

}

}

catch(exception ex)

{

ScriptManager.RegisterStar tupScript(this,this.GetType(),Message,alert('出现错误:+ ex.Message.ToString()+');,true);

return;

}

终于

{

con.Close();

cmd .Dispose();

}

}







和我的数据库有5个字段

userid,firstname,secondname,email,password和is_app字段请帮助我,我无法发送确认邮件,请帮助我们

i am using this code for registration..
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ttConnectionString"].ConnectionString);

protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
MailMessage msg;
SqlCommand cmd = new SqlCommand();
string activationurl = string.Empty;
string emailid = string.Empty;
try
{

cmd = new SqlCommand("insert into touch ( firstname, secondname, emailid ,password) values ( @firstname, @secondname, @emailid, @password)", con);
cmd.Parameters.AddWithValue("@firstname", TextBox1.Text.Trim());
cmd.Parameters.AddWithValue("@secondname", TextBox2.Text.Trim());
cmd.Parameters.AddWithValue("@emailid", TextBox3.Text.Trim());
cmd.Parameters.AddWithValue("@password", TextBox4.Text.Trim());
con.Open();
cmd.ExecuteNonQuery();
con.Close();
msg = new MailMessage();
SmtpClient smtp = new SmtpClient();
emailid = TextBox3.Text;
msg.From = new MailAddress(" touchsoft@gmail.com");
msg.To.Add(emailid);
msg.Subject = " account activation";
activationurl = Server.HtmlEncode( http://localhost:36038/TOUCHSOFT11/activation.aspx? userid = "+ fetchuserid( email) + "&emailid" = " + emailid);
msg.Body = "hi" + TextBox1.Text + "!\n"+ "Thanks for showing interest and registring in webcodeexpert.com " +
" Please
click here to activate your account and enjoy our services");
msg.IsBodyHtml = true;
smtp.Credentials = new NetworkCredential( "yourid@gmail.com", "your passwordd");
smtp.Port = 587;
smtp.Host = " smtp.gmail.com";
smtp.EnableSsl = true;
smtp.Send( msg);
clearcontrol();
ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Confirmation Link to activate your account has been sent to your email address')", true);

}

catch (Exception ex)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Error occured : " + ex.Message.ToString() + "');", true);
return;

}
}

public void clearcontrol()
{
TextBox1.Text = string.Empty;
TextBox2.Text = string.Empty;
TextBox3.Text = string.Empty;
TextBox4.Text = string.Empty;
TextBox1.Focus();
}


private string fetchuserid(string email)
{
SqlCommand cmd1 = new SqlCommand();
cmd1 = new SqlCommand(" select userid from touch where email = @email", con);
cmd1.Parameters.AddWithValue("@email", email);
con.Open();
string userid = Convert.ToString(cmd1.ExecuteScalar());
con.Close();
cmd1.Dispose();
return userid;


}
}


and code for when user click on activation link..


protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ActivateMyAccount();
}
}

private void ActivateMyAccount()
{
SqlConnection con = new SqlConnection();
SqlCommand cmd = new SqlCommand();
try
{
con = new SqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString);

if ((!string.IsNullOrEmpty(Request.QueryString["UserID"])) & (!string.IsNullOrEmpty(Request.QueryString["EmailId"])))
{ //approve account by setting Is_Approved to 1 i.e. True in the sql server table
cmd = new SqlCommand("UPDATE Tb_Registration SET Is_Approved=1 WHERE UserID=@UserID AND EmailId=@EmailId", con);
cmd.Parameters.AddWithValue("@UserID", Request.QueryString["UserID"]);
cmd.Parameters.AddWithValue("@EmailId", Request.QueryString["EmailId"]);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
cmd.ExecuteNonQuery();
Response.Write("You account has been activated. You can Login now! ");
}
}
catch (Exception ex)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Error occured : " + ex.Message.ToString() + "');", true);
return;
}
finally
{
con.Close();
cmd.Dispose();
}
}



and mine database having 5 fields
userid, firstname, secondname, email, password, and is_app fields kindly help me i am not able to sending confirmation mail kindly help me guys

推荐答案

嗯...这听起来真的很傻,但是......你在这里填写了正确的信息:

Um...This may sound really silly, but...did you fill in the right information here:
smtp.Credentials = new NetworkCredential( "yourid@gmail.com", "your passwordd");

因为那看起来像y copy copy'n'paked它从某个地方完全没有阅读代码......

Because that looks like you copy'n'pasted it from somewhere without reading the code at all...


这篇关于如何在asp.net中发送确认邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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