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.
$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);
When I try to run the above in a powershell prompt, it results in an error specifying relay access denied.
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 <remote@example.net>: Relay access denied"
At line:1 char:1
+ $server.Send($fromEmail, $toEmail, $subject, $content);
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SmtpException
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.
This is what a good log entry made by thunderbird looks like:
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
Whereas the entry made by SmtpClient looks like:
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 <remote@example.net>: Relay access denied; from=<local@example.com> to=<remote@example.net> proto=ESMTP helo=<WTFv2>
As test I also tried sending via a gmail address using my gmail credentials and that also failed due to no authentication.
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
What am I missing here? Why does the .net SmtpClient not send the authentication to allow the mail to go through?