Home > Blockchain >  HTML email body truncation while sending email from outlook using PowerShell
HTML email body truncation while sending email from outlook using PowerShell

Time:04-02

I have one HTML file which i want to send from PowerShell using outlook. I have used below code however it is truncating the email body after 603 chars, hence only upper half of the html page is going as body in outllook email.

$body = Get-Content -Path .\\Output.html  #this have around 1500 chars.
$Outlook = New-Object -ComObject Outlook.Application
$Mail = $Outlook.CreateItem(0)
$Mail.To = "[email protected]"
$Mail.Subject = "Daily Dump Status"
$body.ToString()
$Mail.HTMLBody = $body  # while coping the data only 603 char are going into body.
$Mail.Send()
[System.Runtime.Interopservices.Marshal\]::ReleaseComObject($Outlook) | Out-Null

I have also tried options like Out-String while coping but didnt work

CodePudding user response:

There is a limit of data you could set for a property. In the Outlook object model string properties are limited in size depending on the information store type.

To bridge the gap you need to use a low-level API on which Outlook is based on - Extended MAPI which allows opening properties with stream for writing huge amount of data. The IMAPIProp::OpenProperty method provides access to a property through a particular interface. OpenProperty is an alternative to the IMAPIProp::GetProps and IMAPIProp::SetProps methods. When either GetProps or SetProps fails because the property is too large or too complex, call OpenProperty.

You may also take a look at the famous and popular wrapper around Extended MAPI - the Redemption library.

CodePudding user response:

After spending hours, finally got the root cause of this issue. The html page which i was trying to sent as email body was the output of another powershell command using ConvertTo-Html. And the output of PowerShell command had "NUL" in it.

$Mail.HTMLBody = $body 

Hence above command was coping the data till first "NUL". I identified this by opening the html page in notepad . Issue got resolved after replacing NUL with " "

  • Related