Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- .SYNOPSIS
- Parses a MIME email document into a CDO.Message object.
- .DESCRIPTION
- Reads the parameter MIME file into a CDO.Message (CDO for Windows 2000) COM object.
- The returned CDO.Message object can be used to access the MIME file contents in decoded form.
- .PARAMETER FileSpec
- The file specification to be parsed, e.g. "C:\demo\test.eml".
- .EXAMPLE
- Get-ParsedMimeFile -FileSpec "C:\demo\test.eml"
- .OUTPUTS
- A CDO.Message object representing the parsed MIME document. See http://msdn.microsoft.com/en-us/library/ms526453(v=exchg.10).aspx
- .LINK
- http://msdn.microsoft.com/en-us/library/ms526453(v=exchg.10).aspx
- peter.karsai@vamsoft.com
- #>
- function Get-ParsedMimeFile([string] $FileSpec)
- {
- # read MIME into a byte array
- $mimeBytes = [System.IO.File]::ReadAllBytes($FileSpec);
- # load byte array into an ADODB stream, which will act as data source for CDO
- $stream = New-Object -ComObject "ADODB.Stream";
- $stream.Open();
- $stream.Type = 1; # adTypeBinary, i.e. binary stream
- $stream.Write($mimeBytes);
- $stream.Flush();
- $stream.SetEOS();
- # create CDO.Message and assign stream as data source
- $message = New-Object -ComObject "CDO.Message";
- try
- {
- $message.DataSource.OpenObject($stream, "_Stream");
- }
- finally
- {
- $stream.Close();
- }
- return $message;
- }
- # DEMO
- $message = Get-ParsedMimeFile -FileSpec "C:\demo\test.eml";
- # dump subject
- Write-Output "Email subject: $($message.Subject)";
- # dump all parsed header fields
- foreach($field in $message.Fields) {
- Write-Output "Field $($field.Name) => $($field.Value)";
- }
- # dump decoded email text body
- Write-Output "Text body follows:`r`n$($message.TextBody)";
- # dump decoded email HTML body
- Write-Output "HTML body follows:`r`n$($message.HtmlBody)";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement