email function

A PowerShell function for sending email:

Function SS64Mail($strSubject, $strBody, $strSenderemail, $strRecipientemail, $AttachFile)
   {
      $strSMTP = "ExchangeServer01"  #Change this to your SMTP/Exchange server
     
      $MailMessage = New-Object System.Net.Mail.MailMessage
      $SMTPClient = New-Object System.Net.Mail.smtpClient
      $SMTPClient.host = $strSMTP
      $Recipient = New-Object System.Net.Mail.MailAddress($strRecipientemail, "Recipient")
      $Sender = New-Object System.Net.Mail.MailAddress($strSenderemail, "Sender")
     
      $MailMessage.Sender = $Sender
      $MailMessage.From = $Sender
      $MailMessage.Subject = $strSubject
      $MailMessage.To.add($Recipient)
      $MailMessage.Body = $strBody
      if ($AttachFile -ne $null) {$MailMessage.attachments.add($AttachFile) }
      $SMTPClient.Send($MailMessage)
   }

Example:

$dtmToday = ((Get-Date).dateTime).tostring()
$strSubjectLine = "Daily Report - $dtmToday"    
$strBodyText = "report attached."
$strSender = "your.name@example.com"
$strRecipient = "their.name@example.com"
$Attachment = "C:\scripts\results.doc"
# Call the function to send the email
SS64Mail $strSubjectLine $strBodyText $strSender $strRecipient $Attachment

“In most companies, you don't get too much mail where people are saying, 'Hey, we lost this account.' But that's what you really need to know about, because it might change what you're doing. You need to know about competitive activity. You need to know about customer feedback that says things should be better” ~ Bill Gates

Related:

PowerShell Functions


© Copyright SS64.com 1999-2014
Some rights reserved