1. cls
  2.  
  3. function SendSMTP{Param ([string]$To, [string]$From, [string]$Server,
  4. [string]$Subject, [string]$Body,[string]$Attachment)
  5.  
  6. $mail = new-object System.Net.Mail.MailMessage
  7.  
  8. #set the addresses
  9. $mail.From = new-object System.Net.Mail.MailAddress($From);
  10. $mail.To.Add($To);
  11. $mail.Sender = $To;
  12. $mail.ReplyTo = $To;
  13.  
  14. #set the content
  15. $mail.Priority = 'High'
  16. $mail.IsBodyHtml = 'True'
  17. $mail.Subject = $Subject;
  18. $mail.Body = $Body;
  19. $mail.Attachments.Add($Attachment)
  20.  
  21. #Set the SMTP server name
  22. $smtp = new-object System.Net.Mail.SmtpClient($Server);
  23.  
  24. #to authenticate we set the username and password properites on the SmtpClient
  25. #$smtp.Credentials = new-object System.Net.NetworkCredential("domain\user", "password");
  26.  
  27. #send the message
  28. $smtp.Send($mail);
  29.  
  30. }
  31.  
  32. #Construct HTML formatting
  33. $head = @'
  34. <style>
  35. BODY{font-family:Verdana; background-color:lightblue;}
  36. TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
  37. TH{font-size:1.3em; border-width: 1px;padding: 2px;border-style: solid;border-color: black;background-color:#000000}
  38. TD{border-width: 1px;padding: 2px;border-style: solid;border-color: black;background-color:white}
  39. </style>
  40. '@
  41. $header = "<h1>Services status</h1>"
  42. $title = "Running services"
  43.  
  44. #Get body content (The status of all Windows services)
  45. $Body = Get-Service | Select-Object Name, DisplayName, Status
  46.  
  47. #Convert it to HTML
  48. $Body = $Body | ConvertTo-Html -head $head -body $header -title $title | Out-String
  49.  
  50. #Send the mail
  51. $SendTo = ' This e-mail address is being protected from spambots, you need JavaScript enabled to view it ' #Recipients address
  52. $MailFrom = ' This e-mail address is being protected from spambots, you need JavaScript enabled to view it ' #Senders address
  53. $MailServer = 'servername' #The SMTP server
  54. $MailSubject = 'A test mail' #Subject
  55. $MailBody = $body #What's in the mail
  56. $MailAttachment = '.\attachmentname.zip' #Optional attachment
  57.  
  58. SendSMTP $SendTo $MailFrom $MailServer $MailSubject $MailBody $MailAttachment

Last Updated (Friday, 05 June 2009 12:49)