Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- ___ ___ _____ ___
- | _ \ _ \_ _/ __|
- | _/ / | || (_ |
- |_| |_|_\ |_| \___|
- PowerShellMailer
- Parameters have to look like this:
- .\mail.ps1 -From "from@mail.com" -To @("to@mail.com") -Subject "My Subject" -Body "Hello World" -SmtpHost "my.mailserver.com"
- #>
- param(
- ## The recipient of the mail message
- [string[]] $To = $(throw "Please specify the destination mail address"),
- ## The subjecty of the message
- [string] $Subject = "<No Subject>",
- ## The body of the message
- [string] $Body = $(throw "Please specify the message content"),
- ## The SMTP host that will transmit the message
- [string] $SmtpHost = $(throw "Please specify a mail server."),
- ## The sender of the message
- [string] $From = "$($env:UserName)@example.com"
- )
- ## Create the mail message
- $email = New-Object System.Net.Mail.MailMessage
- ## Populate its fields
- foreach($mailTo in $to)
- {
- $email.To.Add($mailTo)
- }
- $email.From = $from
- $email.Subject = $subject
- $email.Body = $body
- $email.Headers.Add("X-Company", "My Company");
- $email.IsBodyHtml = $true;
- ## Send the mail
- $client = New-Object System.Net.Mail.SmtpClient $smtpHost
- $client.Credentials = new System.Net.NetworkCredential("your-mail-username", "your-mail-password");
- #$client.UseDefaultCredentials = $true
- $client.Send($email)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement