Senden von Formatierten HTML-E-Mails

Ich habe eine text-Datei output.txt, die hat folgenden Inhalt:

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

Ich möchte zum senden von Inhalt von output.txt in einer E-Mail als ein Körper in Formatierte HTML-Tabelle look-in Windows server 2008 R2. Ich versuche mit folgenden code, aber es funktioniert nicht für mich..wo bin ich irrtümlich unten ?

$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

Wenn ich Sie modifiziert habe meinen code wie folgt:

$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)

Bekomme ich post wie diese:

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 

Hinweis: ich bin mit Powershell v1.0

1. "Funktioniert nicht" in welcher Weise? Je spezifischer Sie sind, desto wahrscheinlicher wird in der Lage sein, das problem zu identifizieren. 2. Was sind Ihre Absichten für $style? Sie setzen Sie dessen Wert auf eine Reihe von CSS-code, aber dann bist du nicht etwas zu tun mit es. Es scheint, dass es sollte so etwas wie $message.Body = '<head>' + $style + '</head>' + $message.Body zwischen der letzten $style = [...] - Anweisung und die $smtp = [...] - Anweisung. 3. output.txt nicht HTML enthalten. Es sollte die HTML-Tabelle Daten, da sonst die Formatierung werden alle falsch. Ist es das, was "funktioniert nicht"?

InformationsquelleAutor Sunny | 2013-10-29

Schreibe einen Kommentar