Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX (Archived)

Sys mailler object not initialized Error while sending mail through code

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

Below code is running fine but only while it is going to    mailer.body(textmail1); it is giving error stating
Sys mailler object not initialized

 

 

void clicked()
{
    sysemailparameters      parameter    = sysemailparameters::find();
    Dialog                  dialogMail;
    DialogField             ilfsMail;
    DialogField             dfsubject;
    DialogField             dfTowhom;
    DialogField             dftextmail;
    itl_ib_remarks          textmail;
    str                     textMail1;
    str                     Towhome1;
    Email                   Towhome;
    SysMailer               mailer,mailer1;
    itl_ib_remarks          text1;
    itl_ib_remarks          Bodytext;
    str                     username;
    itl_ib_remarks          useremail,COEmail,accessemails;
    Name                    fromName;
    str                     fromEmail;
    userinfo                userinfo;

   ITL_COC_ReqForSecRestrictnDetails    SecRestrictnDetails;
   ;
    if(!ITL_COC_ReqForSecRestrictn.RecId)
    {
        throw Error("No Record Found.");
    }
    else if(!ITL_COC_ReqForSecRestrictnDetails && ITL_COC_ReqForSecRestrictn.RequisitionType == ITL_COC_RequisitionType::Restriction)
    {
        throw Error("No record found in line table.");
    }
    else
    {
       while select SecRestrictnDetails
        where SecRestrictnDetails.RequisitionNo   == ITL_COC_ReqForSecRestrictn.RequisitionNo
           && SecRestrictnDetails.RequisitionType == ITL_COC_ReqForSecRestrictn.RequisitionType
           && SecRestrictnDetails.SecurityCode    == ITL_COC_ReqForSecRestrictn.SecurityCode
      {
          if(SysUserInfo::find(SecRestrictnDetails.UserId).Email == '')
             throw Error(strfmt("Email Id for user id %1 was not found in the User Master. Please update the email id for %2",SecRestrictnDetails.UserId,SecRestrictnDetails.UserName));
          username  += SecRestrictnDetails.UserName + " , ";
          useremail += SysUserInfo::find(SecRestrictnDetails.UserId).Email + ",";
      }

      fromEmail  = sysuserinfo::find(curuserid()).Email;
      select userinfo where userinfo.id == curuserid();
      FromName   = userinfo.Name;

         text1 ="Particulars of Grey Listing :"
               + " "  + "\n Security Name             : " + " " + ITL_COC_ReqForSecRestrictn.SecurityName
               + " "  + "\n Security Type             : " + " " + enum2str(ITL_COC_ReqForSecRestrictn.SecurityType)
               + " "  + "\n Effective Date                 : " + " " + date2str(ITL_COC_ReqForSecRestrictn.FromDate,123,2,2,2,2,2)
               + " "  + "\n Name of Initiator            : " + " " + ITL_COC_ReqForSecRestrictn.Initiatorname
               + " "  + "\n Name of Access Persons   : " + " " + username      ;
          //     + " "  + "\n Grey List Notification No : " + " " + ITL_COC_ReqForSecRestrictn.RequisitionNo
           //    + " "  + "\n\n Please give the Grey List Declaration in Axapta.\n"
           //    + " "  + "\n For Grey list Declaration, please follow this path in Axapta : "
           //    + " "  + "\n Employee Self Operations-> Code Of Conduct-> Grey Listing-> Grey Listed Declaration."  ;

//                 dialogMail          = new Dialog("Mail");
        mailer              = new SysMailer();
        //mailer1             = new SysMailer();

        if (parameter.DNSServerName)
            mailer.DNSServers().add(parameter.DNSServerName,parameter.DNSTCPIPRetryCount,parameter.DNSUDPRetryCount);
        if(parameter.SMTPRelayServerName)
            mailer.SMTPRelayServers().add(parameter.SMTPRelayServerName,parameter.SMTPPortNumber,parameter.SMTPServerIPAddress,parameter.SMTPUserName,parameter.SMTPPassword);

        dialogMail.caption("Mail");

        dfTowhom     = dialogMail.addFieldValue(str2int(useremail),useremail,"To");
        dftextmail   = dialogMail.addFieldValue(str2int(text1), Text1,"Text");
        dftextmail.allowEdit(false);
        dialogMail.run();

        Towhome1 =  dfTowhom.value();
        textmail1 = dftextmail.value();

        if(dialogMail.closedOk())
        {
            while select SecRestrictnDetails
            where SecRestrictnDetails.RequisitionNo    == ITL_COC_ReqForSecRestrictn.RequisitionNo
               && SecRestrictnDetails.RequisitionType  == ITL_COC_ReqForSecRestrictn.RequisitionType
               && SecRestrictnDetails.SecurityCode     == ITL_COC_ReqForSecRestrictn.SecurityCode
               && SecRestrictnDetails.NotifyMailUser   == Noyes::No
               && SecRestrictnDetails.DenotifyMailUser == Noyes::No
            {
                if(SysUserInfo::find(SecRestrictnDetails.UserId).Email == '')
                    throw Error(strfmt("Email Id for user id %1 was not found in the User Master. Please update the email id for %2",SecRestrictnDetails.UserId,SecRestrictnDetails.UserName));
                accessemails = SysUserInfo::find(SecRestrictnDetails.UserId).Email;
                mailer.body(textmail1);
                mailer.fromName(FromName);
                mailer.fromAddress(fromEmail);
                mailer.tos().add(accessemails);
                mailer.subject("Intimation of Security placed under Grey List in Axapta");

                 mailer.sendMail();
                 accessemails='';
                 mailer = null;
                ttsbegin;
                select  ITL_COC_ReqForSecRestrictnDetails where ITL_COC_ReqForSecRestrictnDetails.RequisitionNo    == ITL_COC_ReqForSecRestrictn.RequisitionNo
                                                             && ITL_COC_ReqForSecRestrictnDetails.NotifyMailUser   == Noyes::No
                                                             && ITL_COC_ReqForSecRestrictnDetails.DenotifyMailUser == Noyes::No;
                if( ITL_COC_ReqForSecRestrictnDetails)
                {

                    ITL_COC_ReqForSecRestrictnDetails.NotifyMailUser= Noyes::Yes;
                    ITL_COC_ReqForSecRestrictnDetails.update();
                }
                ttscommit;

            }


            info('email sent');

            ttsbegin;
            ITL_COC_ReqForSecRestrictn.MailSentToAccessPerson = noyes::Yes;
            ITL_COC_ReqForSecRestrictn.update();

            ttscommit;
            MailToAccessPerson.enabled(false);
            mailer = null;
            //CO Mail
//            COEmail = ITLCOCParameter::find().COMailId;
//            mailer1.body(textmail1);
//            mailer1.fromName(FromName);
//            mailer1.fromAddress(fromEmail);
//           // mailer1.tos().add(COEmail);
//            mailer1.subject("Intimation for Declaration of security placed under grey List in Axapta");
//          // mailer1.sendMail();
//            info('email sent');
//            mailer1 = null;

        }
        else
            info('Activity Cancelled ');
   }
  ITL_COC_ReqForSecRestrictn_ds.active();
  ITL_COC_ReqForSecRestrictn_ds.refresh();

 

*This post is locked for comments

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

Congratulations 2024 Spotlight Honorees!

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December!

Congratulations to our December super stars! 🥳

Get Started Blogging in the Community

Hosted or syndicated blogging is available! ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,622 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,354 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans