Click here to Skip to main content
15,888,401 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hi

i need your support guys as the below code of sending mail part is not working as i am trying many time but i haven't receive any mail, so could you please help me to fix this case there is no any error message but no any email is coming to my inbox. ("admin@gmail.com is just a sample and this email is suppose to be "From" and the email which will be written in "emailbox.Text" is "To (i mean user)")

C#
protected void btnSave_Click(object sender, EventArgs e)
{

    HttpCookie cookie = Request.Cookies.Get("Location");
    string Location = string.Empty;
    SqlConnection cn = new SqlConnection(sc);
    SqlCommand cmd = new SqlCommand();
    Location = cookie.Value;

    if (CheckBox1.Checked)
    {


        string sqlstatment = @"INSERT INTO UserInfo (UID, FN, LN, Password, RePass, Email,Country, State,City, Post, Img, Logo,RegDate,Address, UsrType,BizCateg,BizSubCateg) VALUES
        (@UID,@FN,@LN,@Password,@RePass,@Email,@Country,@State,@City,@Post,@Img,@Logo,@RegDate,@Address,@UsrType,@BizCateg,@BizSubCateg)";

        cmd.Connection = cn;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = sqlstatment;

        //Insert the parameters first
        cmd.Parameters.AddWithValue("@UID", UsrNme.Text);
        cmd.Parameters.AddWithValue("@FN", fnbox.Text);
        cmd.Parameters.AddWithValue("@LN", lnamebox.Text);
        cmd.Parameters.AddWithValue("@Password", passtxtbx1.Text);
        cmd.Parameters.AddWithValue("@RePass", passtxtbx2.Text);
        cmd.Parameters.AddWithValue("@Email", emailbox.Text);
        cmd.Parameters.AddWithValue("@Country", cookie.Value);
        cmd.Parameters.AddWithValue("@State", statedrdolst.SelectedItem.Text);
        cmd.Parameters.AddWithValue("@City", citiesdrdolst.SelectedItem.Text);
        cmd.Parameters.AddWithValue("@Post", postbox.Text);

        cmd.Parameters.AddWithValue("@Logo", logoFileUpload.FileName);
        cmd.Parameters.AddWithValue("@Address", regaddrstxtbx.Text);
        cmd.Parameters.AddWithValue("@UsrType", UsrTypeDrDo.SelectedItem.Text);
        //cmd.Parameters.AddWithValue("@BizCateg", BizCateDDL.SelectedItem.Text);
        //cmd.Parameters.AddWithValue("@BizSubCateg", SubCatBizDDL.SelectedItem.Text);


        if (BizCateDDL.SelectedIndex < 0)
            cmd.Parameters.AddWithValue("@BizCateg", DBNull.Value);
        else
            cmd.Parameters.AddWithValue("@BizCateg", BizCateDDL.SelectedItem.Value);


        if (SubCatBizDDL.SelectedIndex < 0)
            cmd.Parameters.AddWithValue("@BizSubCateg", DBNull.Value);
        else
            cmd.Parameters.AddWithValue("@BizSubCateg", SubCatBizDDL.SelectedItem.Value);

        cmd.Parameters.AddWithValue("@RegDate", DateTime.Now);



        if (!string.IsNullOrEmpty(UsrNme.Text))
        {
            Lblcheckusername.Text = "User Name Already Exist";
            Lblcheckusername.ForeColor = System.Drawing.Color.Red;
        }
        else
        {
            Lblcheckusername.Text = "User Name Available";
            Lblcheckusername.ForeColor = System.Drawing.Color.Green;
        }

        string imgnouser = "/images/general/nouser.jpg";
        if (persimgFileUpload1.HasFile)
        {
            cmd.Parameters.AddWithValue("@Img", persimgFileUpload1.FileName);
            persimgFileUpload1.SaveAs(Server.MapPath("~/images/users/" + persimgFileUpload1.FileName));

        }
        else
        {

            cmd.Parameters.AddWithValue("@Img", imgnouser);

        }


        if (logoFileUpload.HasFile)
        {

            logoFileUpload.SaveAs(Server.MapPath("~/images/Logos/" + logoFileUpload.FileName));

        }

        SqlDataAdapter ad = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        ad.SelectCommand = cmd;
        ad.Fill(ds);
        Session["UsrNme"] = UsrNme.Text;


        const string username = "admin@gmail.com";

        const string password = "admin111";
        SmtpClient smtpclient = new SmtpClient();
        System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();

        MailAddress fromaddress = new MailAddress("admin@gmail.com");

        smtpclient.Host = "smtp.gmail.com";

        smtpclient.Port = 587;
        mail.From = fromaddress;

        mail.To.Add(emailbox.Text);

        mail.Subject = ("Test Subject");
        mail.IsBodyHtml = true;

        mail.Body = "This is test Mail";
        smtpclient.DeliveryMethod = SmtpDeliveryMethod.Network;
        smtpclient.Credentials = new System.Net.NetworkCredential(username, password);
        try
        {

            smtpclient.Send(mail);
            Response.Write("Email Has been sent successfully.");
        }
        catch (Exception ex)
        {

            Response.Write(ex.Message);
        }


        Response.Redirect("User panel.aspx");
        }

        else
        {
        Label1.Text = "please check the box to continue";
        }

}
Posted
Comments
ZurdoDev 23-Jan-15 22:01pm    
Are you sure there is no error? If not, then the problem is either in the SMTP server or the email did go and is in your spam folder.

Also, don't post all of your code, just the relevant stuff.

The first time I sent email through gmail using my account it would not go through until I actually logged into gmail. There was a message somewhere, can't remember where, asking to verify that my IP was OK to programatically send emails.

Please go through my blog you will get a working code to send email.
http://sameeakhtar786.blogspot.in/[^]
 
Share this answer
 
v2
Comments
Member 10690878 24-Jan-15 7:21am    
Hi @sameeakhtar786 many thanks for your reply, i have copy and paste your code and i change to my email setting but i would like to ask you i have already delete the previose code of the email part from my code but how i can call the method of your code
[no name] 24-Jan-15 12:13pm    
Nothing to do !!!Just call the method from where you want to send the message.
example--First change the settings in the method and call the method as below
Protected void btnsend_click(-------------------)
{
//do your stuff here
SendMail();
}
Member 10690878 24-Jan-15 15:08pm    
thanks @sameeakhtar786, i do it but i get this error message at line: smtp.Send(fromAddress, toAddress, subject, body); the error message is: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at
[no name] 25-Jan-15 4:33am    
Just change your password,and make the password strength strong.....Google policies has changed so we have have a strong passrword to send a mail.
Nothing to do !!!Just call the method from where you want to send the message.
example--First change the settings in the method and call the method as below
Protected void btnsend_click(-------------------)
{
//do your stuff here
SendMail();
}
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900