0
votes

I'm trying to build a contact form in my website, I'm using asp.net with C#, when I submit the message it does not reach the Email, is it because I'm using local server? or there are mistakes in my code? I got this error message in the catch section: "Your message failed to send, please try again."

this is the code behind page C#

try
{
    //Create the msg object to be sent
    MailMessage msg = new MailMessage();
    //Add your email address to the recipients
    msg.To.Add("[email protected]");
    //Configure the address we are sending the mail from
    MailAddress address = new MailAddress("[email protected]");
    msg.From = address;
    //Append their name in the beginning of the subject
    msg.Subject = txtName.Text + " :  " + ddlSubject.Text;
    msg.Body = txtMessage.Text;

    //Configure an SmtpClient to send the mail.
    SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
    client.EnableSsl = true; //only enable this if your provider requires it
    //Setup credentials to login to our sender email address ("UserName", "Password")
    NetworkCredential credentials = new NetworkCredential("[email protected]", "*");
    client.Credentials = credentials;

    //Send the msg
    client.Send(msg);

    //Display some feedback to the user to let them know it was sent
    lblResult.Text = "Your message was sent!";

    //Clear the form
    txtName.Text = "";
    txtMessage.Text = "";
}
catch
{
    //If the message failed at some point, let the user know
    lblResult.Text = "Your message failed to send, please try again.";
}

this is the exception

System.Net.Mail.SmtpException: 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 at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response) at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, MailAddress from, Boolean allowUnicode) at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception) at System.Net.Mail.SmtpClient.Send(MailMessage message) at Default2.btnSubmit_Click(Object sender, EventArgs e) in c:\Users\looly\Documents\Visual Studio 2015\WebSites\WebSite6\Default2.aspx.cs:line 46

1
We want to know the correct exception so just remove label inside the catch block.then Wrote exception..Catch(exception ex){throw ex};Rajeesh Menoth
At first glance your code seems OK. Have you configured Gmail correctly? It often blocks sending from apps it considers unsecured. Look if you have 2 step enabled, you might need need to generate an app specific password SEE HEREEqualsk
Post your exact exception.Rajeesh Menoth
I get $10 if the exception is pretty much The server response was: 5.5.1 Authentication Required.Equalsk
I updated the post, equalsk you are right :DAfnan Humdan

1 Answers

0
votes

The server response was: 5.5.1 Authentication

Based on this error.you check the following steps.

  • Enter the correct login password.
  • To remove 2-Step Verification.
  • You have to enable login from other timezone / ip for your google account.To do this follow the click here and allow access by clicking the continue button.
  • Go to security settings at the following Click here and enable less secure apps . So that you will be able to login from all apps.