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

Sending email template by code based on template

(0) ShareShare
ReportReport
Posted on by

I have a code sending email with attachment based email template , the issue is that a copy of the email is sent to the email specified as sender , how can I stop this ?

I have the same question (0)
  • André Arnaud de Calavon Profile Picture
    301,035 Super User 2025 Season 2 on at

    Hi Maram,

    Can you share the code you are using here? You can also review if a wrong value is used for the recipient(s).

  • maram fraij Profile Picture
    on at

    Hi André,

    Actually two emails were sent; one for the  recipient which means the provided value for  recipient email is correct and the same email is sent to the sender email and this is the issue .

    this the code that I use :

    public static void sendPDFEamilAttachment(

       SysEmailId      _emailId,

       LanguageId      _language,

       SysEmailAddress _emailAddr,

       Map             _mappings,

       container       _binData,

       str             _fileName)

       {

           SysEmailItemId                   nextEmailItemId;

           SysEmailTable                    sysEmailTable;

           SysEmailMessageTable             sysEmailMessageTable;

           SysEmailContents                 sysEmailContents;

           SysOutgoingEmailTable            outgoingEmailTable;

           SysOutgoingEmailData             outgoingEmailData;

           Filename filename, FileExtension;

           FileExtension=".pdf";

           select sysEmailTable

                  join    sysEmailMessageTable

                   where sysEmailMessageTable.EmailId==sysEmailTable.EmailId

                       && sysEmailMessageTable.EmailId== _emailId

                       && sysEmailMessageTable.LanguageId==_language;

           if(sysEmailTable.RecId>0)

           {

               sysEmailContents=SysEmailMessage::stringExpand(sysEmailMessageTable.Mail, _mappings);

               nextEmailItemId = EventInbox::nextEventId();

               filename =strFmt("%1_%2.pdf",nextEmailItemId,_fileName);

               outgoingEmailTable.clear();

               outgoingEmailTable.Origin=sysEmailTable.Description;

               outgoingEmailTable.EmailItemId = nextEmailItemId;

               outgoingEmailTable.IsSystemEmail = NoYes::No;

               outgoingEmailTable.Sender = sysEmailTable.SenderAddr;

               outgoingEmailTable.SenderName = sysEmailTable.SenderName;

               outgoingEmailTable.Recipient = _emailAddr;

               outgoingEmailTable.Subject = SysEmailMessage::stringExpand(sysEmailMessageTable.Subject, _mappings);

               outgoingEmailTable.Priority = sysEmailTable.Priority;

               outgoingEmailTable.WithRetries = NoYes::Yes;

               outgoingEmailTable.RetryNum = 0;

               outgoingEmailTable.UserId = curUserId();

               outgoingEmailTable.Status = SysEmailStatus::Unsent;

               outgoingEmailTable.Message =  sysEmailContents;

               outgoingEmailTable.LatestStatusChangeDateTime = DateTimeUtil::getSystemDateTime();

               outgoingEmailTable.TemplateId= _emailId;

               outgoingEmailTable.insert();

               if(conLen(_binData)>0)

               {

                   outgoingEmailData.clear();

                   outgoingEmailData.EmailItemId = nextEmailItemId;

                   outgoingEmailData.DataId = 1;

                   outgoingEmailData.EmailDataType = SysEmailDataType::Attachment;

                   outgoingEmailData.Data = _binData;

                   outgoingEmailData.FileName = filename;

                   outgoingEmailData.FileExtension =FileExtension;

                   outgoingEmailData.insert();

               }

           }

       }

  • Ludwig Reinhard Profile Picture
    Microsoft Employee on at

    Good morning maram fraij,

    Do you need that modification for AX2012 or D365FO?

    Can you also let us know what document / attachment you refer to and that you need to send?

    Many thanks,

    Ludwig

  • Blue Wang Profile Picture
    on at

    Hi Maram,

    Why not use SysMailerMessageBuilder.

    You can refer this : dynamicsaxinsight.wordpress.com/.../

  • André Arnaud de Calavon Profile Picture
    301,035 Super User 2025 Season 2 on at

    Hi Maram,

    What is the value provided in the variable '_emailAddr'? Is this containing one or multiple addresses? Did you check using the debugger how many times this method is being executed?

  • maram fraij Profile Picture
    on at

    Hi André ,

    _emailAddr contains the email of  the user linked with certain worker and yes I am sure that it contains only one email . if the method called more than one time I expect that the receiver will receive multiples email not the email sent to different receivers  

  • maram fraij Profile Picture
    on at

    Hi Ludwig ,

    I have custom report convert to pdf and and I send it to worker . This part works correctly but the issue is that the sender also receives a copy from the same email.

    Environment is D365FO

  • Suggested answer
    Jay Barot Profile Picture
    1,502 on at

    Hi Maram,

    Can you check the joins used in code. Either it's getting 2 records because 2 conditions for emailId field.

  • maram fraij Profile Picture
    on at

    Hi Jay,

    I do not have any join query in my code

  • Jay Barot Profile Picture
    1,502 on at

    Screenshot_5F00_20200607_2D00_163024_7E00_2.png

    i was talking about this. 

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