Categories
Windows

Automate and schedule Task Scheduler sending email and attachment using Powershell

1. Create a file with the encrypted server password:

In Powershell, enter the following command (replace myPassword with your actual password):

"myPassword" | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Out-File "C:\EmailPassword.txt"

2. Create a powershell script

(Ex. sendEmail.ps1):

$User ="[email protected]"

$File = "C:\EmailPassword.txt"

$cred=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, (Get-Content $File | ConvertTo-SecureString)

$EmailTo = "[email protected]"

$EmailFrom = "[email protected]"

$Subject = "Email Subject"

$Body = "Email body text"

$SMTPServer ="smtp.gmail.com"

$filenameAndPath = "C:\fileIwantToSend.csv"

$SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)

$attachment = New-Object System.Net.Mail.Attachment($filenameAndPath)

$SMTPMessage.Attachments.Add($attachment)

$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)

$SMTPClient.EnableSsl = $true

$SMTPClient.Credentials = New-Object System.Net.NetworkCredential($cred.UserName, $cred.Password);

$SMTPClient.Send($SMTPMessage)

3. Automate with Task Scheduler

Create a batch file (Ex. emailFile.bat) with the following:

powershell -ExecutionPolicy ByPass -File C:\sendEmail.ps1

Create a task to run the batch file. Note: you must have the task run with the same user account that you used to encrypted the password!