sandeep sandy

sandeep sandy

  • NA
  • 18
  • 6.7k

sending mail from gmail

May 12 2016 8:19 AM
 Hii,
i am facing the following error while sending the mail following is my code please help 
 
“The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.
 
thanks in advance 
 
-----------------------------------------------------------------------------------------------
 
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
MailMessage Msg = new MailMessage();
// Sender e-mail address.
Msg.From = new MailAddress(txtUsername.Text);
// Recipient e-mail address.
Msg.To.Add(txtTo.Text);
Msg.Subject = txtSubject.Text;
Msg.Body = txtBody.Text;
// your remote SMTP server IP.
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials=new System.Net.NetworkCredential(txtUsername.Text,txtpwd.Text);
smtp.EnableSsl = true;
smtp.Send(Msg);
Msg = null;
Page.RegisterStartupScript("UserMsg", "<script>alert('Mail sent thank you...');if(alert){ window.location='SendMail.aspx';}</script>");
}
catch (Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex);
}
}
----------------------------------------------------------------------------------------------------------- 

Answers (3)