PowerShell 5.1 marks the error described below the code I can't find a solution

Welcome Forums General PowerShell Q&A PowerShell 5.1 marks the error described below the code I can't find a solution

This topic contains 3 replies, has 4 voices, and was last updated by

 
Participant
1 week, 3 days ago.

  • Author
    Posts
  • #144378

    Participant
    Points: -7
    Rank: Member

    #setting stmp

    $SMTPServer = "*******.*****.***"$SMTPServerPort = "587"

    $Username = "*****@******.***"

    $Password = "******"

    $smtp.EnableSsl = $true

    #corpo mail

    $to = "*******@*****.***"

    $subject = "Results for AD Report LAST LOGON"

    $body = "Script version 1.0 by Maarten Mol"

    $attachment = "C:\prova.txt"

    $message = New-Object System.Net.Mail.MailMessage

    $message.subject = $subject

    $message.body = $body

    $message.to.add($to)

    $message.from = $username

    $message.attachments.add($attachment)

    $smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);

    $smtp.EnableSSL = $true

    $smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);

    $smtp.send($message)

     

    —ERROR—

    Exception during the "Send" argument with argument "4": "The SMTP server requires a secure connection or the client was not authenticated. Server response: 5.7.1 The client was not authenticated"In C:\Users\Roberto.Bugatti\Desktop\invioemail\mailsender.ps1:27 car:1 + $smtp.send($to,$Username,$body,$attachment)

    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : SmtpException
  • #144710

    Participant
    Points: 181
    Helping Hand
    Rank: Participant

    It would appear that you're not successfully authenticating with your smtp server, most likely requires SSL or something similar.

    Also, why use the .net providers here, when Powershell has a native cmdlet send-mailmessage.

    https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/send-mailmessage?view=powershell-6

  • #144723

    Participant
    Points: 638
    Helping Hand
    Rank: Major Contributor

    I see port 587, I think Office 365, but regardless here is an example of using Send-MailMessage:

    https://blogs.technet.microsoft.com/ccarroll/2017/11/28/use-powershell-to-send-email-from-o365-account/

  • #144758

    Participant
    Points: 140
    Helping Hand
    Rank: Participant

    Not the best example but it works

     

    $login = "Login name" $password = "Password" | Convertto-SecureString -AsPlainText -Force $credentials = New-Object System.Management.Automation.Pscredential -Argumentlist $login,$password
    Send-MailMessage -SmtpServer smtp.office365.com -from "From@email.nl" -credential $credentials -to "To@email.nl" -subject "Subject" -Body "Body" -UseSsl

You must be logged in to reply to this topic.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort