Send email to multiple recipients using PowerShell

Function readEmailAddressList($emailListFilename)
{
  $emailFile = Get-Content $emailListFilename
  $lineCount = [int](Get-Content $emailListFilename | Measure-Object).Count
  $counter = [int]1
  [string[]]$emailAddresses = $null
  foreach ($line in $emailFile)
  {
    if ($counter -lt $lineCount)
    {
       $emailAddresses += $line + ","
       $counter++
    }
    else
    {
       $emailAddresses += $line
    }
  }
  Write-Output -InputObject $emailAddresses
}

# Create from/to addresses
$from = "fromAdd@test.com"
$to = "aaaa@test.com","aaabb@test.com"#readEmailAddressList("H:\jt\it\emailAddressList.txt")

# Set SMTP Server and create SMTP Client
$server = "youre smtp server"

# Send the message
Send-MailMessage -To $To -Subject "Tape Backup" -From $from -Body "This is a test email" -SmtpServer $server
#Send-MailMessage -To $To -Body $Body -Subject $Subject -From $From -SmtpServer $SMTPServer -Credential $Credential -ErrorAction Stop

                                          

Comments

Popular posts from this blog

SharePoint 2013 REST API CRUD Operations

Clearing the Configuration Cache for SharePoint

SharePoint 2010: List and Delete List Item Versions using PowerShell