(分享)ASP.NET 实现注册用户 邮箱激活


问题的提出:

当一个用户注册后,首先他不能立即使用用户名和密码登录,需要使用邮箱验证激活后方能使用。 

 

Code:

 

EmailInfo.aspx(起始页文件)

前台代码

ExpandedBlockStart.gif 代码
  < form id = " form1 "  runat = " server " >
    
< div >
         
< table cellpadding = " 3 "  width = " 550 " >
                
< tr >
                    
< td colspan = " 3 "   >
                        
< b > Fill  in  Your Details </ b >
                    
</ td >
                
</ tr >
                
< tr >
                    
< td >
                    
</ td >
                
</ tr >
                
< tr align = " center " >
                    
< td >
                        Your Name :
                    
</ td >
                    
< td align = " left " >
                         
< asp:textbox id = " txtName "  runat = " server " ></ asp:textbox >
                    
</ td >
                
</ tr >
                
< tr align = " center " >
                    
< td >
                         User Name :
                    
</ td >
                    
< td align = " left " >
                         
< asp:textbox id = " txtUserName "  runat = " server " ></ asp:textbox >
                    
</ td >
                
</ tr >
                
< tr align = " center " >
                    
< td >
                         Email Address :
                    
</ td >
                    
< td align = " left " >
                        
< asp:textbox id = " txtEmail "  runat = " server " ></ asp:textbox >
                    
</ td >
                
</ tr >
                    
< tr align = " center " >
                    
< td  >
                         Password :
                    
</ td >
                    
< td align = " left " >
                        
< asp:textbox id = " txtPassword "  textmode = " Password "  runat = " server " ></ asp:textbox >
                    
</ td >
                
</ tr >
                
< tr align = " center " >
                    
< td >
                        Confirm Password :
                    
</ td >
                    
< td align = " left " >
                        
< asp:textbox id = " txtConfirmPassword "  textmode = " Password "  runat = " server " ></ asp:textbox >
                    
</ td >
                    
< td align = " left " >
                        
< asp:CompareValidator ID = " CompareValidator1 "  runat = " server "  
                            ErrorMessage = " Password not match "  ControlToCompare = " txtPassword "  
                            ControlToValidate = " txtConfirmPassword "  Display = " Dynamic " ></ asp:CompareValidator >
                    
</ td >
                
</ tr >
                
< tr align = " right " >
                    
< td colspan = " 3 " >
                        
< asp:Button ID = " btnRegister "  runat = " server "  Text = " Register "  
                            onclick = " btnRegister_Click "   />
                    
</ td >
                
</ tr >
     
        
</ table >
    
</ div >
    
< asp:Label ID = " Label1 "  runat = " server "  Text = " Label " ></ asp:Label >
    
</ form >

 

 后台代码

ExpandedBlockStart.gif 代码
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.Net.Mail;
using  System.Net;
using  System.Resources;
using  System.IO;


