Sending Email from AX

Sending Email from AX

static void EmailAlert(Args _args)
{
  // Set these variables.
    str                                   sender = 'Ax Mail Id';//From the parameter form
    str                                   recipient = 'Mail Id's';
  //str                                   cc = 'Mail Id's';
    str                                   subject = 'Test1';
    str                                   body = 'Test1';
  //str                                   fileName = @'C:\test.txt';

    List                                  toList;
    List                                  ccList;
    ListEnumerator                        le;

    Set                                   permissionSet;
    System.Exception                      e;

    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;
    ;

    try
    {
        toList = strSplit(recipient, ';');
        //ccList = strSplit(cc, ';');

        permissionSet = new Set(Types::Class);
        permissionSet.add(new InteropPermission(InteropKind::ClrInterop));
       // permissionSet.add(new FileIOPermission(filename, 'rw'));
        CodeAccessPermission::assertMultiple(permissionSet);

        mailServer = SysEmaiLParameters::find(false).SMTPRelayServerName;
        mailServerPort = SysEmaiLParameters::find(false).SMTPPortNumber;
        mailClient = new System.Net.Mail.SmtpClient(mailServer, mailServerPort);

        le = toList.getEnumerator();
        le.moveNext();

        mailFrom = new System.Net.Mail.MailAddress(sender);
        mailTo  = new System.Net.Mail.MailAddress(strLTrim(strRTrim(le.current())));
        mailMessage = new System.Net.Mail.MailMessage(mailFrom, mailTo);

        mailToCollection = mailMessage.get_To();
        while (le.moveNext())
        {
            mailToCollection.Add(strLTrim(strRTrim(le.current())));
        }

     //   le = ccList.getEnumerator();
     //   mailCCCollection = mailMessage.get_CC();
     //   while (le.moveNext())
     //   {
     //       mailCCCollection.Add(strLTrim(strRTrim(le.current())));
     //   }

        mailMessage.set_Priority(System.Net.Mail.MailPriority::High);
        mailMessage.set_Subject(subject);
        mailMessage.set_Body(body);

        //mailAttachementCollection = mailMessage.get_Attachments();
       // mailAttachment = new System.Net.Mail.Attachment(fileName);
       // mailAttachementCollection.Add(mailAttachment);

        mailClient.Send(mailMessage);
        mailMessage.Dispose();

        CodeAccessPermission::revertAssert();

        info("Email sent.");
    }
    catch (Exception::CLRError)
    {
        e = ClrInterop::getLastException();
        while (e)
        {
            info(e.get_Message());
            e = e.get_InnerException();
        }
        CodeAccessPermission::revertAssert();
    }
}
Advertisements

Sending Email from AX

Below code snippet is used to send mail from AX to the desired user:

public void SendEmailNotificationTest()
{
    SysMailer mail;
    InteropPermission permission = new InteropPermission(InteropKind::ComInterop);
    str fromMail,body,sub;
    str 100 tomail;
    ;

    permission.assert();

    mail = new SysMailer();

    fromMail = "MailerId@DomainName.com"; //Take the mail Id from the Email Parameters  
                                          // in System admin\setup\system\Email Parameters
    tomail = "Receiver mail ID";
    sub="Service Warrenty PO Alert ";

    body = "Test Email body";
  
    mail.quickSend(fromMail,tomail,sub,body);
    
    info(strFmt("done!"));
}

Export data to XML from AX Tables

Below code snippet is to export the data in tables to XML format:

class ExportToXML
{
}
   public static void main(Args _args)
{
    XmlDocument doc;
    XmlElement nodeXml;
    XmlElement nodeTable;
    XmlElement nodeAccountId;
    XmlElement nodeName;
    Table6 tab;

    #define.filename(@'C:\Temp\TestXML.xml')

    doc = XmlDocument::newBlank();
    nodeXml = doc.createElement('xml');
    doc.appendChild(nodeXml);

    while select RecId, Name,Id from tab
    {

        nodeTable = doc.createElement(tableStr(Table6));
        nodeTable.setAttribute(fieldStr(Table6, RecId),int642str(tab.RecId));
        nodeXml.appendChild(nodeTable);

        nodeAccountId = doc.createElement(fieldStr(Table6, Id));
        nodeAccountId.appendChild(doc.createTextNode(tab.Id));
        nodeTable.appendChild(nodeAccountId);

        nodeName = doc.createElement(fieldStr(Table6, Name));
        nodeName.appendChild(doc.createTextNode(tab.Name));
        nodeTable.appendChild(nodeName);
    }

    doc.save(#filename);
    info(strFmt("File %1 created.", #filename));
}

Read \ Import data from XML

Below code snippet is to Read \ Import data from XML File to AX

class ImportDataFromXmlFile
{
}

public static void main(Args _args)
{
    XmlDocument doc;
    XmlNodeList data;
    XmlElement nodeTable;
    XmlElement nodeAccount;
    XmlElement nodeName;
    Table6 tab;
    #define.filename(@'C:\Temp\TestXML.xml')
    doc = XmlDocument::newFile(#filename);
    data = doc.selectNodes('//'+tableStr(Table6));
    nodeTable = data.nextNode();
    

    while (nodeTable)
        {
            nodeAccount = nodeTable.selectSingleNode(fieldStr(Table6, Id));
            nodeName = nodeTable.selectSingleNode(fieldStr(Table6, Name));
            
            tab.Id = nodeAccount.text();
            tab.Name = nodeName.text();
            tab.insert();
            
            info(strFmt("%1 - %2",nodeAccount.text(),nodeName.text()));

            nodeTable = data.nextNode();
        }
}