Search This Blog

Wednesday, October 3, 2018

X++ code to Sending email using code and data with table format


static void SendEmail(Args _args)
{
    SysEmailParameters parameters = SysEmailParameters::find();
    SMTPRelayServerName relayServer;
    SMTPPortNumber portNumber;
    SMTPUserName userName;
    SMTPPassword password;
    Str subject,body;
    InteropPermission interopPermission;
    SysMailer mailer;
    System.Exception e;
    str s;
    CCHTMLString            htmlString;
    ;
    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 = "<B>Body of the email</B>";

    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("xx@xxxxxx");

    mailer.tos().appendAddress("xxxx@xxxx");
    mailer.subject(subject);
   // mailer.htmlBody(body);
    s = '<table border="1"> <tr> <td>Sl.No  </td> <td>Item number   </td> </tr>';
     htmlstring = strfmt(s);
    
     s = '<tr> <td>%1  </td><td>%2  </td></tr>';
     htmlstring += strfmt(s,1,'test12');
      htmlstring += strfmt( '</table>');
     mailer.htmlBody(htmlstring);
    mailer.sendMail();
    CodeAccessPermission::revertAssert();
    info("Email has been send!");
    }
    catch (Exception::CLRError)

    {
    e = ClrInterop::getLastException();

    while (e)

    {
        info(e.get_Message());

        e = e.get_InnerException();
    }
    CodeAccessPermission::revertAssert();
    //info(e);
    info ("Failed to Send Email some Error occure");
    }

}


Happy Daxing..

No comments:

Post a Comment