user1832076
user1832076

Reputation: 57

Powershell Script - Send Authenticated email

So I have a report script which is working, and I am adding the below script to it so that it sends the email. It all works apart from it fails the authentication.

The password is stored in a file that works and the authentication part works for -Sendmail version but I can't get it to authenticate adding to the script.

Thanks for your help

I think that this is the part where I need to it to authenticate to "$cred" "IF ($SendEmail -eq $TRUE) {$smtp.Send($msg)}"

$msg = new-object Net.Mail.MailMessage
$msg.IsBodyHTML = $TRUE    
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminName, $Pass
$AdminName = "[email protected]"
$Pass = Get-Content "C:\Masters\WIN SERVER BACKUP SCRIPT\cred.txt" | ConvertTo-SecureString
$msg.From = $FromAddress
$msg.To.Add($ToAddress)
$msg.Subject = $ReportSubject
$msg.Body = $sb

# Format the date and time for the file.
$VarDateTime = (Get-Date -format "yyyy-MMMM-dd-dddd-HHmm")
# Note the extension for the $CleanOldReports
$OutPut = "$OutPutPath\$VarDateTime.html"
# The output folder - The parent folder of the $OutPut.
$TARGETDIR = (split-path $OutPut -Parent)
# If the output folder does not exist then create it.
IF (!(Test-Path -Path $TARGETDIR )) {New-Item -ItemType directory -Path $TARGETDIR}
#Output the report to a file.
"$sb" | Out-File "$OutPut"
# If $TRUE then open the backup report in your default web browser.
IF ($ShowReport -eq $TRUE) {"$OutPut" | invoke-expression}
# If $TRUE then email the report to backup operators.
IF ($SendEmail -eq $TRUE) {$smtp.Send($msg)}
Write-Host "Report Successfully Generated." -ForegroundColor green

Upvotes: 1

Views: 311

Answers (1)

user1832076
user1832076

Reputation: 57

So I have added Send-MailMessage rather than pulling from the top and its working.

Upvotes: 1

Related Questions