Advertisement
stephanlinke

[PRTG] Send Mail

Dec 8th, 2015
485
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2.  ___ ___ _____ ___
  3. | _ \ _ \_   _/ __|
  4. |  _/   / | || (_ |
  5. |_| |_|_\ |_| \___|
  6.    PowerShellMailer
  7.  
  8. Parameters have to look like this:
  9. .\mail.ps1 -From "from@mail.com" -To @("to@mail.com") -Subject "My Subject" -Body "Hello World" -SmtpHost "my.mailserver.com"
  10. #>
  11.  
  12. param(
  13.     ## The recipient of the mail message
  14.     [string[]] $To = $(throw "Please specify the destination mail address"),
  15.  
  16.     ## The subjecty of the message
  17.     [string] $Subject = "<No Subject>",
  18.  
  19.     ## The body of the message
  20.     [string] $Body = $(throw "Please specify the message content"),
  21.  
  22.     ## The SMTP host that will transmit the message
  23.     [string] $SmtpHost = $(throw "Please specify a mail server."),
  24.  
  25.     ## The sender of the message
  26.     [string] $From = "$($env:UserName)@example.com"
  27. )
  28.  
  29. ## Create the mail message
  30. $email = New-Object System.Net.Mail.MailMessage
  31.  
  32. ## Populate its fields
  33. foreach($mailTo in $to)
  34. {
  35.     $email.To.Add($mailTo)
  36. }
  37.  
  38. $email.From = $from
  39. $email.Subject = $subject
  40. $email.Body = $body
  41. $email.Headers.Add("X-Company", "My Company");
  42. $email.IsBodyHtml = $true;
  43.  
  44. ## Send the mail
  45. $client = New-Object System.Net.Mail.SmtpClient $smtpHost
  46. $client.Credentials = new System.Net.NetworkCredential("your-mail-username", "your-mail-password");
  47. #$client.UseDefaultCredentials = $true
  48.  
  49. $client.Send($email)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement