Home > database >  Works outside of a function but not inside of a function
Works outside of a function but not inside of a function

Time:10-13

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

$emailFrom          = "[email protected]"
$emailTo            = "[email protected]"
$emailSubject       = "Report" 
$emailBody          = "Daily Report Attached"
$SMTPAuthUsername   = "[email protected]"
$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          = "[email protected]"
$emailTo            = "[email protected]"
$emailSubject       = "Report" 
$emailBody          = "Daily Report Attached"
$SMTPAuthUsername   = "[email protected]"
$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.

CodePudding user response:

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

  • Related