1

despite all the topics about this on Stackoverflow. I can't manage to find the solution of this problem:

i have this exception :

SMTP server needs a secure connection or the client is not authentificated. Reply from the server is : 5.5.1 Authentication Required. Learn more at

using this code:

    var fromAddress = new MailAddress(user.Email, "From Name");
    var toAddress = new MailAddress("myadress@gmail.com", "To Name");
    const string fromPassword = "fromPassword";
    const string subject = "Subject";
    const string body = "Body";

    var smtp = new SmtpClient
    {
        Host = "smtp.gmail.com",
        Port = 587,
        EnableSsl = true,
        DeliveryMethod = SmtpDeliveryMethod.Network,
        UseDefaultCredentials = false,
        Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
        Timeout = 20000
    };
    using (var message = new MailMessage(fromAddress, toAddress)
    {
        Subject = subject,
        Body = body
    })
    {
        smtp.Send(message);
    }

What am i missing please? thanks for your help

  • 8
    This was asked and answered on SO before - did you search before asking? – Oded Apr 19 '12 at 13:04
  • 3
    http://stackoverflow.com/questions/704636/sending-email-through-gmail-smtp-server-with-c-sharp – Prix Apr 19 '12 at 13:05
  • Check out the following: http://stackoverflow.com/questions/704636/sending-email-through-gmail-smtp-server-with-c-sharp – Tomtom Apr 19 '12 at 13:06
  • 2
    Check your password and ensure it is a strong one, as far as Google is concerned. – Oded Apr 19 '12 at 13:06

3 Answers3

1

You are not missing anything. I suspect you got your password wrong. You didn't forget to change it from "fromPassword" to what it really should be did you?

This is a cleaner implementation of the same task

Community
  • 1
  • 1
BlueVoodoo
  • 3,626
  • 5
  • 29
  • 37
1

This code worked for me

  SmtpClient sc = new SmtpClient("smtp.gmail.com");
  NetworkCredential nc = new NetworkCredential("username","password");
  //username doesn't include @gmail.com         
   sc.UseDefaultCredentials = false;
   sc.Credentials = nc;
   sc.EnableSsl = true;
   sc.Port = 587;
     try
      {
       sc.Send(mm);
      } 
     catch (Exception ex)
      {
        EventLog.WriteEntry("Error Sending", EventLogEntryType.Error);
      }
Shyam sundar shah
  • 2,473
  • 1
  • 25
  • 40
1

Either you have typed your password incorrectly or (more likely) you are trying to use your normal password after you have enabled Google's 2-Step Authentication

You need to generate and use an application specific password

sgmoore
  • 15,694
  • 5
  • 43
  • 67