Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why won't SmtpClient authenticate in my powershell script?

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 = '[email protected]';
$toEmail = '[email protected]';
$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 <[email protected]>: 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, [email protected]

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 <[email protected]>: Relay access denied; from=<[email protected]> to=<[email protected]> 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?

like image 608
kicken Avatar asked Oct 30 '22 06:10

kicken


1 Answers

Can you try this way, this is the one I use for Gmail, I use System.Net.NetworkCredential for the credentials (coming from my C# code) :

$smtpServer = "smtp.gmail.com"
$smtpServerport = "587"
$emailSmtpUser = "[email protected]"
$emailSmtpPass = "toto.123"

$emailMessage = New-Object System.Net.Mail.MailMessage
$emailMessage.From = "[email protected]"
foreach ($addr in $to)
{
  $emailMessage.To.Add($addr)
}
foreach ($attachment in $attachments)
{
  $emailMessage.Attachments.Add($attachment)
}
$emailMessage.Subject = "The subject"
$emailMessage.IsBodyHtml = $true
$emailMessage.Body = $body

$smtpClient = New-Object System.Net.Mail.SmtpClient($smtpServer , $smtpServerport)
$smtpClient.EnableSsl = $true
$smtpClient.Credentials = New-Object System.Net.NetworkCredential($emailSmtpUser , $emailSmtpPass);

$SMTPClient.Send( $emailMessage )
like image 167
JPBlanc Avatar answered Nov 27 '22 06:11

JPBlanc