0

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

5
  • are we talking a outlook msg file?
    – ArcSet
    Oct 31, 2018 at 1:44
  • Yes, I am trying this for outlook msg files. Oct 31, 2018 at 1:53
  • are you just trying to recover text or what ?
    – ArcSet
    Oct 31, 2018 at 1:53
  • You can find the MS-OXMSG standards here link
    – ArcSet
    Oct 31, 2018 at 1:58
  • I am trying to get mail's body content and convert it to an html file. $msg.HTMLBody creates an html file, as I wanted, but with nothing in the body section. Oct 31, 2018 at 2:00

3 Answers 3

2

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).

2
  • Thank you for your answer. I tried it but the html file that has been created doesnt have any html tags and includes only this kind of things: ÐÏࡱá ÿÿÿÿ Oct 31, 2018 at 2:15
  • 1
    I just checked it and found out that it was my mistake, I had passed invalid variant for olHTML. Sorry for that, it works with your solution as well. Thanks Oct 31, 2018 at 4:47
2

You need to remove the $TRUE parameter:

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

And you can open your msg file and after that, save as html

1
  • Yes, that helped a lot ! It created a proper html file. Thanks Oct 31, 2018 at 3:12
1

To make it clear, it worked fine with both answers and my complete code for either ways:

$routlook = New-Object -COM Redemption.RDOSession
$location = "C:\temp\Redemption\"
$msg = $routlook.GetMessageFromMsgFile("C:\temp\test.msg",$FALSE)
$name = "test.html"
$path = $location + $name
$msg.SaveAs($path,5)

Or

$routlook = New-Object -COM Redemption.RDOSession
$location = "C:\temp\Redemption\"
$msg = $routlook.GetMessageFromMsgFile("C:\temp\test.msg")
$name = $_.Name + ".html"
$path = $location + $name
$msg.HTMLBody | Set-Content $path

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.