0

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("jasmine.afnan@gmail.com");
    //Configure the address we are sending the mail from
    MailAddress address = new MailAddress("jasmine@gmail.com");
    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("jasmine.afnan@gmail.com", "*");
    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

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Afnan Humdan
  • 195
  • 3
  • 12
  • 2
    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 Dec 15 '15 at 12:15
  • 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 HERE](https://www.google.com/settings/security/lesssecureapps) – Equalsk Dec 15 '15 at 12:24
  • Post your exact exception. – Rajeesh Menoth Dec 15 '15 at 12:36
  • 1
    I get $10 if the exception is pretty much `The server response was: 5.5.1 Authentication Required.` – Equalsk Dec 15 '15 at 12:37
  • I updated the post, equalsk you are right :D – Afnan Humdan Dec 15 '15 at 12:39
  • @AfnanHumdan...Google may block sign in attempts from some apps or devices when you try to login from some app. As I said checking this answer may help you: http://stackoverflow.com/a/32475872/2946329 – Salah Akbari Dec 15 '15 at 12:41
  • @AfnanHumdan check the updated answer. – Rajeesh Menoth Dec 15 '15 at 12:49

1 Answers1

0

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.
Rajeesh Menoth
  • 1,704
  • 3
  • 17
  • 33