Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
powershell邮件正文中的单行中断不工作_Powershell - Fatal编程技术网

powershell邮件正文中的单行中断不工作

powershell邮件正文中的单行中断不工作,powershell,Powershell,出于某种原因,当尝试在powershell中填充电子邮件正文时,使用`n转义序列的单行中断不起作用,而双行中断则起作用 这是我的密码 param([string]$BuildId, [string]$BuildName) $EmailTo = "[email]" $EmailFrom = "sepaautomation@gmail.com" $Subject = "$BuildName $BuildId SpecFlow Reports" $Body = "The following Spe

出于某种原因,当尝试在powershell中填充电子邮件正文时,使用
`n
转义序列的单行中断不起作用,而双行中断则起作用

这是我的密码

param([string]$BuildId, [string]$BuildName)

$EmailTo = "[email]"
$EmailFrom = "sepaautomation@gmail.com"
$Subject = "$BuildName $BuildId SpecFlow Reports" 
$Body = "The following SpecFlow reports are attached: `n`n" 
$SMTPServer = "smtp.sendgrid.net"

$date = Get-Date -Format yyyy-MM-dd

$files = Get-ChildItem "C:\Build\SA.SEPA.Web.UI.Selenium" -Recurse | Where-Object { $_.Name -match "^.*$date.*\.html$" } | % { $_.FullName }

foreach($item in $files)
{
    $Body += "$item `n"
}

$SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)

foreach($item in $files)
{
    $attachment = New-Object System.Net.Mail.Attachment($item)
    $SMTPMessage.Attachments.Add($attachment)
}

$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587) 
$SMTPClient.EnableSsl = $true 
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("apikey", "[apiKey]"); 
$SMTPClient.Send($SMTPMessage)
这是无法添加换行符的部分

foreach($item in $files)
{
    $Body += "$item `n"
}
我在outlook中的电子邮件如下所示

您可以看到已应用了
`n`n
双线分隔符,但未应用
`n
。顺便说一句,如果我改成

foreach($item in $files)
{
    $Body += "$item `n`n"
}  
我确实看到了双线中断


如何只获得一个换行符?

只需使用[Environment]::换行符即可解决此问题。它将消除与这个问题有关的大多数头痛问题

用法:

foreach($item in $files)
{
    $Body += "$item " + [Environment]::NewLine
}

如果确定使用HTML正文,则可以使用
换行符。 您需要在消息变量上设置以下属性:

$SMTPMessage.IsBodyHTML = $true

$Body += "$item <br>"
$SMTPMessage.IsBodyHTML=$true
$Body+=“$item

通常在Windows中,换行符由回车符和换行符分隔:
'r'n
。你试过了吗?