none
Powershell Send email script RRS feed

  • Question

  • Hello All,

    I am implementing a solution to send an email automatically to alert the users about some activities in AD. The AD script is working fine but when it try to send the mail I'm getting an error.Can someone please let me know what is wrong in my script. I had tried using port 587 and 25 with and without -UseSSL parameter without any luck.

    This is my script, first I'm encrypting password as below

    Read-Host -Prompt "Password" -AsSecureString | ConvertFrom-SecureString | Out-File "C:\password.txt"

    Then using the below script to send the email

    $AdminName = "From email"
    $Pass = Get-Content "C:\password.txt" | ConvertTo-SecureString
    $Cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminName, $Pass
    Send-MailMessage –From $AdminName –To "To email" –Subject “Test Auto” –Body “Automation Test using powershell” -SmtpServer smtp.office365.com -Credential $Cred -UseSsl -Port 587

    Here is the error

    Send-MailMessage : The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 SMTP; Client was not
    authenticated to send anonymous mail during MAIL FROM [server.prod.outlook.com]

    Thanks,

    Wednesday, May 20, 2020 3:26 AM

All replies

  • It appears that your SMTP server does not allow mail to be sent or that it does not support the SSL that you are using.

    To eliminate the ambiguity test without saving the password.

    $O365Email = '<your O365 email address>'
    $paramSendMailMessage = @{
        From = $O365Email
        To = 'To email'
        Subject = 'Test Auto'
        Body = 'Automation Test using powershell'
        SmtpServer = 'smtp.office365.com'
        Credential = Get-Credential $O365Email
        UseSsl = $true
        Port = 587
    }
    Send-MailMessage @paramSendMailMessag


    \_(ツ)_/

    Wednesday, May 20, 2020 4:44 AM
  • If you don't mind using VBS but works fine and does the task, check out this link.

    Good luck!


    Every second counts..make use of it. Disclaimer: This posting is provided AS IS with no warranties or guarantees and confers no rights.

    Wednesday, May 20, 2020 9:17 AM
  • If you don't mind using VBS but works fine and does the task, check out this link.

    Good luck!


    Every second counts..make use of it. Disclaimer: This posting is provided AS IS with no warranties or guarantees and confers no rights.

    Note that VBScript is obsolete and use for any new work should be avoided.   It is also much less flexible.

    In nearly all cases VBScript is less useful and not needed.


    \_(ツ)_/

    Wednesday, May 20, 2020 10:54 AM