0

I am trying to create a simple user registration form in which Email Verification is not working.

I am getting SMTP Exception. Please see images on below link:-

http://social.microsoft.com/Forums/en-US/7bcf7011-538d-4c56-8a59-65ee3434a062/email-verfication-in-simple-user-registration-form-using-aspnet-and-sql-is-not-working?forum=mas Please find code below:-

using System.Net;
using System.Net.Mail;

public partial class Help8 : System.Web.UI.Page
{ 
    protected void RegisterUser(object sender, EventArgs e)
    {
        int userId = 0;
        string constr = ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("Insert_User"))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Username", txtUsername.Text.Trim());
                    cmd.Parameters.AddWithValue("@Password", txtPassword.Text.Trim());
                    cmd.Parameters.AddWithValue("@Email", txtEmail.Text.Trim());
                    cmd.Connection = con;
                    con.Open();
                    userId = Convert.ToInt32(cmd.ExecuteScalar());
                    con.Close();
                }
            }
            string message = string.Empty;
            switch (userId)
            {
                case -1:
                    message = "Username already exists.\\nPlease choose a different username.";
                    break;
                case -2:
                    message = "Supplied email address has already been used.";
                    break;
                default:
                    message ="Registration successful. Activation email has been sent.";
                    SendActivationEmail(userId);
                    break;
            }
            ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + message + "');", true);
        }
    }

    private void SendActivationEmail(int userId)
    {
        string constr = ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString;
        string activationCode = Guid.NewGuid().ToString();
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("INSERT INTO tblUserActivation VALUES(@UserId, @ActivationCode)"))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.AddWithValue("@UserId", userId);
                    cmd.Parameters.AddWithValue("@ActivationCode", activationCode);
                    cmd.Connection = con;
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
            }
        }
        using (MailMessage mm = new MailMessage("sender@gmail.com", txtEmail.Text))
        {
            mm.Subject = "Account Activation";
            string body = "Hello " + txtUsername.Text.Trim() + ",";
            body += "<br /><br />Please click the following link to activate your account";
            body += "<br /><a href = '" + Request.Url.AbsoluteUri.Replace("Help8.aspx", "Help8_Activation.aspx?ActivationCode=" + activationCode) + "'>Click here to activate your account.</a>";
            body += "<br /><br />Thanks";
            mm.Body = body;
            mm.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.EnableSsl = true;
            NetworkCredential NetworkCred = new NetworkCredential("sender@gmail.com", "<password>");
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = NetworkCred;
            smtp.Port = 587;
            smtp.Send(mm);
        }
    }
}

using System.Configuration;
using System.Data.SqlClient;

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

        if (!this.IsPostBack)
        {
            string constr = ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString;
            string activationCode = !string.IsNullOrEmpty(Request.QueryString["ActivationCode"]) ? Request.QueryString["ActivationCode"] : Guid.Empty.ToString();
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand("DELETE FROM tblUserActivation WHERE ActivationCode = @ActivationCode"))
                {
                    using (SqlDataAdapter sda = new SqlDataAdapter())
                    {
                        cmd.CommandType = CommandType.Text;
                        cmd.Parameters.AddWithValue("@ActivationCode", activationCode);
                        cmd.Connection = con;
                        con.Open();
                        int rowsAffected = cmd.ExecuteNonQuery();
                        con.Close();
                        if (rowsAffected == 1)
                        {
                            ltMessage.Text = "Activation successful.";
                        }
                        else
                        {
                            ltMessage.Text = "Invalid Activation code.";
                        }
                    }
                }
            }
        }
    }
}
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
  • 1
    Doesn't `smtp.gmail.com` use port 465 for secure connection? – Izzy Aug 28 '14 at 15:10
  • Possible Duplicate? [The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required?](http://stackoverflow.com/questions/18503333/the-smtp-server-requires-a-secure-connection-or-the-client-was-not-authenticated) – Izzy Aug 28 '14 at 15:30
  • Please find my code:- – user3115435 Aug 28 '14 at 16:05

4 Answers4

0

You should enable application to access your gmail account. Go to this link on google https://accounts.google.com/b/0/DisplayUnlockCaptcha

Codemasta
  • 111
  • 4
0

Try without this line :

smtp.UseDefaultCredentials;

Since you are providing them manually.

bob
  • 774
  • 1
  • 7
  • 16
  • I uncomment this line but it is throwing the same exception.although i set smptp.usedefaultcreditals=true but its value is false in debug mode. – user3115435 Aug 28 '14 at 15:23
0

Change the Smtp port to 465 for secure connections.

Codemasta
  • 111
  • 4
  • it becomes slow, but the same exception. – user3115435 Aug 28 '14 at 15:52
  • Please scroll down the below link:- http://social.microsoft.com/Forums/en-US/7bcf7011-538d-4c56-8a59-65ee3434a062/email-verfication-in-simple-user-registration-form-using-aspnet-and-sql-is-not-working?forum=mas#f48d98dc-6f15-4147-a4b9-b7a29db493b4 – user3115435 Aug 28 '14 at 15:59
0
mm.BodyEncoding = Encoding.UTF8;    
smtp.UseDefaultCredentials = false;
Rajeesh Menoth
  • 1,704
  • 3
  • 17
  • 33