Page 1 of 1

winmail.dat convert generate error

Posted: 2017-12-12 13:31
by DanneG
Hi!

I try to use a script I found in the forum to convert winmail.dat files. The files are saved to disk but when trying to load the files back into the mail, I get an error message.
Can anyone see why?

"ERROR" 3032 "2017-12-12 00:04:04.425" "Script Error: Source: hMailServer COM library - Error: 800403E9 - Description: Failed to attach file. - Line: 119 Column: 8 - Code: (null)"

Line 119 is:

Code: Select all

Call oMessage.Attachments.Add(obFile.Path)
Here is the code:

Code: Select all

	Const g_sTempPath = "D:\temp"
	Const g_sTnefPath = """C:\Program\Winmail Opener\wmopener.exe"""
	Const g_sCmdPath = "c:\WINDOWS\system32\cmd.exe"

   Sub OnDeliverMessage(oMessage)
	ExtractTNEFAttachments(oMessage)
   End Sub

Sub ExtractTNEFAttachments(oMessage)
' Iterates over all attachments and checks where
' there's a TNEF-attachment we should decode.
Dim obAttachments
Set obAttachments = oMessage.Attachments

For i = 1 to obAttachments.Count 
dim obAttachment
set obAttachment = obAttachments.Item(i-1)
' DebugLog.Log "Attachment Filename: " & obAttachment.FileName, null

If trim(LCase(obAttachment.FileName)) = "winmail.dat" OR trim(LCase(obAttachment.FileName)) = "win.dat" Then
Call ExtractTNEF(oMessage, obAttachment)
End If
Next
End Sub

Sub ExtractTNEF(oMessage, obAttachment)
' Extracts the TNEF attachments from a winmail.dat

' Generate a random string. Used for directory name.
Dim obUtilities
Set obUtilities = CreateObject("hMailServer.Utilities")
Dim sGUID 
sGUID = obUtilities.GenerateGUID
Set obUtilities = Nothing

' Generate GUID-based temporary directory name
Dim sOutPath
sOutPath = g_sTempPath & "\" & sGUID

' Create the directory
Dim obFS 
Set obFS = CreateObject("Scripting.FileSystemObject")
Call obFS.CreateFolder(sOutPath)

' Extract the file to a temporary directory.
Dim sAttachmentFile
sAttachmentFile = sOutPath & "\winmail.dat"
Call obAttachment.SaveAs(sAttachmentFile)

' Create an output directory for the extracted
' attachments.
Dim sAttachmentsDirctory
sAttachmentsDirctory = sOutPath & "\output"
Call obFS.CreateFolder(sAttachmentsDirctory)

' Run tnef-decoder.
Dim sCommandLine
sCommandLine = g_sTnefPath & " " & sAttachmentFile & " " & sAttachmentsDirctory
Call RunCommand(sCommandLine)

' Check the output directory for extracted files
Dim obFiles
Set obFiles = obFS.GetFolder(sAttachmentsDirctory).Files

      ' Go through the output directory, and see if theres
      ' any extracted ifles      
      Dim obFile
      For Each obFile in obFiles
        ' Add this as an attachment to the message,
        ' if it's not named winmail.dat. No point
        ' in adding itself...
        
        Call oMessage.Attachments.Add(obFile.Path)
      Next

If obFiles.Count > 0 Then
' Delete the original attachment.
obAttachment.Delete 

' Since the message has been modified, we need to save it.
oMessage.Save 
End If

Set obFiles = Nothing

' Delete the temporary folder
Call obFS.DeleteFolder(sOutPath)
End Sub

Function RunCommand(sCommand)
' Runs a command
Dim obShell
Set obShell = CreateObject("WScript.Shell")

' Run the command
obShell.Run sCommand, 0, True

Set obShell = Nothing
End Function

Re: winmail.dat convert generate error

Posted: 2017-12-13 00:23
by mattg
I suspect that it has to do with the D Drive path for the temp directory

