I have a text file output.txt which has following content:
OPERATINGSYSTEM PROJECTSERVER1 PROJECTSERVER2
Windows 1.36 4.42
Linux12 2.78 5.76
MacOS 3.45 6.39
Ubuntu 4.12 0.00
Android 0.00 3.46
FreePhysicalMemory 30.12 31.65
TotalVisibleMemorySize 48.00 48.00
CPULoadPercentage 2 4
I want to send content of output.txt in a email as a body in Formatted HTML table look in Windows server 2008 R2. I am trying with below code, but its not working for me..where am I mistaking below ?
$smtpServer = "[email protected]"
$smtpFrom = "[email protected]"
$smtpTo = "[email protected]"
$messageSubject = "Servers Memory"
$message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
$message.Subject = $messageSubject
$message.IsBodyHTML = $true
$message.Body = Get-Content C:\Logs\output.txt
$style = "< style>BODY{font-family: Arial; font-size: 10pt;}"
$style = $style + "TABLE{border: 1px solid black; border-collapse: collapse;}"
$style = $style + "TH{border: 1px solid black; background: #dddddd; padding: 5px; }"
$style = $style + "TD{border: 1px solid black; padding: 5px; }"
$style = $style + "< /style>"
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($message)
EDIT1
When i modified my code as below:
$smtpServer = "[email protected]"
$smtpFrom = "[email protected]"
$smtpTo = "[email protected]"
$messageSubject = "Servers Memory"
$message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
$message.Subject = $messageSubject
$message.IsBodyHTML = $true
$style = "<style>BODY{font-family: Arial; font-size: 10pt;}"
$style = $style + "TABLE{border: 1px solid black; border-collapse: collapse;}"
$style = $style + "TH{border: 1px solid black; background: #dddddd; padding: 5px; }"
$style = $style + "TD{border: 1px solid black; padding: 5px; }"
$style = $style + "</style>"
$message.Body = "<head><pre>$style</pre></head>"
$message.Body += Get-Content C:\Logs\output.txt
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($message)
I am getting mail like this:
OPERATING SYSTEM SERVER1 SERVER2 Windows 1.36 4.42 Linux 2.78 5.76 MacOS 3.45 6.39 Android 0.00 3.46 Ubuntu 4.12 0.00 FreePhysicalMemory 30.12 31.65 TotalVisibleMemorySize 48.00 48.00
Note: I am using Powershell v1.0
$message.Body = '<head>' + $style + '</head>' + $message.Bodybetween the last$style = [...]statement and the$smtp = [...]statement. 3. output.txt doesn't contain HTML. It should have HTML table data, otherwise the formatting will be all wrong. Is that what's "not working"?