Error in Sending Email via a SMTP Client

23,342

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);   
}
Share:
23,342
Sike12
Author by

Sike12

learning to code at the moment.

Updated on October 24, 2020

Comments

  • Sike12
    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
    Sike12 over 10 years
    This will report an error message saying "{"The remote name could not be resolved: 'smtp.google.com'"}
  • Sike12
    Sike12 over 10 years
    Hi Pete. I think it could be some firewall issues or something to do with my googlemail username and password.
  • Pete
    Pete over 10 years
    Are 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
    Pete over 10 years
    Try using Port 465 instead.
  • Pete
    Pete over 10 years
    And 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 the Send().
  • Sike12
    Sike12 over 10 years
    yes 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
    Pete over 10 years
  • Sike12
    Sike12 over 10 years
    The "Operation Timed out" message at least gives us a clue that we are getting nearer to the solution :)
  • Sike12
    Sike12 over 10 years
  • Sike12
    Sike12 over 10 years
    if you change it to smtp.gmail.com (with googlemail credentials) or smtp.live.com (with hotmail credentials) then this would work fine :)
  • Edward Brey
    Edward Brey over 8 years
    The SmtpClient should be wrapped in a using directive so that it gets disposed in a timely fashion.