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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

Cannot attach file when sending email via STMP using X++ and .NET framework

(0) ShareShare
ReportReport
Posted on by 272

I am tasked to send a .csv file via email. 

I've searched the web for possible approaches and chose this approach:

Sending email via SMTP using X and .NET Framework in Dynamics AX | Corbitech

the only problem I have is that I am getting the exact same error from this post in this forum:

Send email with attachment - Microsoft Dynamics AX Forum Community Forum

I have searched all possible reason for the error to happen, I made sure that the folder path is accessible, also I tried to to an UNC path for the file and made sure it has access to that path as well. Also, the file is created. I am able to see it. 

Why can I write a file to that path, but I can't get the file to attach it?

I also tried instead of using this syntax:

permissionSet = new Set(Types::Class);
        permissionSet.add(new InteropPermission(InteropKind::ClrInterop));
        permissionSet.add(new FileIOPermission(filename, 'rw'));
        CodeAccessPermission::assertMultiple(permissionSet);

I used this because some articles only used this syntax:

permission = new FileIOPermission(fileName,'r');
permission.assert();

Unfortunately, everything I've researched doesn't work and the code stops at this point:

mailAttachment = new System.Net.Mail.Attachment(fileName);

Removing the line of code above, or commenting it out, it successfully sends an email. This line hinders the code to work.

There's already a lot of articles with no answer to this question, just wanted to post this again and bring it up to put an end to it.

Would like to ask for help on this. Also, is this approach still supported?

EDIT: Additional information, prior to sending an email, I have created a csv file, is it possible that the csv file I have just created cannot be accessed because it is being used by another process? Is there a way where affter creating the csv file, I can close it so no other process is using it?

Here's the version of my AX 2012:

Kernel Version: 6.2.2000.197

Application version: 6.2.1000.1437

I have the same question (0)
  • zhifeng Profile Picture
    on at

    Hello Miguel,

    First what's kind of exception did you see when you run your code? Secondly if you run a report and send to email as attachment, does it work? If it works, can you refer that code to send out attachment such as SrsReportRunPrinter::toEmail?

  • Miguel Zuniga Profile Picture
    272 on at

    Hi Kevin,

    Based on the link I used for reference, the exception used is CLRError. Does that answer your question?

    For PDF file types, we are using SSRS. But for csv, we are not.

  • ergun sahin Profile Picture
    8,826 Moderator on at

    Is fileName correct and have you checked if file is in fileName? The file may not be in the respective folder (or server / client side)

  • Martin Dráb Profile Picture
    237,880 Most Valuable Professional on at

    CLRError is just a type of exception, not the exception itself. CLR exceptions are objects, which will give you much more information. For example, you can see the class name (such as FileNotFoundException), the Message property and so on.

    Start by adding exception handling code to your solution:

    try
    {
    	... your code with .NET Interop ...
    }
    catch (Exception::CLRError)
    {
    	throw error(AifUtil::getClrErrorMessage());
    }

  • Miguel Zuniga Profile Picture
    272 on at

    Yes I've checked the file name is with a proper url for that specific file. I've tired local filepath and network file path.

  • Miguel Zuniga Profile Picture
    272 on at

    Hi Martin, it just throws out the same error. It doesn't reach the throw error(AifUtil::getClrErrorMessage());

    Also, I have one speculation that maybe you have an opinion on, because prior to sending the email, I've created a csv file, is it possible that the file is in use or not accessible because it is being used by another operation?

    I'll add this to my post as well. I'll update it

  • Martin Dráb Profile Picture
    237,880 Most Valuable Professional on at

    If you can't catch the exception, then it's either not CLRError (I suspect that this is the case) or you're catching it in a wrong way. Make sure that the code throwing the error is in the 'try' block and that your code isn't in a transaction.

    Yes, it is possible that the file is in use.

  • Miguel Zuniga Profile Picture
    272 on at

    Yes the code throwing the error is inside a try block and also isn't in a transaction it's just on a button for emailing it to a list of recipients.

  • ergun sahin Profile Picture
    8,826 Moderator on at

    Can you share the code you generated the csv? Maybe you left the file open (there are more than one csv creation method, we don't know which one you are using. If you share the codes, we can detect if there is a problem)

  • Suggested answer
    Miguel Zuniga Profile Picture
    272 on at

    Hi everyone, I've solved my problem.

    For everyone trying to send a file attachment and getting an error when the file is being attached, make sure that to finalize the file you just created or else it will be in a state where it is being used by another process. 

    In my scenario, prior to sending the email I have created a CSV file, so what I did is after the code for placing the values inside my CSV file I placed:

     commaIO.finalize();

    Others may have a different scenario, but the main point here is to close/finalize the file and make sure it is not being used. 

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 584 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 499 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 254 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans