- This topic has 2 replies, 2 voices, and was last updated 3 weeks, 4 days ago by
October 1, 2020 at 10:35 am #260253ParticipantTopics: 5Replies: 12Points: 70Rank: Member
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 system.net.mail.mailmessage
$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?
October 1, 2020 at 12:10 pm #260310ParticipantTopics: 25Replies: 197Points: 718Rank: Major Contributor
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.
October 1, 2020 at 3:22 pm #260373ParticipantTopics: 5Replies: 12Points: 70Rank: Member
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.PowerShell1234567891011$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 'smtp.smtp.org'-Port 587-UseSsl-Credential $cred
- This reply was modified 3 weeks, 3 days ago by grokkit. Reason: code formatting - please read the guide
- You must be logged in to reply to this topic.