timmack
timmack

Reputation: 590

How to fix the smtp authentication runtime error when sending email using smtp client?

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(@"[email protected]",@"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();
                }

Upvotes: 0

Views: 3244

Answers (1)

Eqbal Sohrabi
Eqbal Sohrabi

Reputation: 841

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("[email protected]");
    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("[email protected]", "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();
    }
}

Upvotes: 1

Related Questions