Monday, December 23, 2013

Emailing in DAX using X++ code

static void TestEmail(Args _args)
{
    // Set these variables.
    str                                   sender= “anil.kumar@gmail.com”
    str                                   recipient = ‘anil.kumar@gmail.com’;
    str                                   cc1 = ‘anil.kumar@gmail.com’;
    str                                   subject = “DemoMail”;
    str                                   body = “Happy mailing”;
    str                                   fileName1=@’c:\\anil.pdf’;
    SysEmailMessageTable                  sysEmailMessageTable;
    SysEmailTable                         sysEmailtable;
    VendTable                             vendtable;
    LedgerJournalTrans                    ledgerJournalTrans;
    Set                                   permissionSet;
    System.Exception                      e;

    str                                   mailServer;
    System.Net.Mail.SmtpClient            mailClient;
    System.Net.Mail.MailMessage           mailMessage;
    System.Net.Mail.MailAddress           mailFrom;
    System.Net.Mail.MailAddress           mailTo;
    System.Net.Mail.MailAddressCollection mailCCCollection;
    System.Net.Mail.AttachmentCollection  mailAttachementCollection;
    System.Net.Mail.Attachment            mailAttachment;
;

try
{
    permissionSet = new Set(Types::Class);
    permissionSet.add(new InteropPermission(InteropKind::ClrInterop));
    permissionSet.add(new FileIOPermission(filename1, ‘rw’));
    CodeAccessPermission::assertMultiple(permissionSet);

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

    mailFrom = new System.Net.Mail.MailAddress(sender);
    mailTo  = new System.Net.Mail.MailAddress(recipient);
    mailMessage = new System.Net.Mail.MailMessage(mailFrom, mailTo);
    mailCCCollection = mailMessage.get_CC();
    mailCCCollection.Add(cc1);
    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(fileName1);
    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();
}
}

No comments:

Post a Comment