# 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.
 
    