1

我正在为我的网页创建一个注册页面,我希望人们收到一封激活邮件,但我不知道如何实现这一点。

这就是我调用数据库的代码的样子

DBconnect dbcon = new DBconnect();
        if (dbcon.Checkmail(tbx_Remail.Text) == true)
        {
            lbl_Remail.Text = "This email is already used";
        }
        else
            lbl_Remail.Text = "";
        if (tbx_Rpassword.Text == tbx_Rrenter.Text && lbl_Remail.Text!="")
        {
            Mail mail = new Mail();
            Hashing hash = new Hashing();
            Account acc = new Account("user", tbx_Rname.Text, tbx_Rcity.Text, tbx_Rstate.Text, tbx_Remail.Text, tbx_Rpostal.Text, tbx_Radress.Text, tbx_Rtelephone.Text, hash.hashpass(tbx_Rpassword.Text, "asIoqc"));
            dbcon.CreateAccount(acc);
            mail.ActivationEmail(tbx_Remail.Text, "test", tbx_Remail.Text);
            btn_Rreset_Click(this, new EventArgs());
        }

我的数据库设置了一个激活列,该列的默认设置为 1,当激活 = 1 时,您无法登录。

这是邮件类的样子

public bool ActivationEmail(string username, string message, string email)
    {
        try
        {
            MailMessage Message = new MailMessage();
            SmtpClient Smtp = new SmtpClient();
            System.Net.NetworkCredential SmtpUser = new System.Net.NetworkCredential();

            // Basis gegevens email
            Message.From = new MailAddress("xxx@xxx.com", "no-reply@xxx.com");

            Message.To.Add(new MailAddress(email, username));
            Message.IsBodyHtml = true;

            // Gegevens onderwerp & Body
            Message.Subject = "Account Activation";
            Message.Body = message;

            // SMTP Auth, een emailadres welke is aangemaakt in het control panel
            SmtpUser.UserName = "xxx";
            SmtpUser.Password = "xxx";

            // Bericht verzenden
            Smtp.UseDefaultCredentials = false;
            Smtp.Credentials = SmtpUser;
            Smtp.Port = 80;
            Smtp.Host = "xxx";
            Smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            Smtp.Send(Message);
            return true;
        }
        catch
        {
            return false;
        }
    }

我希望有人可以帮助我现在应该做什么,因为我以前从未这样做过。

4

2 回答 2

1

这是我现在解决的方法,

在我的数据库中,我创建了一个“激活”列,当您登录时会检查此列,如果为 1,则无法登录,如果为 2,则可以登录。我还创建了一个列“activationcde”,该列保存了您创建新帐户时创建的唯一代码。

这是我在服务器端的代码

 DBconnect dbcon = new DBconnect();
        if (dbcon.Checkmail(tbx_Remail.Text) == true)
        {
            lbl_Remail.Text = "This email is already used";
        }
        else
            lbl_Remail.Text = "";
        if (tbx_Rpassword.Text == tbx_Rrenter.Text && lbl_Remail.Text=="")
        {
            Mail mail = new Mail();
            Hashing hash = new Hashing();
            string confirmationcode = tbx_Rpostal.Text + tbx_Remail.Text;
            Account acc = new Account("user", tbx_Rname.Text, tbx_Rcity.Text, tbx_Rstate.Text, tbx_Remail.Text, tbx_Rpostal.Text, tbx_Radress.Text, tbx_Rtelephone.Text, hash.hashpass(tbx_Rpassword.Text, "asIoqc"));
            dbcon.CreateAccount(acc);
            if (!mail.ActivationEmail(tbx_Remail.Text, "Dear " + tbx_Rname.Text + ", <br /><br />By pressing the link underneed you confirm you're account. <br /><a href=http://cngraphix.com/validate.aspx?Confirm=" + confirmationcode + ">Click here to confirm</a> <br /><br />Best regards,<br />The Management Team.", tbx_Remail.Text))
            {
                Response.Redirect("index.aspx?nullexeption=1");
            }
            else
            {
                dbcon.SetActivation(confirmationcode, tbx_Remail.Text);
            }
            btn_Rreset_Click(this, new EventArgs());
        }

这是我在确认页面上的代码

protected void Page_Load(object sender, EventArgs e)
    {
        DBconnect dbcon = new DBconnect();
        string confirm = Request.Params["Confirm"];
        if (dbcon.CheckActivation(confirm) == true)
        {
            dbcon.UpdateStatus(confirm);
        }
    }
于 2013-10-23T10:46:15.153 回答
0
create table emailValidation(
userId int not null,
username varchar(250) not null,
emailaddress varchar(250) not null,
activationkey varchar(150) not null
)

以上是验证表

发送验证邮件功能

Function sendVerifyEmail(userName)
    on error resume next
    ' Function used to send a verification code to the user's email address
    ' To Achieve that task we use DB Functions which get needed details 
    ' Process
    ' Kill the bad characters in userName
    ' Post the username to db for email id
    ' Format a new mail with new 10 digit random code
    str_Result=selectEmailAddress(Username,"")
    if str_Result="N/A" then
        ' No username just end the code no more process
    else
        ' create one random code 
        emailAddress=str_Result
        str_Randoms=generate_Password_Salt (10)
        ' insert the random number and the email to validate table
        mysql_Date=dateMYSQL(date)
        sql_Insert_data="insert into validate(emailid,validationCode,authcode) values('" & emailAddress & "','" & str_Randoms & "','VRFYS')"
        mysq_V_Constr=application("connectionstring")
        set db_Validate=server.CreateObject ("ADODB.Connection")
        db_Validate.Open mysq_V_Constr
        set rs_Validate=db_Validate.Execute(sql_Insert_Data)
        db_Validate.Close 
        set db_Validate=nothing
        str_Result="DataInsertFinished"
        ' get the standard format of verification mailer 
        ' it will be a text mailer
        if str_Result="DataInsertFinished" then
            str_Mail_Body=getVerificationMailBody(str_Randoms,"http://www.example.com/penpals/verify.asp?r=",UserName,emailAddress)
            ' You got mail body. Now send the mail using CDOSYS
            ' change the code when we are uploading
            'Response.Write str_mail_body

on error resume next
dim objMsg
dim objCfg
dim objFlds 
Set objMsg = CreateObject("CDO.Message")
set objCfg= CreateObject("CDO.Configuration")
Set objFlds = objCfg.Fields 

With objFlds
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mailserver" 
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
    .Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "your email address" 'YOUR EMAIL USERNAME
    .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "email address password" 'YOUR EMAIL PASSWORD
    .Update
End With

With objMsg
    Set .Configuration = objCfg
    .Subject = "Confirm your Free registration with Bepenfriends"
    .From = "Bepenfriends Dating <friends@bepenfriends.com>"
    .To = emailaddress
    .HTMLBody = str_Mail_Body
    .Send
    if err.number <> 0 then
        str_ERR_MSG=err.number & " " & err.Description 
    else
        str_ERR_MSG=""
    end if
End With 
set objFlds = Nothing
set objCfg = Nothing
set objMsg = Nothing 
        end if

    end if
    if err.number <> 0 then
        Response.Write err.number & err.Description 
        Response.End 
    end if
    sendVerifyEmail="NA"
End Function

在验证页面中更新数据库的代码

sql_Check_Login="update users set verified='1' where emailid='" & emailAddress & "'"
set rs2=db_checkLogin.Execute(sql_Check_login)

上面的代码是几年前为我的免费约会网站创建的。它仍然工作正常。

于 2013-10-21T15:20:27.963 回答