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

2 months, 1 week ago.

  • Author
  • #144378

    Topics: 1
    Replies: 0
    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.from = $username


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

    $smtp.EnableSSL = $true

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




    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

    Topics: 23
    Replies: 139
    Points: 262
    Helping Hand
    Rank: Contributor

    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.


  • #144723

    Topics: 8
    Replies: 1190
    Points: 639
    Helping Hand
    Rank: Major Contributor

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


  • #144758

    Topics: 3
    Replies: 42
    Points: 277
    Helping Hand
    Rank: Contributor

    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