I want to send an email from my application and i have written following code for sending mail
MailMessage msg = new MailMessage();
msg.From = new MailAddress("mymailid");
msg.To.Add("receipientid");
msg.Subject = "test";
msg.Body = "Test Content";
msg.Priority = MailPriority.High;
SmtpClient client = new SmtpClient();
client.Credentials = new NetworkCredential("mymailid", "mypassword", "smtp.gmail.com");
client.Host = "smtp.gmail.com";
client.Port = 587;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
client.UseDefaultCredentials = true;
client.Send(msg);
I am running it on localhost so what mistake i am doing to send it.
When i send button it gives an error like
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.
Code in Web.config file
<appSettings>
<add key="webpages:Version" value="2.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="PreserveLoginUrl" value="true" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="smtpServer" value="smtp.gmail.com" />
<add key="EnableSsl" value = "true"/>
<add key="smtpPort" value="587" />
<add key="smtpUser" value="sender@gmail.com" />
<add key="smtpPass" value="mypassword" />
<add key="adminEmail" value="sender@gmail.com" />
</appSettings>
<system.net>
<mailSettings>
<smtp from="sender@gmail.com">
<network host="smtp.gmail.com" password="mypassword" port="587" userName="sender@gmail.com" enableSsl="true"/>
</smtp>
</mailSettings>
</system.net>
what should i do to solve this error and send mail??
try to enable allow less secure app access.
Here, you can enable less secure app after login with your Gmail.
https://myaccount.google.com/lesssecureapps
Thanks.
I've searched and tried different things for hours.. To summarize, I had to take into consideration the following points:
smtp.gmail.com
instead ofsmtp.google.com
client.UseDefaultCredentials = false;
before setting credentialsclient.EnableSsl = true;
If these steps didn't help, check this answer.
Perhaps, you can find something useful on this System.Net.Mail FAQ too.
Try to login in your gmail account. it gets locked if you send emails by using gmail SMTP. I don't know the limit of emails you can send before it gets locked but if you login one time then it works again from code. make sure your webconfig setting are good.
I have the same problem.
I have found this solution:
Google may block sign in attempts from some apps or devices that do not use modern security standards. Since these apps and devices are easier to break into, blocking them helps keep your account safer.
Some examples of apps that do not support the latest security standards include:
Therefore, you have to enable Less Secure Sign-In in your google account.
After sign into google account, go to:
https://www.google.com/settings/security/lesssecureapps
In C#, you can use the following code:
-------------------
Info shared by Michael Freidgeim in below comments area:
Similar answer with screenshots https://stackoverflow.com/a/32457468/52277
some smtp servers (secure ones) requires you to supply both username and email, if its gmail then most chances its the 'Less Secure Sign-In' issue you need to address, otherwise you can try:
notice that the email from and the username are different unlike some implementation that refers to them as the same.
calling this method can be done like so:
Turn on less secure app from this link and boom...