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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Send email with attachment

(0) ShareShare
ReportReport
Posted on by

Hi,

this is the code I am using to send email with attachment.
I am able to send email without attachment with the below code by commenting the bold lines , but while using the bold lines it gives an exception stating the "clr object could not be created"  on line 2

public void sendMail()
{
str sender = "abc"mail id of the sender
str recipient = "xyz"; //mulitple recipients can be specified
str cc = "pqr"; // mulitple cc id's can be specified
str subject = "seat master excel file";
str body = "Please find the attachment";
str fileName = @"C:\\Users\\admin\\Desktop\\seat_master.xlsx"; //Location of the attachment file


List toList;
List ccList;
ListEnumerator enumList;
Set permissionSet;
System.Exception exception;

str mailServer;
int mailServerPort;
System.Net.Mail.SmtpClient mailClient;
System.Net.Mail.MailMessage mailMessage;
System.Net.Mail.MailAddress mailFrom;
System.Net.Mail.MailAddress mailTo;
System.Net.Mail.MailAddressCollection mailToCollection;
System.Net.Mail.MailAddressCollection mailCCCollection;
System.Net.Mail.AttachmentCollection mailAttachementCollection;
System.Net.Mail.Attachment mailAttachment;
FileIOPermission permission;
;

try
{
toList = strSplit(recipient, ';');
ccList = strSplit(cc, ';');


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

mailServer = SysEmaiLParameters::find(false).SMTPRelayServerName;
mailServerPort = SysEmaiLParameters::find(false).SMTPPortNumber;
mailClient = new System.Net.Mail.SmtpClient(mailServer,mailServerPort);

enumList = toList.getEnumerator();
enumList.moveNext();

mailFrom = new System.Net.Mail.MailAddress(sender);
mailTo = new System.Net.Mail.MailAddress(strLTrim(strRTrim(enumList.current())));
mailMessage = new System.Net.Mail.MailMessage(mailFrom, mailTo);

mailToCollection = mailMessage.get_To();
while (enumList.moveNext())
{
mailToCollection.Add(strLTrim(strRTrim(enumList.current())));
}

enumList = ccList.getEnumerator();
mailCCCollection = mailMessage.get_CC();
while (enumList.moveNext())
{
mailCCCollection.Add(strLTrim(strRTrim(enumList.current())));
}

mailMessage.set_Priority(System.Net.Mail.MailPriority::High);
mailMessage.set_Subject(subject);
mailMessage.set_Body(body);

mailAttachementCollection = mailMessage.get_Attachments(); //1
mailAttachment = new System.Net.Mail.Attachment(fileName); //2
mailAttachment.set_Name("seat_master"); //3
mailAttachementCollection.Add(mailAttachment); //4

mailClient.Send(mailMessage);
mailMessage.Dispose();

CodeAccessPermission::revertAssert();

info("Email sent successfully");
}
catch (Exception::CLRError)
{
exception = ClrInterop::getLastException();
while (exception)
{
info(exception.get_Message());
exception = exception.get_InnerException();
}
CodeAccessPermission::revertAssert();
}

}

*This post is locked for comments

I have the same question (0)
  • Jie G Profile Picture
    Microsoft Employee on at

    Hi adisonone,

    Try to modify the fileName from  @"C:\\Users\\admin\\Desktop\\seat_master.xlsx" to  @"C:\Users\admin\Desktop\seat_master.xlsx"

  • Martin Dráb Profile Picture
    239,069 Most Valuable Professional on at

    First of all, is the path valid? If the code executes on server, which it normally should, it there a file at "C:\Users\admin\Desktop on the AOS server and does the AOS service account access to admin's Desktop folder? If it's there, it's an extremely weird location for these purposes. Also, if you execute your code on server and you have more than one AOS, does the file exist on the server currently executing the code?

    Please test whether the file exist on the machine where the code executes and whether the account has access to it. It looks like the most probable reason.

  • Community Member Profile Picture
    on at

    tried this but not working

  • Community Member Profile Picture
    on at

    I am running on client machine and yes this path does exists and the file too.

  • Martin Dráb Profile Picture
    239,069 Most Valuable Professional on at

    You didn't address the question of permissions. I recommend you test it; I suspect you're just assuming that everything is correct.

    Anyway, the next thing you should try is catching the exception. Examine it and all its inner exceptions, their types, messages and so on. It should give you much more details about the failure.

  • Community Member Profile Picture
    on at

    Suggest what changes do I need to make in this code.

    Yes I am assuming everything's correct because I am able to successfully send email with this code excluding the file attachment (commenting 4 bold lines).

  • Suggested answer
    Vilmos Kintera Profile Picture
    46,149 on at

    As it was already suggested you are trying to use literal string with double backslashes, which is not a valid path. Double backslash is used to escape the control characters like line break \n, tab \t and so on. However literal string passes it in as is, in which case your path is wrong.

    Now also depending on where you run your code, the path must be local to that. If it runs on server-side, whichever AX AOS is executing it should have your file in that location within the C:\ drive local to that server, and must have permissions to that by the AOS service account, or the specific user account if impersonation is 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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Joris dG Profile Picture

Joris dG 5

#2
Andrew Jones a1x Profile Picture

Andrew Jones a1x 2

#3
GL-01081504-0 Profile Picture

GL-01081504-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans