0

I've written C# code to send email on my gmail account from my own gmail account.but System is raising below exception whenever I click the submit button.

{"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"}

I've also receoved security alerts on my gmail account on clicking submit. How to resolve this? Here is my code:

contactus.aspx.cs

public partial class contactus : System.Web.UI.Page
{
    protected void Button1_Click(object sender, EventArgs e)
    {
        // To Do. Implementation will be discussed in Part 142
    }
}
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            if (Page.IsValid)
            {
                MailMessage mailMessage = new MailMessage();
                mailMessage.From = new MailAddress(" mygmail@gmail.com");
                mailMessage.To.Add("mygmail@gmail.com");
                mailMessage.Subject = txtSubject.Text;

                mailMessage.Body = "<b>Sender Name : </b>" + txtName.Text + "<br/>"
                    + "<b>Sender Email : </b>" + txtEmail.Text + "<br/>"
                    + "<b>Comments : </b>" + txtComments.Text;
                mailMessage.IsBodyHtml = true;


                SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
                smtpClient.EnableSsl = true;
                smtpClient.Credentials = new
                System.Net.NetworkCredential("mygmail@gmail.com", "password");
                smtpClient.Send(mailMessage);

                lblMessage.ForeColor = System.Drawing.Color.Blue;
                lblMessage.Text = "Thank you for contacting us";

                txtName.Enabled = false;
                txtEmail.Enabled = false;
                txtComments.Enabled = false;
                txtSubject.Enabled = false;
                Button2.Enabled = false;
            }
        }
        catch (Exception ex)
        {
            // Log the exception information to 
            // database table or event viewer
            lblMessage.ForeColor = System.Drawing.Color.Red;
            lblMessage.Text = "There is an unkwon problem. Please retry;
        }
    }

enter image description here

exception

enter image description here

Emmy
  • 145
  • 11

0 Answers0