title: C# sending authenticated mail through smtp
body: <p>i'm having a little issue with my sendmail script. My mail can't be delivered at one of my internal distribution lists. I'm getting the returned message</p>
  
  <pre><code>RESOLVER.RST.AuthRequired; Authentication required
  </code></pre>
  
  <p>my c# code looks like this:</p>
  
  <pre><code>public void SendMail(int supplierValue, List&lt;Location&gt; locationList, string meldingsNr, string date, string fromT, string tillT)
      {
      var mailFrom = "mail@mail.com";
      SmtpClient client = new SmtpClient("server ip",25);
      client.UseDefaultCredentials = false;
      NetworkCredential basicAuthInfo = new NetworkCredential("username", "password");
      client.Credentials = basicAuthInfo;
      client.EnableSsl = false;
  
      // Create some empty vars
  
      var toList = "";
      var ccList = "";
      var mailSubject = "";
      var mailBody = "";
      List&lt;string&gt; locationListString = new List&lt;string&gt;();
      List&lt;string&gt; locationListString2 = new List&lt;string&gt;();
  
      // Populate toList with var llString
  
      ** code removed **
  
      // switch on supplier Value to fill toList, ccList and mailSubject
  
      ** code removed **
  
      // Create mail message
  
      MailMessage message = new MailMessage();
      MailAddress mailAddress = new MailAddress(mailFrom);
      message.To.Add(toList);
      message.From = mailAddress;
      message.CC.Add(ccList);
  
      // set subject and encoding
  
      message.Subject = mailSubject;
      message.SubjectEncoding = Encoding.UTF8;
  
      // set body and encoding
  
      message.Body = mailBody;
      message.BodyEncoding = Encoding.UTF8;
      message.IsBodyHtml = true;
  
      try
      {
          // Send mail 
          client.SendCompleted += (s, e) =&gt;
          {
              client.Dispose();
              message.Dispose();
          };
          client.SendAsync(message, null);
  
      } catch (SmtpException ex)
      {
          MessageBox.Show(ex.InnerException.Message);
  
      } catch (System.Exception ex)
      {
         MessageBox.Show(ex.InnerException.Message);
      }
  }
  </code></pre>
  
  <p>I applied this suggestion:
  <a href="https://stackoverflow.com/questions/298363/how-can-i-make-smtp-authenticated-in-c-sharp">How can I make SMTP authenticated in C#</a></p>
  
  <p><em>Ensure you set SmtpClient.Credentials after calling SmtpClient.UseDefaultCredentials = false. The order is important as setting SmtpClient.UseDefaultCredentials = false will reset SmtpClient.Credentials to null.</em></p>
  
  <p>but this didn't work. Is there anything else wrong with my code ? </p>