• Home
  • Help
  • Register
  • Login
  • Home
  • Members
  • Help
  • Search

 
  • 0 Vote(s) - 0 Average

Send email alerts with this PowerShell code

#1
03-20-2024, 08:37 AM
Use email alert script below in a file, named sendemail.ps1.

If you want to run it from a regular command prompt or batch file, run it as:
powershell.exe -file C:\myscripts\sendemail.ps1

Code:
$EmailFrom = "from@email.com"
$EmailTo = "to@email.com"
$Subject = "Alert: enter a useful email subject here. Local time is: $(Get-Date)"
$Body = "enter something useful here. time is:  $(Get-Date)"
$SMTPServer = "my.smtp.server.address.com"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, <my smtp port number example 587>)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("my@email.com","<my password>");
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)

The command to run the email alert can be configured in the Windows Task Scheduler or Event Viewer, to run triggered by various events in the system.
Hope this helps!
savas@BackupChain
Offline
Joined: Jun 2018
« Next Oldest | Next Newest »

Users browsing this thread: 1 Guest(s)



Messages In This Thread
Send email alerts with this PowerShell code - by savas@BackupChain - 03-20-2024, 08:37 AM

  • Subscribe to this thread
Forum Jump:

Backup Education Windows Server Utilities Send email alerts with this PowerShell code

© by FastNeuron Inc.

Linear Mode
Threaded Mode