Thursday, August 16, 2018

Powershell script to send mail


$a = ""

$table1 = Get-Childitem D:\MyFiles | where {$_.LastwriteTime -le (Get-date).AddDays(-7)}|where {$_.Name -like "myFile.xls" }| Select Name,LastWriteTime |ConvertTo-Html -head $a -Property Name,LastWriteTime


$FromEmail="sender@test.com"
$ToEmail="receiver@test.com"
$CcEmail="cc1@test.com;cc2@test.com;cc3@test.com"
$SMTPMail="mailserver.test.com"


 Function sendEmail([string]$emailFrom, [string]$emailTo,[string]$emailCc, [string]$subject,[string]$body,[string]$smtpServer)
{
$email = New-Object System.Net.Mail.MailMessage 
$email.From = $emailFrom
$email.To.Add($emailTo)
$email.Cc.Add($CcEmail)
$email.Subject = $subject
$email.IsBodyHTML = $true
$email.Body = $body
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($email)
}
$date=get-date
$message = "Hi Team,
"
$message += "
"
$message += "Test File file has not been uploaded today
"
$message += "
$table1
"
$message += "Thanks
Test Team
"


sendEmail -emailFrom $fromEmail -emailTo $ToEmail -emailCc $CcEmail -subject "Test file Status -$($date)" -body $message -smtpServer $SMTPMail

No comments:

Post a Comment