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

  • Suggested answer
    ergun sahin Profile Picture
    8,816 Moderator on at
    RE: Cannot attach file when sending email via STMP using X++ and .NET framework

    It is possible to find many examples on the web, I shared a few below but you probably just need to add a single line.

    File.finalize ();

    community.dynamics.com/.../export-data-to-csv-file-795497558

    community.dynamics.com/.../code-to-create-table-data-in-csv-file-in-axapta-2012

    community.dynamics.com/.../598991

    vimalaxapta.blogspot.com/.../how-to-export-data-from-microsoft.html

  • Miguel Zuniga Profile Picture
    272 on at
    RE: Cannot attach file when sending email via STMP using X++ and .NET framework

    Hi Ergun,

    Do you have links/references for these csv creation method? Would like to know more about it.

  • Suggested answer
    Miguel Zuniga Profile Picture
    272 on at
    RE: Cannot attach file when sending email via STMP using X++ and .NET framework

    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. 

  • ergun sahin Profile Picture
    8,816 Moderator on at
    RE: Cannot attach file when sending email via STMP using X++ and .NET framework

    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)

  • Miguel Zuniga Profile Picture
    272 on at
    RE: Cannot attach file when sending email via STMP using X++ and .NET framework

    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.

  • Martin Dráb Profile Picture
    232,879 Most Valuable Professional on at
    RE: Cannot attach file when sending email via STMP using X++ and .NET framework

    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
    RE: Cannot attach file when sending email via STMP using X++ and .NET framework

    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

  • Miguel Zuniga Profile Picture
    272 on at
    RE: Cannot attach file when sending email via STMP using X++ and .NET framework

    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.

  • Martin Dráb Profile Picture
    232,879 Most Valuable Professional on at
    RE: Cannot attach file when sending email via STMP using X++ and .NET framework

    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());
    }

  • ergun sahin Profile Picture
    8,816 Moderator on at
    RE: Cannot attach file when sending email via STMP using X++ and .NET framework

    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)

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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,137 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,879 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Product updates

Dynamics 365 release plans