Search This Blog

Friday, November 4, 2011

AX2009 Send Mail using Mail Template in X++ code.

void krishh_UseMailTemplateAndSendMail(PurchTable _purchTable)
         {
             str                                 LIQ_message;
             Map                                 LIQ_emailParamMap;
             SysMailer     LIQ_Mailer;
       
             SysMailerAddressField               LIQ_tos;
             PurchtotalsForm                     LIQ_totalsForm;
             Amount                              LIQ_totalAmount;
             SysEmailId                           _emailId;
             SysEmailMessageTable                message;
             SysEmailTable                       Emailtable;
            
             ;
        
        
             //Get the Mail Template and Get the Message body

             _emailId = SysEmailTable::find(VendParameters::find().LIQ_DefaultMinPOTemplate).EmailId;

             Emailtable= SysEmailTable::find(_emailId);
             message=SysEmailMessageTable::find(_emailId,Emailtable.DefaultLanguage);
        
             LIQ_totalsForm   = PurchtotalsForm::newPurchTotalsForm(_purchTable, PurchUpdate::All);
             LIQ_totalsForm.calctotals();
             LIQ_totalAmount=LIQ_totalsForm.invoiceAmountValue();
        
            if (message.LayoutType == SysEmailLayoutType::StaticLayout)
            {
                     LIQ_message = message.Mail;
            }
           
            //Set the variable values for the email template.
            LIQ_emailParamMap    = new Map(Types::String,Types::String);
            LIQ_emailParamMap.insert('PurchId', _purchTable.PurchId);
            LIQ_emailParamMap.insert('Total amount',num2str(abs(LIQ_totalAmount), 0, 2, 1, 0));
            LIQ_emailParamMap.insert('currecncy',CompanyInfo::find().CurrencyCode);
            LIQ_emailParamMap.insert('PurchName',_purchTable.PurchName);
            LIQ_message=SysEmailMessage::stringExpand(LIQ_message,LIQ_emailParamMap);

//Get the Mail parameters ex:mailserver.
           if(!LIQ_mailInitialized)
           {
               this.LIQ_initMail();
           }
 //validate the Mail ID before sending the mail.
           if (LIQ_mailer != null && SysEmailDistributor::validateEmail(RCMStoreTable::find
                                                                                                       (_purchTable.InventSiteId).Email))
          {
        
          LIQ_mailer.fromAddress(SysEmailTable::find("POminvalue").SenderAddr, "@SYS115063");
          LIQ_mailer.subject('Minimum PO value');
          LIQ_tos=LIQ_mailer.tos();
          LIQ_tos.clear();
          LIQ_tos.appendAddress(RCMStoreTable::find(_purchTable.InventSiteId).Email);
          LIQ_mailer.priority(1);
          LIQ_mailer.htmlBody(strReplace(LIQ_message, '\r\n', '<br>'));
          LIQ_mailer.sendMail();
          }
}/Method Close

         void LIQ_initMail()
         {
             SysEmailParameters parameters = SysEmailParameters::find();
             InteropPermission interopPermission;
             ;
             //initialize mailer class
             interopPermission = new InteropPermission(InteropKind::ComInterop);
             interopPermission.assert();
             LIQ_mailer = new SysMailer();
             if(parameters.smtpRelayServerName)
             {
                 LIQ_mailer.SMTPRelayServer(parameters.smtpRelayServerName,
                 parameters.smtpPortNumber,
                 parameters.smtpUserName,
                 SysEmailParameters::password(),
                 parameters.ntlm);
             }
             else
             {
                 LIQ_mailer.SMTPRelayServer(parameters.smtpServerIPAddress,
                 parameters.smtpPortNumber,
                 parameters.smtpUserName,
                 SysEmailParameters::password(),
                 parameters.ntlm);
             }
             LIQ_mailInitialized = true;
        
         }

1 comment:

  1. Hello Krishna,

    Can you please let me know how do I replace the placeholders defined in my email template with the actual values from AX table?
    Have you used the placeholders in you email template in the example shown above?

    ReplyDelete

Thanks for visiting my blog,
I will reply for your comment within 48 hours.

Thanks,
krishna.