Search code examples
powershelloutlookoutlook-redemptionmsg

convert msg to html without using outlook in powershell


I want to convert msg files (in the local folder) to html in powershell. I have done this by using outlook but the problem is this script will work on server so I cant use outlook. So far, I have searched these questions:

I couldnt find many approaches with Powershell. However, with the help of the questions above I downloaded Redemption and used it like this:

$routlook = New-Object -COM Redemption.RDOSession
$routlook.Logon()
$msg = $routlook.GetMessageFromMsgFile("C:\temp\test.msg",$TRUE)
$path = "C:\temp\test.html"
$msg.HTMLBody | Set-Content $path

But $msg.HTMLBody just returns an empty html in the below and $msg.Body returns nothing:

<HTML>
<HEAD><META http-equiv=Content-Type content="text/html; charset=UTF-8"> 
</HEAD>
<BODY>
<!-- Converted from text/plain format -->
</BODY></HTML>

Probably I am using Redemption in a wrong way and dont know how to fix it. Or is there any other 3rd party solution that can be used in servers rather than using outlook?

Thanks for any help


Solution

  • The second parameter when you call GetMessageFromMsgFile is true, which means you are creating a brand new file. Pass false instead.

    Also note that GetMessageFromMsgFile does not require an active session, so there is no need to call RDOSession.Logon. And there won't be a profile if you are running under a service user anyway.

    To create an HTML file, you don't need to read the RDOMail.HTMLBody property. You can call RDOMail.SaveAs(..., olHTML).