来自文件的正文

时间:2017-05-23 11:49:13

标签: powershell outlook

如果文本包含多个句子,则会向电子邮件添加文本,但不会正确添加任何句子。

内容@为空。

它只添加签名。

输入文件(名称:Text_file.txt):

email:m.kowalczyk@email.test.com
emialcc:
emailbcc:
Subject:2513000013
attachment:Y:\attachment\2513000013.pdf

文字档案:

This is a test
This and other lines do not show up
test3
test4
test5

PowerShell脚本:

$destDir = "C:\M3_Outlook"
if (!(Test-Path $destDir)) {
    New-Item -Path $destDir -ItemType Directory
} else {}

$file_patch = Get-ChildItem 'C:\M3_Outlook' | Sort {$_.LastWriteTime} |
              select -Last 1 | % { $_.FullName }
$name = Select-String -Path $file_patch -pattern name
$email = Select-String -Path $file_patch -pattern email
$subject=Select-String -Path $file_patch -pattern subject
$attachment = Select-String -Path $file_patch -pattern attachment
$Signature = Get-Content ($env:USERPROFILE + "\AppData\Roaming\Microsoft\Signatures\*.htm")
$rname = $name -replace ".*:"
$remail = $email -replace ".*:"
$rsubject = $subject -replace ".*:"
$rattachment = $attachment -replace ".*attachment:"
$rattachment
$sname = $rname -split ";"
$semail = $remail -split ";"
$ssubject = $rsubject -split ";"
$sattachment = $rattachment -split ";"
$body = Get-Content C:\M3_Outlook\PPS200.txt
$Signature = Get-Content ($env:USERPROFILE + "\AppData\Roaming\Microsoft\Signatures\*.htm")
$sRecipientAddr = $semail
$sMsgSubject = $ssubject
$oOutlook = New-Object -ComObject Outlook.Application 
$oMapiNs = $oOutlook.GetNameSpace("MAPI")
$oMailMsg = $oOutlook.CreateItem(0)
$oMailMsg.GetInspector.Activate()
$sSignature = $oMailMsg.HTMLBody
$sSignature = $oMailMsg.HTMLBody
$sattachment | ForEach-Object { $oMailMsg.Attachments.Add($_) }
$oMailMsg.TO = $semail
$oMailMsg.Subject = $sMsgSubject
$oMailMsg.HTMLBody = $body + $sSignature

任何人都知道这样的行为可能是什么原因以及如何解决?

1 个答案:

答案 0 :(得分:1)

问题可能来自$body需要一个Get-Content C:\M3_Outlook\PPS200.txt返回字符串集合的字符串这一事实。

您应该测试以下内容,它允许您将多行文本文件加载到单个字符串中:

$body = Get-Content C:\M3_Outlook\PPS200.txt -raw

对我来说,最简单的方法是使用HTML编写文本文件。