title: Why won't SmtpClient authenticate in my powershell script?
body: <p>I'm trying to send an email from a PowerShell script, but I cannot seem to get it to properly authenticate with my SMTP server.</p>
  
  <pre><code>$fromEmail = 'local@example.com';
  $toEmail = 'remote@example.net';
  $mailUser = 'myUsername';
  $mailPassword = 'myPassword';
  $smtpServer = 'smtp.example.com';
  $smtpPort = 587;
  $content = 'Email message content';
  $subject = 'Email message subject';
  
  $credentials = New-Object System.Net.NetworkCredential $mailUser, $mailPassword;
  
  $server = New-Object System.Net.Mail.SmtpClient $smtpServer, $smtpPort;
  $server.UseDefaultCredentials = $false;
  $server.EnableSSL = $true;
  $server.Credentials = $credentials
  $server.Send($fromEmail, $toEmail, $subject, $content);
  </code></pre>
  
  <p>When I try to run the above in a powershell prompt, it results in an error specifying relay access denied.</p>
  
  <pre><code>Exception calling "Send" with "4" argument(s): "Client does not have permission to submit mail to this server. The
  server response was: 4.7.1 &lt;remote@example.net&gt;: Relay access denied"
  At line:1 char:1
  + $server.Send($fromEmail, $toEmail, $subject, $content);
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
      + FullyQualifiedErrorId : SmtpException
  </code></pre>
  
  <p>If I try sending an email using Thunderbird or PHP's Swiftmailer and the same server and credential settings it works fine.  From looking at the postfix logs it appears as though the SmtpClient is not even attempting to authenticate but instead just trying to send the mail anonymously.</p>
  
  <p>This is what a good log entry made by thunderbird looks like:</p>
  
  <pre><code>Jun 14 21:17:42 services postfix/submission/smtpd[16653]: connect from unknown[xxxx]
  Jun 14 21:17:43 services postfix/submission/smtpd[16653]: 59074F96E: client=unknown[xxxx], sasl_method=PLAIN, sasl_username=local@example.com
  </code></pre>
  
  <p>Whereas the entry made by SmtpClient looks like:</p>
  
  <pre><code>Jun 14 22:11:16 services postfix/submission/smtpd[16824]: connect from unknown[xxxx]
  Jun 14 22:11:16 services postfix/submission/smtpd[16824]: NOQUEUE: reject: RCPT from unknown[xxxx]: 454 4.7.1 &lt;remote@example.net&gt;: Relay access denied; from=&lt;local@example.com&gt; to=&lt;remote@example.net&gt; proto=ESMTP helo=&lt;WTFv2&gt;
  </code></pre>
  
  <p>As test I also tried sending via a gmail address using my gmail credentials and that also failed due to no authentication.</p>
  
  <pre><code>Exception calling "Send" with "4" argument(s): "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"
  At line:1 char:1
  + $server.Send($fromEmail, $toEmail, $subject, $content);
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
      + FullyQualifiedErrorId : SmtpException
  </code></pre>
  
  <p>What am I missing here?  Why does the .net SmtpClient not send the authentication to allow the mail to go through?</p>