Powershell Script to Send out an Email

PowerShell script to send out an email, 
  1. $LogTime = Get-Date -Format yyyy-MM-dd_hh-mm  
  2. $LogFile = ".\SendEmailPatch-$LogTime.rtf"  
  3.  
  4. # Add SharePoint PowerShell Snapin  
  5.   
  6.   
  7. if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null ) {  
  8.     Add-PSSnapin Microsoft.SharePoint.Powershell  
  9. }  
  10.   
  11. $scriptBase = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent  
  12. Set-Location $scriptBase  
  13.  
  14.  
  15. #Deleting any .rtf files in the scriptbase location  
  16. $FindRTFFile = Get-ChildItem $scriptBase\*.* -include *.rtf  
  17. if($FindRTFFile)  
  18. {  
  19.  foreach($file in $FindRTFFile)  
  20.   {  
  21.    remove-item $file  
  22.   }  
  23. }  
  24.   
  25.   
  26. start-transcript $logfile  
  27.   
  28. function sendMail{  
  29.   
  30. Write-Host "Sending Email"  
  31. $smtpServer = "SMTP Server Address"  
  32. $message = new-object Net.Mail.MailMessage  
  33. $smtp = new-object Net.Mail.SmtpClient($smtpServer)  
  34. $message.From = "Email from address"  
  35. $message.ReplyTo = "Email reply to address"  
  36. $message.To.Add("To address")  
  37. $message.subject = "Subject for the email"  
  38. $message.body = "Body for the email"  
  39. $smtp.Send($message)  
  40.     
  41. }  
  42.  
  43. ####Calling function####  
  44. sendmail  
  45. ####################