#Connection Object $cn = New-Object System.Data.SqlClient.SqlConnection( "Password=********;Persist Security Info=True;User ID=sa;Initial Catalog=AdventureWorks;Data Source=DEEVUS-PC\SQLEXPRESS" ) $q = "SELECT TOP 50 * FROM HumanResources.vEmployee ORDER BY LastName" #Data Adapter which will gather the data using our query $da = New-Object System.Data.SqlClient.SqlDataAdapter($q, $cn) #DataSet which will hold the data we have gathered $ds = New-Object System.Data.DataSet #Out-Null is used so the number of affected rows isn't printed $da.Fill($ds) | Out-Null #Close the database connection $cn.Close() #HTML Email Styles $style = "" $emailbody = $ds.Tables[0] | Select-Object LastName, FirstName, JobTitle, Phone, EmailAddress, AddressLine1, City, PostalCode | ConvertTo-HTML -head $style #| Out-File test.html $emailFrom = "deevus@isp.com.au" $emailTo = "someone@anotherisp.com" $subject = "AdventureWorks - Current Employees" $message = New-Object Net.Mail.MailMessage($emailFrom, $emailTo, $subject, $emailbody) #This is needed to make sure it interprets the email as HTML $message.IsBodyHTML = $true $smtpServer = "smtp.isp.com.au" $smtp = New-Object Net.Mail.SmtpClient($smtpServer) $smtp.Send($message)