0

I'm sending simple email messages in my application using smtp client and I was using this code before and it just works fine. Now, when I tried to run my project again from my local host computer and try to send email messages. I got a runtime error that says

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.

I don't know what just happened since it was working fine before. Now I can't send email and all I've got is this error. I could hardly troubleshoot what went wrong. How do I resolve this? Here's my code below: Thanks...

SmtpClient client = new SmtpClient();

                client.Host = "smtp.gmail.com"; 

                client.Port = 587;

                client.EnableSsl = true;

                client.Credentials = new System.Net.NetworkCredential(@"myemailaddress@gmail.com",@"myemailpassword");            

                // create message
                MailMessage message = new MailMessage();

                message.From = new MailAddress(TextBox4.Text);
                message.To.Add(new MailAddress(TextBox1.Text));
                message.Subject = TextBox2.Text;
                message.Body = TextBox3.Text; //body of the message to be sent
                message.BodyEncoding = System.Text.Encoding.UTF8;
                message.IsBodyHtml = true;
                // message.Subject = "subject";
                message.SubjectEncoding = System.Text.Encoding.UTF8;

               try
               {
                    client.Send(message);
                    Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('Mail has been successfully sent!')", true);

                }
                catch (SmtpException ex)
                {

                    Response.Write(ex.Message);

                }
                finally
                {
                    // Clean up.
                    message.Dispose();
                }
timmack
  • 590
  • 2
  • 12
  • 43

1 Answers1

1

Just Go here : Less secure apps , Log on using your Email and Password which use for sending mail in your c# code , and choose Turn On.

Also please go to this link and click on Continue Allow access to your Google account

also I edit it little bit :

public string sendit(string ReciverMail)
{
    MailMessage msg = new MailMessage();

    msg.From = new MailAddress("YourMail@gmail.com");
    msg.To.Add(ReciverMail);
    msg.Subject = "Hello world! " + DateTime.Now.ToString();
    msg.Body = "hi to you ... :)";
    SmtpClient client = new SmtpClient();
    client.UseDefaultCredentials = true;
    client.Host = "smtp.gmail.com";
    client.Port = 587;
    client.EnableSsl = true;
    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    client.Credentials = new NetworkCredential("YourMail@gmail.com", "YourPassword");
    client.Timeout = 20000;
    try
    {
       client.Send(msg);
        return "Mail has been successfully sent!";
    }
    catch (Exception ex)
    {
        return "Fail Has error" + ex.Message;
    }
    finally
    {
       msg.Dispose();
    }
}
Eqbal Sohrabi
  • 826
  • 6
  • 23
  • I'm on my account settings now after I click the <- Less Secure Apps. So which part I should turn on there. I can't find the option to Turn On. Thank u. – timmack Apr 04 '15 at 12:17
  • Let me remind u that I've changed the password of this gmail account and after I changed the password that's where I noticed that my app can't send an email anymore using this gmail account. – timmack Apr 04 '15 at 12:28
  • after you login into your account , click on this [link](https://www.google.com/settings/security/lesssecureapps) , then you should see something like this image : [Page screenshot](http://i57.tinypic.com/j6pkle.jpg) – Eqbal Sohrabi Apr 05 '15 at 04:12
  • Thanks I got it now. The reason why I don't have the Turn On option same as the screen you've given is because my gmail account was set for 2-Step verification enabled so I have turn it that off in order to have option to turn on for access for less secure apps. I realize that we have 2 options for these either we have do this step or just totally use another new gmail account. – timmack Apr 05 '15 at 05:59
  • One more thing before I upvote your answer. If I deploy my apps to the web host server or in the internet we understand that we use port 587 for the gmail smtp client. Do this still works on the web host server? Thanks – timmack Apr 05 '15 at 06:02
  • Yes this will works fine in web servers. but also may have this problem Again , this is Google security. also you can test this smtp : **smtp.googlemail.com** – Eqbal Sohrabi Apr 05 '15 at 10:47
  • I edited my answer and add another links in that, I upload **Edited Code** and upload here : [Send Test Mail](http://karmozd.com/SendMailTest.aspx) and you can also test it. – Eqbal Sohrabi Apr 05 '15 at 11:01
  • 1
    Thank you sir! It helps me understand security policies for a particular account like gmail. – timmack Apr 05 '15 at 12:49