How to fix 'the SMTP server requires a secure connection or the client was not authenticated' error when the server response was 5.7.0?

13,372

Have you tried to change the port? It looks to me that you're still using 587 which is expecting TLS/SSL or STARTTLS.

Is there a reason you're not authenticating and using SSL/TLS ?

I've read that Google does not fall back to plaintext SMTP if STARTTLS is not supported. If you've explicitly selected the "Unsecured" option when configuring the server I believe you need to use a different port. 25(maybe).

Edit: It looks like you're missing arguments on the send method. Try this code. Alternatively try changing for the much easier looking Send-MailMessage cmdlet https://docs.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Utility/Send-MailMessage?view=powershell-5.1

$EmailFrom = "[email protected]"
$EmailTo = "[email protected]" 
$Subject = "Notification from XYZ" 
$Body = "this is a notification from XYZ Notifications.." 
$SMTPServer = "smtp.gmail.com" 
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587) 
$SMTPClient.EnableSsl = $true 
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("username", "password"); 
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
Share:
13,372
Pratama
Author by

Pratama

Updated on June 04, 2022

Comments

  • Pratama
    Pratama about 2 years

    I did turn on my less secure app in Gmail, I think the problem is SSL.

    My powershell screenshot

    My current problem after changing port and $smtp

    $time = get-date 
    $smtpServer = “smtp.gmail.com”
    $smtp.EnableSSL = $true
    $smtp.Credentials = New-Object System.Net.NetworkCredential(“user”, “password”)
    $smtp = new-object Net.Mail.SmtpClient($smtpServer,587) **587 to 465**
    $msg = new-object Net.Mail.MailMessage
    $msg.From = (“[email protected]”)
    $msg.To.Add(“[email protected]”)
    $body=”Your message Body”
    $msg.Subject = “Mail From Gmail ” + $time
    #your file location
    $files=Get-ChildItem “C:\Users\Public\Pictures\LINE”
    Foreach($file in $files)
    {
    Write-Host “Attaching File :- ” $file
    $attachment = New-Object System.Net.Mail.Attachment –ArgumentList 
    C:\Users\Public\Pictures\LINE\$file
    $msg.Attachments.Add($attachment)
    }
    $smtp.Send($msg)
    $attachment.Dispose()
    $msg.Dispose()
    
    • AdminOfThings
      AdminOfThings about 5 years
      The assignment of $smtp is problematic here. You are setting properties on $smtp and then declaring a new Net.Mail.SmtpClient instance with the same name AFTER you set the properties. This will set those properties to the default values. At a minimum, $smtp = new-object Net.Mail.SmtpClient($smtpServer,587) should come before changing any properties of $smtp.
    • Jason Aller
      Jason Aller about 5 years
      Welcome to Stack Overflow. Images of text are not searchable, please edit the question to replace them with the text they contain and format it appropriately. If the text of the error is in the question it makes it easier to find, and also makes it accessible.
    • Pratama
      Pratama about 5 years
      Bro please see my current problem on my edited question.. @AdminOfThings