namespace  EmailActive
{
    
public   partial   class  EmailInfo : System.Web.UI.Page
    {
        
protected   void  Page_Load( object  sender, EventArgs e)
        {

        }

        
protected   void  btnRegister_Click( object  sender, EventArgs e)
        {
            
string  iUserID  =  Users.RegisterClient(txtUserName.Text.Trim(), txtEmail.Text.Trim(), txtPassword.Text.Trim(), txtName.Text.Trim()).ToString();
            
string  sData  =  File.ReadAllText(Server.MapPath( " ~/NewMemberEmail.txt " ));

            Label1.Text  =  iUserID;
            Response.Write(sData);
            sData  =  sData.Replace( " [Name] " , txtName.Text.Trim());
            sData  =  sData.Replace( " [LINK] " " http://localhost:18277(这里根据VS生成地址修改) "   +   " /Activate.aspx?UserID= "   +
            iUserID  +   " &UN= "   +  Server.UrlEncode(txtUserName.Text.Trim()));                // 使用Server.UrlEncode()解决QueryString()传递中文乱码问题
            sData  =  sData.Replace( " [UserName] " , txtUserName.Text.Trim());
            sData  =  sData.Replace( " [Pwd] " , txtPassword.Text.Trim());
            SMTPManager.SendEmail( " zfj123589@gmail.com " , txtName.Text.Trim(), txtEmail.Text.Trim(), sData,  " New Member Activation " false );
            Response.Redirect( " Login.aspx " );
        }

        
public   class  Users
        {
            
public   static   int  RegisterClient( string  UserName,  string  EmailAddress,  string  Password,  string  Name)
            {
                
string  strConn  =  ConfigurationManager.ConnectionStrings[ " Reg " ].ConnectionString;
                SqlConnection conn  =   new  SqlConnection(strConn);
                conn.Open();
                SqlCommand cmd  =  conn.CreateCommand();
                cmd.CommandText  =   " Insert into tblUsers (UserName, Email, Password, Name) Values (@UserName, @EmailAddress, @Password, @Name) "   +
                                    
" select @UserID = SCOPE_IDENTITY() " ;
                cmd.CommandType  =  CommandType.Text;
                cmd.Parameters.Add( " @UserName " , SqlDbType.VarChar,  50 );
                cmd.Parameters[ " @UserName " ].Value  =  UserName;
                cmd.Parameters.Add( " @EmailAddress " , SqlDbType.VarChar,  100 );
                cmd.Parameters[ " @EmailAddress " ].Value  =  EmailAddress;
                cmd.Parameters.Add( " @Password " , SqlDbType.VarChar,  50 );
                cmd.Parameters[ " @Password " ].Value  =  Password;
                cmd.Parameters.Add( " @Name " , SqlDbType.VarChar,  100 );
                cmd.Parameters[ " @Name " ].Value  =  Name;

                SqlParameter p  =  cmd.Parameters.Add( " @UserID " , SqlDbType.Int);
                p.Direction  =  ParameterDirection.Output;

                
int  newUserID  =   0 ;

                cmd.ExecuteNonQuery();
                conn.Close();
                newUserID  =  ( int )p.Value;

                
return  newUserID;
            }
        }

        
///   <summary>
        
///  SMTPManager发送邮件类
        
///   </summary>
        
/// FROM 发送者邮箱地址, FromDisplayName 发送者名字, TO接受者邮箱地址, BODY邮件内容,SUBJECT 主题行, bIsHtml是否是HTML邮件
        
        
public   class  SMTPManager
        {
            
public  SMTPManager()
            {
            }
            
public   static   void  SendEmail( string  FROM,  string  FromDisplayName,  string  TO,  string  BODY,  string  SUBJECT,  bool  bIsHtml)
            {
                MailMessage m  =   new  MailMessage();
                m.From  =   new  MailAddress(FROM, FromDisplayName);
                m.To.Add(TO);
                m.Subject  =  SUBJECT;
                m.Body  =  BODY;
                m.BodyEncoding  =  System.Text.Encoding.UTF8;
                m.IsBodyHtml  =  bIsHtml;
                m.ReplyTo  =   new  MailAddress(FROM);
                
// smtp.126.com不可用,所有使用smtp.gmail.com
                SmtpClient smtp  =   new  SmtpClient( " smtp.gmail.com " 587 );          
                smtp.Credentials  =   new  NetworkCredential( " zfj123589 " " 123zxcvb " );
                smtp.EnableSsl  =   true ;
               
                smtp.Send(m);

            }
        }
    }
          


}

 

 

Activate.aspx(激活页面)

 

 前台代码

 <form id="form1" runat="server">

     < div >
        
< asp:Label ID = " lblMsg "  runat = " server "  Text = " Label " ></ asp:Label >
    
</ div >
</ form >

 

 后台代码:

ExpandedBlockStart.gif 代码
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Web;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Data;
using  System.Data.SqlClient;
using  System.Configuration;

