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("[email protected]", "mypassword"); smtp.DeliveryMethod = SmtpDeliveryMethod.Network; smtp.UseDefaultCredentials = false; // draft the email MailAddress fromAddress = new MailAddress("[email protected]"); 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; } } } 
5
  • See if this helps : stackoverflow.com/questions/18503333/… Commented Apr 6, 2015 at 4:03
  • 1
    Have u tried to ping the server, if yes then the solution mentioned by Eghbal Sohrabi is ok Commented Apr 6, 2015 at 6:55
  • @Shubhojit ping the Google server? Commented Apr 6, 2015 at 22:28
  • @Johnny, I meant to say whether your fire wall is not blocking the request, this sometimes happens. Just to be sure... Commented Apr 7, 2015 at 10:24
  • So why is it that I can run my similar code successfully from localhost, but I get this error when running it from a web server? Commented Apr 18, 2016 at 21:23

1 Answer 1

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

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("[email protected]", "YourPassword"); 
Sign up to request clarification or add additional context in comments.

7 Comments

I've already tried both of those things and it still doesn't work.
It turns out that smtp.UseDefaultCredentials = false; must go before smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "mypassword"); I simply swapped those lines and everything works fine.
you means to change smtp.UseDefaultCredentials = true; to smtp.UseDefaultCredentials = false; and place it befor smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "mypassword"); if yes tell me to edit my answer.
Yes, I think default credentials need to be false and that line does have to be before the other one.
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
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.