Sending mail through PowerShell

Automated email notification is always worth during software automation process. Now a days continuous integration tools come with this feature. If you want to automate a small task with email notification then PowerShell is the best option.

PowerShell is a command-line tool from Microsoft. Using PowerShell we can automate the administrative tasks of windows. You can launch PowerShell from Windows program menu.

Here explains the code snippet for sending email
send-Mail #Function call

#function definition
function send-Mail {
###########Define Variables######## 
$fromaddress = "From address" 
$toaddress = "To Address" 
$Subject = "Subject" 
$attachment = "Attachment Path" 
$smtpserver = "SMTP Server Name" 
#################################### 
 
$message = new-object System.Net.Mail.MailMessage 
$message.From = $fromaddress 
$message.To.Add($toaddress) 
$message.IsBodyHtml = $True 
$message.Subject = $Subject 
$attach = new-object Net.Mail.Attachment($attachment) 
$message.Attachments.Add($attach) 
$smtp = new-object Net.Mail.SmtpClient($smtpserver) 
$smtp.Send($message) 
}
Modify variables based on your requirement and save it as PowerShell script. Call this script in your test automation suite to get the mail.
Previous Post Next Post