2017-05-23 89 views
1

如果文本包含多個句子,則向文本添加文本不會正確添加任何語句。來自文件的正文文本

Content @爲空。

它只添加一個簽名。

輸入文件(名稱:Text_file.txt):

 
email:[email protected] 
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 

任何人都知道,這可能是對這種行爲的原因以及如何解決它?

+0

我不清楚你面臨什麼問題。你的代碼中哪部分不起作用,以及它究竟如何「不起作用」,即什麼是期望的和實際的結果? –

回答

1

它的問題可能來自於$body需要一個字符串,Get-Content C:\M3_Outlook\PPS200.txt返回字符串的集合。

您應該測試下面,讓你多行文本文件加載到一個字符串:

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

對我來說,最簡單的方法是寫使用HTML文本文件。

+0

它很好用 這就是它的原因。 非常感謝你 –

+0

所以你可以把問題的答案? – JPBlanc