fatherazrael fatherazrael - 3 years ago 695
Powershell Question

Powershell EMail: How to add new line in email Body?

Very new to Powershell. Wrote powershell script which is working perfectly but when it sends mail it does not show new line character in body. Following are configurations:

$EmailFrom = "monitoring@mydomainname.no"
$EmailTo = "fatherazrael@tcs.com"
$Subject = "Disk Space Low: $server"

$Body = "Server Name: $server, <NEED NEW LINE> Drive: C, <NEED NEW LINE> Total Size: $sizeGB, <NEED NEW LINE> Space Left: $freeSpaceGB"

$SMTPServer = "scan.opinergo.fn"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25)
#$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("<From mail ID>", "Password");
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)

In Body i have used following in place of ; searched around:

1) %0d%0a - NOT WORKING when user checks mail

2) \n - NOT WORKING when user checks mail

3) < br > - Not Working when user check mail

Could anyone please suggest what else could i use to get new line or any modifications required in script?

Answer Source

(From my comment)

The PowerShell newline indicator is `n (backtick-n), so:

$Body = "Server Name:  $server, <NEED NEW LINE> Drive: C


$Body = "Server Name:  $server, `n Drive: C
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download