web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Email Distributor batch Job not deleting file

(0) ShareShare
ReportReport
Posted on by 1,270

Hi,

  I'm hitting an issue in AX 2012 CU3 in the email distribution batch job. When sending multiple emails with the same named attachment only the first email will send and the rest will fail.  After some debugging I've found this the issue is in SysEmailDistributor.processemails() where the WinAPIserver::deletefile() fails to actually delete the attachment out of the temp directory.  It doesn't actually throw an error at this point but when it loops around the second time to the bindata.savefile() and tries to rewrite the same names file to this directory it jumps out to the exception catching and fails the email.  Strangely, when the batch job has finished processing the file then disappears (sometime after exiting the BatchRun::servergetNextTask() method).

Before both the save and delete command are the same permission assertions:

              fileIOPermission = new FileIOPermission(filePathName,'w');
         fileIOPermission.assert();

The permissions on the temp folder give full permission to the account under which the AX service is running.

I wouldn't expect it to be a coding error since its all unaltered foundation layer code.


Can anyone offer any insights as to what may be going on?  Why would the save work fine but the delete not work?  Are API delete calls somehow subject to ttsbegin / commit calls?

Any thoughts are apprecitated.

Cheers

David

 

 

 

*This post is locked for comments

I have the same question (0)
  • Jonathan  Halland Profile Picture
    11,310 on at
    RE: Email Distributor batch Job not deleting file

    Are you sending emails from custom code?

  • David Rose Profile Picture
    1,270 on at
    RE: Email Distributor batch Job not deleting file

    Hi Jonathan,

      Yes, the job generating the emails is custom.  I replicate the issue in a very simple job.  In this case I only receive the first email and the second 2 fail.

    void SendMail()

    {

       int i;

       Filename  filename1 = @'\\TEST-AX12-AOS\Email\Pickinglist1.pdf';

       for(i=1; i <=3; i++)

       {

           SysEmailTable::sendMail('smartsend',

                                   CompanyInfo::find().LanguageId, // Receiver's language

                                   'my@email.address',//Receiver

                                   null,

                                   Filename1, // Report file path

                                   '',

                                   true,

                                   curUserId(),

                                   false);

      }

    }

  • Verified answer
    Community Member Profile Picture
    on at
    RE: Email Distributor batch Job not deleting file

    I suggest you to copy the file with some other name and the  attach it to the mail.

    otherwise mail will have same file attached thrice which will be confusing

    other point the issue what you are facing is may be because of a file attached is getting deleted...

    So please try by copying the file before attaching it to the email.

    Please verify and update us with your findings

  • Suggested answer
    Jonathan  Halland Profile Picture
    11,310 on at
    RE: Email Distributor batch Job not deleting file

    Hi David.

    I Agree with Nitesh, hence my original question. I would create a copy of the file for each email.  If they are all being deleted then it won't waste space.

    Regards

  • David Rose Profile Picture
    1,270 on at
    RE: Email Distributor batch Job not deleting file

    Hi Jonathan / Nitesh,

      Thanks for your response.  The working code in the system is some VAR code for a modification that is designed to automatically email our a bunch of statements to customers.  The attachment name is always the same (i.e statement.pdf) but each email in the batch will be going off to a different email address so its not a case of one person receiving an email with a bunch of identically named attachments.

    The main issue I was trying to figure out was why the WinAPI::deletefile() method was not deleting the file and not erroring out.  If I could resolve this there would no longer be a problem.  

    If I can't figure this out, perhaps I'll follow your thoughts and do something like append the customer name to the attachment so each instance in the temp folder is unique.

    Regards

    David

  • Verified answer
    David Rose Profile Picture
    1,270 on at
    RE: Email Distributor batch Job not deleting file

    Hi All,

      I got to the bottom of this.  The problem was the mailer object which had just received the file as an attachment was then holding some kind of lock on the file preventing the delete.  Once the mailer object was finalised the delete could occur.

     In the end I modified some other code to make the filenames unique so I could completely avoid this bug.

    Regards

    David

  • Suggested answer
    Community Member Profile Picture
    on at
    RE: Email Distributor batch Job not deleting file

    Awesome! That seems to be the cause for the problem. Thanks David!

  • Jonathan  Halland Profile Picture
    11,310 on at
    RE: Email Distributor batch Job not deleting file

    Hi David.

    Have you found where these file locks are occurring? We are trying to send out certain documents via attachments that need to have the same file name on all mails. Even after removing the auto-deleting of attachments the mail queue fails when trying to attach the file to the second email due to file logs.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Community Member Profile Picture

Community Member 4

#1
Martin Tocauer Profile Picture

Martin Tocauer 4

#3
Nayyar Siddiqi Profile Picture

Nayyar Siddiqi 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans