Question

I'm creating a registration page for my webpage, what I want is people to get an activation mail but I'm not sure how to achieve this.

this is what my code that calls the database looks like

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());
        }

my database is setup with a activation column, the default of this column is set at 1, when activation = 1 you aren't able to login.

here is what mail class looks like

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;
        }
    }

I hope someone can help me with what I should do now, since I've never done this before.

Was it helpful?

Solution

Here is how I solved it now,

in my database I created a column "activation" this column gets checked when you login, if it's 1 you can't login if it is 2 you can login. I also created a column "activationcde" this column saves the unique code that is created when you create a new account.

this is my code on serverside

 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());
        }

and this is my code on the confirm page

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);
        }
    }

OTHER TIPS

create table emailValidation(
userId int not null,
username varchar(250) not null,
emailaddress varchar(250) not null,
activationkey varchar(150) not null
)

above is the verification table

Function to send verification mail

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

Code to update the db in verify page

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

The above code was created some years ago for my free dating site. It is still working fine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top