0
# This works, it works fine when not in a function

$emailFrom          = "account@gmail.com"
$emailTo            = "account@customer.com"
$emailSubject       = "Report" 
$emailBody          = "Daily Report Attached"
$SMTPAuthUsername   = "acocunt@gmail.com"
$SMTPAuthPasswrd    = "strongpass" 
$SMTPServer         = "smtp.gmail.com"
$SMTPPort           = "587"

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12     

$mailmessage = New-Object System.Net.Mail.MailMessage 
$mailmessage.from = ($emailFrom)
$mailmessage.To.add($emailTo)
$mailmessage.Subject = $emailSubject
$mailmessage.Body = $emailBody
    
$SMTPClient = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort)
$SMTPClient.UseDefaultCredentials = $false
$SMTPClient.EnableSSL = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential($SMTPAuthUsername, $SMTPAuthPasswrd)
$SMTPClient.Send($mailmessage)





# This does not work

$emailFrom          = "account@gmail.com"
$emailTo            = "account@customer.com"
$emailSubject       = "Report" 
$emailBody          = "Daily Report Attached"
$SMTPAuthUsername   = "acocunt@gmail.com"
$SMTPAuthPasswrd    = "strongpass" 
$SMTPServer         = "smtp.gmail.com"
$SMTPPort           = "587"
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12   

function Send-Email {
    param (
        [Parameter(Mandatory=$true)][string]$emailFrom,
        [Parameter(Mandatory=$true)][string]$emailTo,
        [Parameter(Mandatory=$true)][string]$emailSubject,
        [Parameter(Mandatory=$true)][string]$emailBody,
        [Parameter(Mandatory=$true)][string]$SMTPAuthUsername,
        [Parameter(Mandatory=$true)][string]$SMTPAuthPasswrd,
        [Parameter(Mandatory=$true)][string]$SMTPServer,
        [Parameter(Mandatory=$true)][string]$SMTPPort
    )

     
    $mailmessage = New-Object System.Net.Mail.MailMessage
    $mailmessage.from = ($emailFrom)
    $mailmessage.To.add($emailTo)
    $mailmessage.Subject = $emailSubject
    $mailmessage.Body = $emailBody
      
    $SMTPClient = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort)
    $SMTPClient.UseDefaultCredentials = $false
    $SMTPClient.EnableSSL = $true
    $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($SMTPAuthUsername, $SMTPAuthPasswrd)
    $SMTPClient.Send($mailmessage)

    
}  

Send-Email    -emailFrom $emailFrom `
-emailTo $emailTo `
-emailSubject $emailSubject `
-emailBody $emailBody `
-SMTPAuthUsername $SMTPAuthUsername `
-SMTPAuthPasswrd $SMTPServer `
-SMTPServer $SMTPServer `
-SMTPPort $SMTPPort 

When I run this using a function I get the following error:

Exception calling "Send" with "1" argument(s): "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Authentication Required. Learn more at". Line 86

Line 86 is $SMTPClient.Send($mailmessage). I was getting this message for the working one but fixed it with the TLS entry.

  • Posibly duplicate of this question https://stackoverflow.com/questions/20906077/gmail-error-the-smtp-server-requires-a-secure-connection-or-the-client-was-not – Nico Osorio Oct 12 '22 at 16:00
  • totally different, that was environment/server differences. I am running both the non working and working code on the same machine. Please read carefully. Also, I already did the 2fa and app password for gmail, its working 100%, it only does not work when inside a function. Same exact code inside and outside of the function. – JPPintoDotCom Oct 12 '22 at 16:02

1 Answers1

0

You are passing the wrong argument to the function.

The line

-SMTPAuthPasswrd $SMTPServer `

should be

-SMTPAuthPasswrd $SMTPAuthPasswrd `

As I pointed out in the comment the error is that you are passing wrong credential.

Refer to case 1 in https://stackoverflow.com/a/26709761/8473136

Nico Osorio
  • 101
  • 9