The server response was: 5.7.1 : Relay access denied

The server response was: 5.7.1 : Relay access denied

Trying to send mail from Powershell to extern mailaccount but get Relay access denied.

# ======================
# Sending Email Section
# ======================

$strFrom = “mailfrom”
$strPassword = “Psw”
$strTo = “mailto”
$strSubject = “Alert”
$strSMTPServer = “”
$strPort = 587

$objEmailMessage = New-Object
$objEmailMessage.From = ($strFrom)
$objEmailMessage.Subject = $strSubject
$objEmailMessage.IsBodyHTML = $true
$objEmailMessage.Body = $mailbody

$objSMTP.EnableSsl = $true
$objSMTP.Credentials = New-Object System.Net.NetworkCredential($strFrom, $strPassword);
$objSMTP = New-Object Net.Mail.SmtpClient($strSMTPServer, $strPort)

Some idé whats wrong?

first guess would be your credential object isn’t valid.
or whatever creds you are passing is not allowed to send email through your SMTP server.

in addition, look at send-mailmessage, built in powershell cmdlet to perform this action. and way easier to utilize.

Absolutely I have rewritten the code for the following instead.
I know that the security in this example is not the best, but I will solve it later.

This works fine.

$cred = [pscredential]::new("username",(ConvertTo-SecureString -String "password" -AsPlainText -Force))

#Now it's time to send the result to right instance.
Send-MailMessage -From 'Alias mailaddress'
-To 'Alias mailaddress'
-Subject 'Text' -Encoding "UTF8"
-BodyAsHtml -Body $mailbody
-SmtpServer ''
-Port 587
-Credential $cred