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