Recently we had a need to have Dynamics AX send emails without the use of Outlook. I was presented with the following blog by Mohammed Rasheed titled Sending Emails From Dynamics AX without Outlook, which proved to be extremely useful. There was however an issue with the code (perhaps simply related to our environment) which was puzzling. The problem was that the email would send successfully, but afterward, an error was thrown stating:
ClrObject static method invocation error
The line of code that appeared to be the culprit was basically the last line in the function:
However, after analyzing things more closely the problematic line was actually the line just above:
winApi::deleteFile(fileNameforEmail); // delete temp file
As it turns out, the deleteFile() function was failing because the file had not been released by a previous process. Though it was a bit painful to figure out what was going on, the solution itself was rather simple.
Immediately after sending the email, I modified the code to dispose the objects that may have been clinging to the file
After this, the file was released, and was able to be deleted, and the error went away!