Functions/Outlook/Send-AdvMailMessage.ps1
Function Send-AdvMailMessage { [CmdletBinding()] [OutputType([System.Object])] Param ( [Parameter(Mandatory=$false,ValueFromPipeLine=$true)] [Alias('PsPath')] [string[]] $Attachments, [Parameter(Mandatory=$false)] [string[]] $Bcc, [Parameter(Mandatory=$false,Position=2)] [string] $Body, [Parameter(Mandatory=$false)] [Alias('BAH')] $BodyAsHtml, [Parameter(Mandatory=$false)] [string[]] $Cc, [Parameter(Mandatory=$false)] [pscredential] $Credential, [Parameter(Mandatory=$false)] [ValidateSet("None","OnSuccess","OnFailure","Delay","Never")] [Alias('DNO')] [System.Net.Mail.DeliveryNotificationOptions] $DeliveryNotificationOption = "None", [Parameter(Mandatory=$false)] [Alias('BE')] [System.Text.Encoding] $Encoding, [Parameter(Mandatory=$true)] [string] $From, [Parameter(Mandatory=$false)] [string] $ReplyTo, [Parameter(Mandatory=$false)] [int] $Port, [Parameter(Mandatory=$false)] [ValidateSet("Normal","Low","High")] [System.Net.Mail.MailPriority] $Priority = "Normal", [Parameter(Mandatory=$false,Position=3)] [Alias('ComputerName')] [string] $SmtpServer, [Parameter(Mandatory=$true,Position=1)] [Alias('sub')] [string] $Subject, [Parameter(Mandatory=$false)] $UseSsl, [Parameter(Mandatory=$true,Position=0)] [string[]] $To ) Begin { # Redefine Variables for Unspecified Parameters $MailServer = if ($SmtpServer){$SmtpServer}else{$env:COMPUTERNAME} $EnableSsl = if($UseSsl){$true}else{$false} $MailBodyAsHtml = if($BodyAsHtml){$true}else{$false} $MailPort = if ($Port){$Port}else{if($EnableSsl){"465"}else{"25"}} $MailCredentials = if ($Credential){New-Object System.Net.NetworkCredential($Credential.username,$Credential.password);} $MailAttachments = foreach ($Attachment in $Attachments){New-Object System.Net.Mail.Attachment($Attachment)} $MailReply = if($ReplyTo){$ReplyTo}else{$From} } Process { # Construct Email Object $email = New-Object System.Net.Mail.MailMessage # Configure Options $email.IsBodyHtml = $MailBodyAsHtml if($Encoding){$email.BodyEncoding = $Encoding} $email.DeliveryNotificationOptions = $DeliveryNotificationOption $email.Priority = $Priority # Configure Senders and Recipients $email.From = $From $email.ReplyTo = $MailReply foreach ($recipient in $To){$email.to.add($recipient)} foreach ($recipient in $Cc){$email.cc.add($recipient)} foreach ($recipient in $bcc){$email.Bcc.add($recipient)} # Configure Payload $email.Subject = $Subject $email.Body = $Body foreach ($Attachment in $MailAttachments){$email.Attachments.Add($Attachment)} # Construct SMTP Client $SMTPClient = New-Object System.Net.Mail.SmtpClient($MailServer,$MailPort) $SMTPClient.EnableSsl = $EnableSsl if ($MailCredentials){$SMTPClient.Credentials = $MailCredentials} # Send Message Through SMTP Client $SMTPClient.Send($email) } } |