Home

Tuesday, February 21, 2017

AX 2009 AX 2012 Send EMail through SQL AX2009 AX2012

Below Job will send email through SQL, Before that configure your Database email (refer this link : https://msdn.microsoft.com/en-us/library/hh245116.aspx)

Note:- create an Action menu item and call the Job to send email successfully. You can change @body_format parameter to 'HTML' if your body design is HTML



public server static void sendSQLEMail(Args _args)
{
    SqlSystem   sqlSystem = new SqlSystem();
    Connection  connection = new Connection();
    Statement   statement = connection.createStatement();
    SqlStatementExecutePermission sqlStatementExecutePermission;
    Container   conVal;
    str         sendSQLMail;
    int         i;
    str         smtpServer;
    str         userMailAddress;
    str         Body;
    str         mailSubject;
    ;

    Body = "Hi from ax";
    mailSubject = strfmt("AX SQL Email trigger");
    sendSQLMail = "EXEC msdb.dbo.sp_send_dbmail ";
    sendSQLMail += "@recipients = 'uremailid@domain.com',";   
    sendSQLMail += "@body = '" + Body + "',";
    sendSQLMail += "@subject = '" + mailSubject + "',";
    sendSQLMail += "@body_format = 'TEXT'";
    sendSQLMail = strfmt(sendSQLMail, sqlSystem.loginDatabase());

    try
    {
        sqlStatementExecutePermission = new SqlStatementExecutePermission(sendSQLMail);
        sqlStatementExecutePermission.assert();

        // BP Deviation Documented
        statement.executeUpdate(sendSQLMail);
        CodeAccessPermission::revertAssert();
        info ("Mail sent successfully");
    }

    catch (Exception::Error)
    {
        exceptionTextFallThrough();
    }

}




No comments:

Post a Comment