IS D a mapped drive or a local drive? Can you use a temp directory from the C drive

Re: winmail.dat convert generate error

Posted: 2017-12-13 09:30
by DanneG
D is local drive, changed to c:\tmp and the same result.

I can see the output folder on C:\tmp\{081A271F-4D05-4E16-B114-47249DB5E874} and all the files in the winmail.dat in the directory

Re: winmail.dat convert generate error

Posted: 2017-12-13 09:33
by jimimaseye

Code: Select all

   Const g_sTnefPath = """C:\Program\Winmail Opener\wmopener.exe"""
Have you installed this? Where did you get this from?

(What is the original thread of the script?)

Re: winmail.dat convert generate error

Posted: 2017-12-13 09:42
by DanneG
jimimaseye wrote:

Code: Select all

   Const g_sTnefPath = """C:\Program\Winmail Opener\wmopener.exe"""
Have you installed this? Where did you get this from?

(What is the original thread of the script?)
Yes the program is installed and working (http://winmail-opener.software.informer.com/1.5/)

The script is saving and extracting the winmail.dat file without problem.
the problem occurs when it´s loading the extracted files into the mail.
Image

Re: winmail.dat convert generate error

Posted: 2017-12-13 12:03
by estradis
DanneG wrote:Hi!

I try to use a script I found in the forum to convert winmail.dat files. The files are saved to disk but when trying to load the files back into the mail, I get an error message.
Can anyone see why?

"ERROR" 3032 "2017-12-12 00:04:04.425" "Script Error: Source: hMailServer COM library - Error: 800403E9 - Description: Failed to attach file. - Line: 119 Column: 8 - Code: (null)"

Line 119 is:

Code: Select all

Call oMessage.Attachments.Add(obFile.Path)
Here is the code:

Code: Select all

...
Call ExtractTNEF(oMessage, obAttachment)
....
Call obFS.CreateFolder(sOutPath)
....
Call obAttachment.SaveAs(sAttachmentFile)
....
Call obFS.CreateFolder(sAttachmentsDirctory)
....
Call RunCommand(sCommandLine)
....
        Call oMessage.Attachments.Add(obFile.Path)
....
Call obFS.DeleteFolder(sOutPath)
....
I guess, your "calls" are the problem. You don't need them inside your script.

Code: Select all

...
If trim(LCase(obAttachment.FileName)) = "winmail.dat" OR trim(LCase(obAttachment.FileName)) = "win.dat" Then
Call ExtractTNEF(oMessage, obAttachment)
End If

Sub ExtractTNEF(oMessage, obAttachment)
' Extracts the TNEF attachments from a winmail.dat
....
You should replace with

Code: Select all

...
If trim(LCase(obAttachment.FileName)) = "winmail.dat" OR trim(LCase(obAttachment.FileName)) = "win.dat" Then
ExtractTNEF oMessage, obAttachment 
End If

Sub ExtractTNEF(oMessage, obAttachment)
' Extracts the TNEF attachments from a winmail.dat
....
The brackets must not be used when no result is returned.

Hope that helps

Re: winmail.dat convert generate error

Posted: 2017-12-13 12:25
by DanneG
I think this script orginal is for Hmailser 4..
Perhaps i need to use other functions?

I Still get:

Code: Select all

"ERROR"	2652	"2017-12-13 11:21:36.457"	"Script Error: Source: hMailServer COM library - Error: 800403E9 - Description: Failed to attach file. - Line: 119 Column: 8 - Code: (null)"
Line 119 is:

Code: Select all

        oMessage.Attachments.Add(obFile.Path)
My code:

Code: Select all

	
	Const g_sTempPath = "C:\tmp"
	Const g_sTnefPath = """C:\Program\Winmail Opener\wmopener.exe"""
	Const g_sCmdPath = "c:\WINDOWS\system32\cmd.exe"

   Sub OnDeliverMessage(oMessage)
	ExtractTNEFAttachments(oMessage)
   End Sub

Sub ExtractTNEFAttachments(oMessage)
' Iterates over all attachments and checks where
' there's a TNEF-attachment we should decode.
Dim obAttachments
Set obAttachments = oMessage.Attachments

For i = 1 to obAttachments.Count 
dim obAttachment
set obAttachment = obAttachments.Item(i-1)
' DebugLog.Log "Attachment Filename: " & obAttachment.FileName, null

If trim(LCase(obAttachment.FileName)) = "winmail.dat" OR trim(LCase(obAttachment.FileName)) = "win.dat" Then
ExtractTNEF oMessage, obAttachment 
End If
Next
End Sub

Sub ExtractTNEF(oMessage, obAttachment)
' Extracts the TNEF attachments from a winmail.dat

' Generate a random string. Used for directory name.
Dim obUtilities
Set obUtilities = CreateObject("hMailServer.Utilities")
Dim sGUID 
sGUID = obUtilities.GenerateGUID
Set obUtilities = Nothing

' Generate GUID-based temporary directory name
Dim sOutPath
sOutPath = g_sTempPath & "\" & sGUID

' Create the directory
Dim obFS 
Set obFS = CreateObject("Scripting.FileSystemObject")
obFS.CreateFolder(sOutPath)

' Extract the file to a temporary directory.
Dim sAttachmentFile
sAttachmentFile = sOutPath & "\winmail.dat"
obAttachment.SaveAs(sAttachmentFile)

' Create an output directory for the extracted
' attachments.
Dim sAttachmentsDirctory
sAttachmentsDirctory = sOutPath & "\output"
obFS.CreateFolder(sAttachmentsDirctory)

' Run tnef-decoder.
Dim sCommandLine
sCommandLine = g_sTnefPath & " " & sAttachmentFile & " " & sAttachmentsDirctory
RunCommand(sCommandLine)

' Check the output directory for extracted files
Dim obFiles
Set obFiles = obFS.GetFolder(sAttachmentsDirctory).Files


      ' Go through the output directory, and see if theres
      ' any extracted ifles      
      Dim obFile
      For Each obFile in obFiles
        ' Add this as an attachment to the message,
        ' if it's not named winmail.dat. No point
        ' in adding itself...
        
        oMessage.Attachments.Add(obFile.Path)
      Next

If obFiles.Count > 0 Then
' Delete the original attachment.
obAttachment.Delete 

' Since the message has been modified, we need to save it.
oMessage.Save 
End If

Set obFiles = Nothing

' Delete the temporary folder
obFS.DeleteFolder(sOutPath)
End Sub

Function RunCommand(sCommand)
' Runs a command
Dim obShell
Set obShell = CreateObject("WScript.Shell")

' Run the command
obShell.Run sCommand, 0, True

Set obShell = Nothing
End Function

Re: winmail.dat convert generate error

Posted: 2017-12-13 13:03
by estradis
DanneG wrote:I think this script orginal is for Hmailser 4..
Perhaps i need to use other functions?

I Still get:

Code: Select all

"ERROR"	2652	"2017-12-13 11:21:36.457"	"Script Error: Source: hMailServer COM library - Error: 800403E9 - Description: Failed to attach file. - Line: 119 Column: 8 - Code: (null)"
Line 119 is:

Code: Select all

        oMessage.Attachments.Add(obFile.Path)
My code:

Code: Select all

	
...
      ' Go through the output directory, and see if theres
      ' any extracted ifles      
      Dim obFile
      For Each obFile in obFiles
        ' Add this as an attachment to the message,
        ' if it's not named winmail.dat. No point
        ' in adding itself...

' Guess that "DebugLog.Log" is your way to log outputs?
' If yes, try this:
' DebugLog.Log "objFile.Path= *" & objFile.Path & "*", null
        
        oMessage.Attachments.Add(obFile.Path)
      Next
...
Have you already tried to output the value of objFile.Path, immediately before output?

PS: I wrote the message above because I experienced a lot of such problems in vbscript using unnecessary calls or brackets.