Click here to Skip to main content
Click here to Skip to main content

Email Sendig OnClick - Verification Email

, 16 Aug 2013
Rate this:
Please Sign up or sign in to vote.
This Article is written for those who need help for sending simple email or also Verificetion type Email..

Simple Email Sending Code:

For Sending Email you should first add the given below lines to your project Web.Config file,

<configuration>
<!--Email Sending Required Setting-->
  <system.net>
    <mailSettings>
      <smtp from="onlinehelpdesk@mustafaiqbal.cu.cc">
        <network host="mx1.1freehosting.com" port="2525"
        userName="onlinehelpdesk@mustafaiqbal.cu.cc" //Your Email
        password="123456" /> //Your Password
      </smtp>
    </mailSettings>
  </system.net>
</configuration> 

Okay, Now add the below given code to you page and OnClick or whenever you need to send Email Call that Function by sending single argument like; SendEmail(txtEmail.text.ToString().Trim());,

private void SendEmail(string email)
    {
        try
        {
            MailMessage message = new MailMessage();
            message.From = new MailAddress("onlinehelpdesk@mustafaiqbal.cu.cc");
            message.To.Add(new MailAddress(email));
            message.Subject = "Code Project - Registration";
            message.Body = "Congragultions! You Are Now Registered at Code Project. \n\nLink: www.Codeproject.com";
            SmtpClient client = new SmtpClient();
            client.Send(message);
        }
        catch (Exception ex)
        {
            string ErrMsg = "alert('Found Error at Sending Email Function, " + ex + "');";
            Page.ClientScript.RegisterStartupScript(this.GetType(), "ButtonClickEventScript", ErrMsg, true);
        }
        finally
        {
            sqlcon.Close();
        }
    }	 

Verification Email Sending:

For Verification Process, First you should add "User_Status" with data type "int" column to your database table, and when user click on register button, you can set the value of "User_Status" to "0" or set the Defaut_Value of column to "0" But on the Backend when user click on register button, you can generate a random number and send it user vai Email and, also redirect user to verification page (or take two panels one for registration, one for verification, then you should set visible = false to registration panel and visible = true to verification Panel on button click). user will receve your verification code and then copy paste it at your page text field and then click on verify. so it easy, now you can match previously generated number(generated at the time of email) to the input number and if match set "User_Status" column value to "1".

First Define Property Class, so you can set you verification code to it;

Property Class  

protected static string PropVerificationCode { get; set; }

Email with Random Verification Code to User

Code for generating and sending verification code via email 

private void SendEmail(string email)
    {
        try
        {
            //Sending Registration Email to the User;
            var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            var random = new Random();
            VerificationCode = new string(
                Enumerable.Repeat(chars, 8)
                          .Select(s => s[random.Next(s.Length)])
                          .ToArray());

            MailMessage message = new MailMessage();
            message.From = new MailAddress("onlinehelpdesk@mustafaiqbal.cu.cc");
            message.To.Add(new MailAddress(email));
            message.Subject = "Code Project - Registration";
            message.Body = "Congragultions! You Are Now Registered at Online Help Desk. \n\nYour Account Verification Code: " + VerificationCode + "\n\nCopy & Paste that code to the website and click conform, to Verify your email. \n\n\n  Link: www.onlinehelpdesk.com";
            SmtpClient client = new SmtpClient();
            client.Send(message);
        }
        catch (Exception ex)
        {
            string ErrMsg = "alert('Found Error at Sending Email Function, " + ex + "');";
            Page.ClientScript.RegisterStartupScript(this.GetType(), "ButtonClickEventScript", ErrMsg, true);
        }
        finally
        {
            sqlcon.Close();
        }
    }	 

 After Sending Verification Code to the User, you can add a text field for input and a button, on which you can check that input and gererated value is equal or not, so I am showing you only the code for matching throug text field text and property value, you can add fields by yourself 

   try
        {
            if (txtVerificationCode.Text.ToString() == PropVerificationCode)
            {
                sqlcon.Open();
                cmd = new SqlCommand("update user_table set user_status = 1 where user_email = '" + txtEmail.Text.ToString().Trim() + "'", sqlcon);
                cmd.ExecuteNonQuery();
                //Message Box;
                string ErrMsg = "alert('Thank You For Verification!');";
                Page.ClientScript.RegisterStartupScript(this.GetType(), "ButtonClickEventScript", ErrMsg, true);
            }
        }
        catch (Exception ex)
        {
            string ErrMsg = "alert('Found Error in Updating User Status, " + ex + "');";
            Page.ClientScript.RegisterStartupScript(this.GetType(), "ButtonClickEventScript", ErrMsg, true);
        }
        finally
        {
            sqlcon.Close();
        } 

 Note: If you are using mail server like Gmail, so, I want to tell you that first you should disable all extra security from you account or profile then try to send email from that address. other wise email will not send!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

MustafaIqbal

Pakistan Pakistan
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 16 Aug 2013
Article Copyright 2013 by MustafaIqbal
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid