15

I am getting an error when trying to send an e-mail through my web service. I have tried enabling access to less secure apps disabling 2-step verification and logging into the account via a web browser. None of the solutions on SO have worked for me. I am still getting:

Error: 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.

What can I do to fix this issue?

namespace EmailService
{
    public class Service1 : IService1
    {    
        public string SendEmail(string inputEmail, string subject, string body)
        {
            string returnString = "";
            try
            {
                MailMessage email = new MailMessage();
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";

                // set up the Gmail server
                smtp.EnableSsl = true;
                smtp.Port = 587;
                smtp.Credentials = new System.Net.NetworkCredential("myemail@gmail.com", "mypassword");
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtp.UseDefaultCredentials = false;

                // draft the email
                MailAddress fromAddress = new MailAddress("cse445emailservice@gmail.com");
                email.From = fromAddress;
                email.To.Add(inputEmail);
                email.Subject = body;
                email.Body = body;

                smtp.Send(email);

                returnString = "Success! Please check your e-mail.";
            }
            catch(Exception ex)
            {
                returnString = "Error: " + ex.ToString();
            }
            return returnString;
        }
    }
}
Johnny
  • 675
  • 3
  • 15
  • 25

1 Answers1

37

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();
    }
}

If the above code don't work , try to change it like the following code :

    SmtpClient client = new SmtpClient();
    client.Host = "smtp.gmail.com";
    client.Port = 587;
    client.EnableSsl = true;
    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    client.UseDefaultCredentials = false;
    client.Credentials = new NetworkCredential("YourMail@gmail.com", "YourPassword");
Eqbal Sohrabi
  • 826
  • 6
  • 23
  • I've already tried both of those things and it still doesn't work. – Johnny Apr 06 '15 at 22:24
  • 5
    It turns out that `smtp.UseDefaultCredentials = false;` must go before `smtp.Credentials = new System.Net.NetworkCredential("myemail@gmail.com", "mypassword");` I simply swapped those lines and everything works fine. – Johnny Apr 06 '15 at 22:50
  • you means to change `smtp.UseDefaultCredentials = true;` to `smtp.UseDefaultCredentials = false;` and place it befor `smtp.Credentials = new System.Net.NetworkCredential("myemail@gmail.com", "mypassword");` if yes tell me to edit my answer. – Eqbal Sohrabi Apr 07 '15 at 04:28
  • Yes, I think default credentials need to be false and that line does have to be before the other one. – Johnny Apr 07 '15 at 04:33
  • Ive been trying to get this to work for hours and all I had to do was click that Continue button! Thank you so much you are a life savor – Bad Dub Sep 09 '16 at 22:20
  • Can someone update the answer to what actually works? – rollsch Oct 22 '16 at 05:21
  • I get a "Null" exception where it doesn't tell me what the actual error is. – rollsch Oct 22 '16 at 05:40