Thursday, December 15, 2022

Sending Email using X++ Code in D365 F&O/ AX 7 in HTML fomat

 I was just researching on the emailing capabilities in D365 and found that SysINetMail, SysMailer and some of the smmOutlook classes are deprecated because these classes used predominantly client-side technologies that are no longer available

Below is the quick code snippet that can be used in D365 to send emails. Please note, this is just a untested sample code, with out any setups configured. please ensure all setups are done and improvise the code accordingly.
SysMailerFactory class internally used SysImailer* classes to send emails.

[Form]
public class Irfantest extends FormRun
{
    str                                 header,mainbody,footer;
    str                                     strBody;
    str                                     strFileName,email;
    SysMailerMessageBuilder mail = new SysMailerMessageBuilder();
//send mail to To address    
void sendmailtousers()
    {
        this.htmlbody();
        Email = "";
        Email ="syedirfanReciever@company.com";
        if(Email != "")
        {
       this.sendmail(Email);
        }
    }
// Create a content for body in  HTML format 
    void htmlbody()
    {
        date subdate;
        str comp,event;
        ;
        header = "" ;
        mainbody = "";
        footer = "";
        comp    = "Ahmed Soliman Al Fahhad (Closed Joint Stock)";
        event   = "test ALERTS";
        header = strfmt("<p><strong><font color=#003399 face=Arial>test alert </font></strong> </p></br>"+
        "<p><strong><font color=#003399 face=Tahoma> االتاشيره  </font></strong> </p></br>"+
          "<TABLE cellSpacing=0 cellPadding=1 width='100%' border=3>"+
       "<tr bgColor=gray> "+
       "<td align=center><font  size=2 face=Arial>الرقم التسلسلي</td>"+//sn
       "<td align=center><font  size=2 face=Arial>‏‏رمز الإجازة</td>"+//leave code
       "<td align=center><font  size=2 face=Arial>الموظف</td>"+//employee
        "<td align=center><font  size=2 face=Arial>اسم الموظف</td>"+//employee
       "<td align=center><font  size=2 face=Arial>رقم الاقامة</td>"+//iqama no.
       "<td align=center><font  size=2 face=Tahoma>تاريخ انتهاء الإقامة</td>"+//iqamaexpire date
       "<td align=center><font  size=2 face=Arial>اسم المشروع</td>"+//project name
      
       "</tr>");

        mainbody  += strfmt("<TR ALIGN='CENTER'>"+
                "<TD><font color=#003399 size=2 face=Arial>" +  num2str(1,3,0,0,0) + "</TD>"+
                "<TD><font color=#003399 size=2 face=Arial>" +  "Leave" + "</TD>"+
                "<TD><p><font color=#003399 size=2 face=Arial>" +  "40157" +"</p></TD>"+
                 "<TD><font color=#003399 size=2 face=Tahoma>" +  "syed irfan gaffoor"+"</TD>"+
                "<TD><p><font color=#003399 size=2 face=Arial>" +  "12235455"+ "</p></TD>"+
                 "<TD><p><font color=#003399 size=2 face=Tahoma>" +  "1447/04/28" +"</p></TD>"+
                "<TD><font color=#003399 size=2 face=Arial>" +  "Head office"+"</TD>"+
                 
                "</TR>" );
        footer = strfmt( "</TABLE></br><font color=#cccccc size=2 face=Arial><p>_______________________________________________________________________ </p>"+
       "</font><b><font color=#003399 face=Arial><p>Alert details<font color=#003399 size=2 face=Arial></font></p>"+
       "</font></b><font color=#003399 size=2 face=Arial></font><font color=#cccccc size=2><p>"+
       "<table border= 5 cellSpacing=1 cellPadding=3>"+
       "<tbody>"+
       "<tr> <td><p align=left><b><font  size=2 face=Arial>Date:</font></p></td> <td><p><font face=Arial><font color=#003399 size=2>"+date2str(today(),123,2,4,3,4,4)+"</font></font></p></td></tr>"+
       "<tr> <td><p align=left><b><font size=2><font  face=Arial>Company:</font></font></p></td><td><font face=Arial><font color=#003399><font size=2> "+comp+" </font> </font></font></td></tr>"+
       "<tr> <td><font face=Arial><b><font size=2 face=Arial>Event:</font></font></td><td><font face=Arial><font color=#003399 size=2 face=Arial>"+event+"</font></font></td></tr>"+
       "</tbody> </table>") ;
    }
//call SysMailerMessageBuilder  for send mail
    void sendmail(str to)
    {
        strBody =header+mainbody+footer;
        try
        {
            mail.setBody(strBody, true);
            mail.setSubject("Email Test from D365");
            mail.addTo(to);
            mail.setFrom("sender@company.com");
            //SysMailerFactory::sendInteractive(mail.getMessage()); // with dialog box 
            SysMailerFactory::sendNonInteractive(mail.getMessage());//without diloag box
            
        }
        catch (Exception::Error)
        {
            throw error("@SYS33567");
        }
    }
//form button click event
    [Control("Button")]
    class FormButtonControl1
    {
        public void clicked()
        {
            super();
            element.sendmailtousers();
        }
    }
}

output:






No comments:

Post a Comment

Sending Email using X++ Code in D365 F&O/ AX 7 in HTML fomat

  I was just researching on the emailing capabilities in D365 and found that SysINetMail, SysMailer and some of the smmOutlook classes are d...