Monday, 12 October 2015

Configure email notification in MSD AX-2012


Set up SMTP mail server

The very first step you need to do is to set up a Simple Mail Transfer protocol (SMTP) in your environment. Just you need to follow the following procedure.
  •  Install the SMTP ServerInstall the SMTP server on a computer that is running Window Server. The following steps are applicable if you are using Windows Server 2012.
o   Go to Server Manager.
o   Click the Manage menu and then click Add Roles and Features.
o   New window will open, Click Next in Before you begin page.
o   The Select installation type Select the Role-base or feature-based installation option and then, click next.
o   The Select destination server page is displayed. Select the server on which you want to install the SMTP sever. Click Next.
o   The Select server role page is displayed. Click Next.
o   The Select Features window is displayed. Click the SMTP Server check box.
o   A window is displayed that asks whether you want to add role services and features for the SMTP server. Click Add Features.
o   The Select Features page is redisplayed. Click Next.
o   The Web Server (IIS) page is displayed. Click Next.
o   The Select Role Services page is displayed. Click Next.
o   The Confirm Installation Services page is displayed. Click Install.

  •   Configure the SMTP server
           Use following steps to configure the SMTP server to use integrated Windows authentication.

o   Go to Administrative Tools.
o   Click Internet Information Services (IIS) 6.0 Manager to open IIS Manager.
o   In the left pane, select the name of your server.

o   In the right pane, right-click [SMTP Virtual Server #1] and then click Properties.

o   The [SMTP Virtual Server #1] Properties window is displayed. Click the Access tab.
o   Click the Authentication button.
o   The Authentication window is displayed. Select the Integrated Windows Authentication check box. Click OK.
o   The Access tab is redisplayed. Click the Relay button.
o   The Relay Restrictions window is displayed. Select the Allow all computers which successfully authenticate to relay, regardless of the list above check box. Click OK.
o   The [SMTP Virtual Server #1] Properties window is redisplayed. Click OK to close the window.


  • Go to Services. 
    You have to change some properties of SMTP services.
o   Go to services in start menu, find Simple Mail Transfer Protocol (SMTP).
o   Right click on Simple Mail Transfer Protocol (SMTP), new window will open.Now go to Log On        tab check Local System account and Allow services to interact with desktop.



  • Create a unique port (Optional)
    Now you need to create a specific port that you can use for email.

Note: The default port is 25 for mail configuration, if the default port is not working then try to create different port by using following steps.
o   Search firewall with Advanced Security in start and click.
o   Right click on Inbound Rules, select Create new Rule.
o   New window will open select Port, click next.

o   Give an specific port number e.g. 587 and click next.
  

o   For Action step check, Allow the connection and click Next.


o   Click next for Profile step, and in Name step give an unique name to port.


  • Connect Microsoft Dynamics AX to the SMTP mail server
    Use the following procedure to connect Microsoft Dynamics Ax to the SMTP mail server.

o   Open the Microsoft Dynamics AX client.
o   Click System administration > Setup > System > E-mail parameters.
o   In the Outgoing mail server field, enter the name of the computer on which you installed the SMTP server.
o   In the Local computer name field, enter the name of the local computer.
o   In the SMTP port number field, enter the port number to use with SMTP. The default port number is 25.
o   If the SMTP server requires authentication, enter a valid username and password.
o   Select the Use NTLM check box.
o   In the Attachment size limit (MB) field, enter a size limit for email attachments.
o   The max buffer size registry key also limits the acceptable size for email attachments. If the size that you enter in this field exceeds the maximum buffer size, the maximum buffer size limit will apply.
o   In the Allow embedded data and attached files from field, enter or browse to the location where email attachments or other embedded data in an email is stored.

Use batch processing to send email messages

  •  Create a batch group
          A batch job, which is named E-mail Distributor Batch Job, is used to send email messages that are generated from Microsoft Dynamics AX. The batch job must run within the context of a batch group. Complete the following steps to create a batch group for the E-mail Distributor Batch Job.
o   Click System administration > Setup > Batch group.
o   Click New to create a new batch group.
o   In the Group field, enter a unique name for the batch group. For example, enter Email.
o   In the Description field, enter a description to help you identify the batch group.
o   Click the Batch servers tab.
o   The Selected servers list displays the AOS instances that the batch group runs on. The Remaining servers list displays the remaining AOS instances that are available as batch servers.
o   Use the arrow buttons to add servers to the Selected Servers list or to remove servers from the Selected Servers list.

  • Configure the batch jobComplete the following steps to configure the E-mail Distributor Batch Job.
o   Click System administration > Periodic > E-mail processing > Batch.
o   Select the Batch processing check box.
o   In the Task description field, enter a description for this batch job.
o   In the Batch group list, select the batch group that you created in the previous procedure.
o   Select the Private check box if you want to restrict other users from running this batch job. A private batch job can be run only by the user who specified it and only on the computer where the user is logged on.
o   Click Recurrence to specify how often this batch job will run.
o   Click Alerts to send notifications when this batch job ends, has an error, or is canceled.

  •  Create a new job in AX to test email (Use the following code)
static void EmailTestJob(Args _args)
{

    SysEmailParameters parameters = SysEmailParameters::find();
    SMTPRelayServerName relayServer;
    SMTPPortNumber portNumber;
    SMTPUserName userName;
    SMTPPassword password;
    Str1260 subject,body;
    InteropPermission interopPermission;
    SysMailer mailer;
    System.Exception e;
    UserGroupList   _UserGroupList;
    SysUserInfo     _sysuser;

    container  con;
    int i=0;
    str reciever;
    Str 30 _emailAlert;//SysEmailParameters_MailAlert

    if (parameters.SMTPRelayServerName)
    {
        relayServer = parameters.SMTPRelayServerName;
    }
    else
    {
        relayServer = parameters.SMTPServerIPAddress;
        portNumber = parameters.SMTPPortNumber;
        userName = parameters.SMTPUserName;
        password = SysEmailParameters::password();

        subject = "Subject line for the email";
        body = "Body of the email";

        CodeAccessPermission::revertAssert();
    }
        
    try
    {
        interopPermission = new InteropPermission(InteropKind::ComInterop);
        interopPermission.assert();
        mailer = new SysMailer();
        mailer.SMTPRelayServer(relayServer,portNumber,userName,password, parameters.NTLM);
        //instantiate email
        mailer.fromAddress(userName);
        MAILER.TOS().appendAddress("User@DomainName.com");
        MAILER.SUBJECT(SUBJECT);
        MAILER.HTMLBODY(BODY);
        MAILER.SENDMAIL();
        CODEACCESSPERMISSION::REVERTASSERT();
        MAILER.TOS().CLEAR();
        info("Email has been sent");         
    }
    catch   (Exception::CLRError)
    {
        e = ClrInterop::getLastException();
        while (e)
        {  
            info(e.get_Message());
            e = e.get_InnerException();
        }
    }

    CodeAccessPermission::revertAssert();
    info ("Failed to Send Email some Error occur");
    
}