Gmail Contact form asp.net C#

So I was using this code to send an Email via a Contact form, and it was working great. so now that the site I’m working on is almost done, I wanted to change the email I want the contact form to send to, both are Gmail accounts.

When I changed the email in the code, it just stopped working. When I try to use the form, it goes streight to Catch. I did of course check the login information several times, and allowed the new gmail I’m now using for the contact form, to “allow less secure apps”, and still nothing is happening. So I tried to change back to my old Gmail account and it works just fine again…
Is this because i missed some other option for my Gmail i have to allow?
any tips / help would be greatly appreciated.

protected void ButtonSendBesked_Click(object sender, EventArgs e)
    {
        try
        {
            if (Page.IsValid)
            {
                MailMessage mailMessage = new MailMessage();
                mailMessage.From = new MailAddress("my@email.com");
                mailMessage.To.Add("my@email.com");
                mailMessage.Subject = ("Kontakt Massør Anne");

                mailMessage.Body = "<b>Kontakt Navn : </b>" + textbox_name.Text + "<br/>"
                    + "<b>Email : </b>" + textbox_email.Text + "<br/>"
                    + "<b>Besked : </b>" + textbox_message.Text;
                mailMessage.IsBodyHtml = true;


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

                Label_Kontakt.Text = "Thank you for contacting us!";

                textbox_name.Enabled = false;
                textbox_email.Enabled = false;
                textbox_message.Enabled = false;

                ButtonSendBesked.Enabled = false;
            }
        }
        catch (Exception ex)
        {  
            Label_Kontakt.ForeColor = System.Drawing.Color.Red;
            Label_Kontakt.Text = "There was an error, try again later.";
        }

    }


Source: forms

Leave a Reply