namespace  EmailActive
{
    
public   partial   class  Activate : System.Web.UI.Page
    {
        
protected   void  Page_Load( object  sender, EventArgs e)
        {
            
string  sUserID  =   string .Empty;

            
string  sID  =  Request.QueryString[ " UserID " ].Trim();
            lblMsg.Text  =  sID;
            
// Server.UrlDecode()对URL地址进行解密
             string  sUserName  =  Server.UrlDecode(Request.QueryString[ " UN " ]).ToString().Trim();
            
            
string  strConn  =  ConfigurationManager.ConnectionStrings[ " Reg " ].ConnectionString;
            SqlConnection conn  =   new  SqlConnection(strConn);
            SqlCommand cmd  =  conn.CreateCommand();
            
try
            {
                conn.Open();
                
                cmd.CommandText  =   " Select UserID from tblUsers where UserName = ' "   +  sUserName  +   " ' " ;
                sUserID  =  cmd.ExecuteScalar().ToString().Trim();
            }
            
finally
            {
                conn.Close();
            }
            
if  (String.Compare(sID, sUserID)  ==   0 )
            {
                conn.Open();
                SqlCommand sqlCmd  =  conn.CreateCommand();
                cmd.CommandText  =   " Update tblUsers set Verified = 1 where UserName = ' "   +  sUserName  +   " ' "
                
string  UserID  =  cmd.ExecuteNonQuery().ToString();
                conn.Close();
                Response.Redirect( " Login.aspx " );
            }
            
else
            {
                lblMsg.Text  =   " Failed to activate your account. Please contact zfj123589@gmail.com<br> " ;
            }
             

        }
    }
}

 

 

Login.aspx(登录界面)

 前台代码:

ExpandedBlockStart.gif 代码
< form id = " form1 "  runat = " server " >
    
< div >
        
< table id = " RegTable "  cellpadding = " 2 " >
            
< tr >
                
< td >
                    UserName:
                
</ td >
                
< td >
                    
< asp:TextBox ID = " txtUserName "  runat = " server " ></ asp:TextBox >
                
</ td >  
            
</ tr >
            
< tr >
                
< td >
                    Password:
                
</ td >
                
< td >
                    
< asp:TextBox ID = " txtPassword "  runat = " server " ></ asp:TextBox >
                
</ td >
            
</ tr >
            
< tr >
                
< td >
                   
< asp:Button ID = " btnLog "  runat = " server "  Text = " Log In "  onclick = " btnLog_Click "   />
                
</ td >
                
< td >
                    
< asp:Label ID = " lblShow "  runat = " server "  ForeColor = " Red "  Text = " You are not activated..Please do it.. " ></ asp:Label >
                
</ td >
            
</ tr >
        
</ table >
    
    
</ div >
</ form >

 

 

 

后台代码:

ExpandedBlockStart.gif 代码
 1  using  System;
 2  using  System.Collections.Generic;
 3  using  System.Linq;
 4  using  System.Web;
 5  using  System.Web.UI;
 6  using  System.Web.UI.WebControls;
 7  using  System.Data;
 8  using  System.Data.SqlClient;
 9  using  System.Configuration;
10 
11  namespace  EmailActive
12  {
13       public   partial   class  Login : System.Web.UI.Page
14      {
15           protected   void  Page_Load( object  sender, EventArgs e)
16          {
17              lblShow.Visible  =   false ;
18          }
19 
20           protected   void  btnLog_Click( object  sender, EventArgs e)
21          {
22               string  strConn  =  ConfigurationManager.ConnectionStrings[ " Reg " ].ConnectionString;
23              SqlConnection conn  =   new  SqlConnection(strConn);
24              conn.Open();
25              SqlCommand cmd  =  conn.CreateCommand();
26              cmd.CommandText  =   " Select Verified from tblUsers where UserName = ' "   +  txtUserName.Text  +   " ' and Password = ' "   +  txtPassword.Text  +   " ' " ;
27               string  strVer  =  cmd.ExecuteScalar().ToString();
28              conn.Close();
29 
30               // 判断Verified字段是否为1如果为1说明已经激活..
31               if  (String.Compare(strVer,  " 1 " ==   0 )
32              {
33                  Response.Redirect( " Success.aspx " );
34 
35              }
36               else
37              {
38                  lblShow.Visible  =   true ;
39              }
40            
41             
42          }
43      }
44  }
45 

 

 

 Success.aspx(成功激活后的界面) 不介绍了(就一个标签显示成功),看源代码..

 Web.config中数据库连接自己修改下。

 

 注意:发送邮件时,发现smtp.126.com,smtp.163.com不能使用,于是使用smtp.gmail.com发送邮件。 

通过这个例子学到不少东西, 发送邮件,QueryString传值,解决乱码问题。。

 

在WIN 7 + Visual Web Develop 2008调试通过。 

 


 

代码地址http://www.lovetc.xinxiban.com/Upload/EmailActive.rar

转载于:https://www.cnblogs.com/loveasm/archive/2009/12/12/1622541.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值