Skip to main content

Notifications

Announcements

No record found.

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

  • Suggested answer
    Vilmos Kintera Profile Picture
    Vilmos Kintera 46,149 on at
    RE: Send email with attachment

    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.

  • RE: Send email with attachment

    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).

  • Martin Dráb Profile Picture
    Martin Dráb 231,409 Most Valuable Professional on at
    RE: Send email with attachment

    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.

  • RE: Send email with attachment

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

  • RE: Send email with attachment

    tried this but not working

  • Martin Dráb Profile Picture
    Martin Dráb 231,409 Most Valuable Professional on at
    RE: Send email with attachment

    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.

  • Jie G Profile Picture
    Jie G on at
    RE: Send email with attachment

    Hi adisonone,

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

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February 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... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,409 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans