fatherazrael fatherazrael - 4 months ago 48
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

(From my comment)

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

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

becomes:

$Body = "Server Name:  $server, `n Drive: C