Powershell
Send html formatted mail
cls function SendSMTP{Param ([string]$To, [string]$From, [string]$Server, [string]$Subject, [string]$Body,[string]$Attachment) $mail = new-object System.Net.Mail.MailMessage #set the addresses $mail.From = new-object System.Net.Mail.MailAddress($From); $mail.To.Add($To); $mail.Sender = $To; $mail.ReplyTo = $To; #set the content $mail.Priority = 'High' $mail.IsBodyHtml = 'True' $mail.Subject = $Subject; $mail.Body = $Body; $mail.Attachments.Add($Attachment) #Set the SMTP server name $smtp = new-object System.Net.Mail.SmtpClient($Server); #to authenticate we set the username and password properites on the SmtpClient #$smtp.Credentials = new-object System.Net.NetworkCredential("domain\user", "password"); #send the message $smtp.Send($mail); } #Construct HTML formatting $head = @' <style> BODY{font-family:Verdana; background-color:lightblue;} TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;} TH{font-size:1.3em; border-width: 1px;padding: 2px;border-style: solid;border-color: black;background-color:#000000} TD{border-width: 1px;padding: 2px;border-style: solid;border-color: black;background-color:white} </style> '@ $header = "<h1>Services status</h1>" $title = "Running services" #Get body content (The status of all Windows services) $Body = Get-Service | Select-Object Name, DisplayName, Status #Convert it to HTML $Body = $Body | ConvertTo-Html -head $head -body $header -title $title | Out-String #Send the mail $SendTo = ' This e-mail address is being protected from spambots, you need JavaScript enabled to view it ' #Recipients address $MailFrom = ' This e-mail address is being protected from spambots, you need JavaScript enabled to view it ' #Senders address $MailServer = 'servername' #The SMTP server $MailSubject = 'A test mail' #Subject $MailBody = $body #What's in the mail $MailAttachment = '.\attachmentname.zip' #Optional attachment SendSMTP $SendTo $MailFrom $MailServer $MailSubject $MailBody $MailAttachment
Last Updated (Friday, 05 June 2009 12:49)