Error in Sending Email via a SMTP Client
I don't think there's anything wrong with your code other than the e-mail addresses. I used this code to successfully send an e-mail from gmail to my personal account (ran it in LINQPad, actually). Simply replace the 3 string values with valid values for your accounts and you should be good to go:
MailMessage message = new System.Net.Mail.MailMessage();
string fromEmail = "[email protected]";
string fromPW = "mypw";
string toEmail = "[email protected]";
message.From = new MailAddress(fromEmail);
message.To.Add(toEmail);
message.Subject = "Hello";
message.Body = "Hello Bob ";
message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
using(SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587))
{
smtpClient.EnableSsl = true;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential(fromEmail, fromPW);
smtpClient.Send(message.From.ToString(), message.To.ToString(),
message.Subject, message.Body);
}
Comments
-
Sike12 over 3 years
This may be very trivial for you but i just couldn't figure out why am i getting this error message when i run my code. I looked some of the relative questions on this same website for eg Sending email through Gmail SMTP server with C# but none of them was helpful. Anyone willing to help please? using different assemblies are also acceptable. so if anyone got a working solution that would be appreciated.
Error Message = The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at
here is my code
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(); message.From = new MailAddress("[email protected]"); message.To.Add("[email protected]"); message.Subject = "Hello"; message.Body = "Hello Bob "; message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure; SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587); smtpClient.EnableSsl = true; smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; smtpClient.UseDefaultCredentials = false; smtpClient.Credentials = new NetworkCredential("MyGoogleMailAccount", "mygooglemailpassword"); smtpClient.Send(message.From.ToString(), message.To.ToString(), message.Subject, message.Body);
-
Sike12 over 10 yearsThis will report an error message saying "{"The remote name could not be resolved: 'smtp.google.com'"}
-
Sike12 over 10 yearsHi Pete. I think it could be some firewall issues or something to do with my googlemail username and password.
-
Pete over 10 yearsAre you sure you're using the right account & password? I would think you wouldn't get a response back if it were blocked by a firewall.
-
Pete over 10 yearsTry using Port 465 instead.
-
Pete over 10 yearsAnd also to verify, did you copy and paste my code and make changes (as opposed to modifying existing code)? It's important that you set
UseCredentials = false
prior to setting the Credentials. Otherwise it'll wipe out your credentials. In fact, you might verify the credentials in the debugger just prior ot theSend()
. -
Sike12 over 10 yearsyes Pete. I checked the account username and password by login into googlemail and i can login fine. Changing the port to 465 gives me the timeout message. Yes i am using your code and testing it
-
Pete over 10 yearsAlso see this: rochcass.wordpress.com/2013/05/25/…
-
Sike12 over 10 yearsThe "Operation Timed out" message at least gives us a clue that we are getting nearer to the solution :)
-
Sike12 over 10 years
-
Sike12 over 10 yearsif you change it to smtp.gmail.com (with googlemail credentials) or smtp.live.com (with hotmail credentials) then this would work fine :)
-
Edward Brey over 8 yearsThe
SmtpClient
should be wrapped in a using directive so that it gets disposed in a timely fashion.