Skip to content

Send Email

This How-To will show you how to create and send emails.

Steps

  1. Create a new Busines Object, named UtilitiesBO
  2. Create a new class named EmailHelper
  3. Make the class unpersisted
    (Select the class, at Properties grid on the right make sure the Persisted checkbox is not checked)
  4. Create one more class named Email (This class we want it to be Persisted)
  5. Insert the following attributes:

    Application User Class

  6. Select class EmailHelper

  7. Go to tab Operations (bottom of the screen)
  8. Create a new operation named CreateEmail

    • Create a static function CreateEmail


    Insert the following code:

        static function Domain.Email CreateEmail(
            string subject, 
            string body,
            Collection[string] to,
            Collection[string] cc   
            ) 
        {
            Domain.Email newMail;
            newMail.CreatedDate = DateTime.Now();
    
            if (cc != null && cc.Length > 0)
            {
                newMail.CC = string.Join(",", cc.ToArray());
            }
    
            if (to != null && to.Length > 0)
            {
                newMail.To = string.Join(",", to.ToArray());
            }
    
            newMail.Subject = subject;
            newMail.Body = body;
            newMail.IsSent = true;
            newMail.SentDate = DateTime.Now();
            newMail.Save();
    
            return newMail;
        }
    
  9. Create a new operation named SendEmail

    • Create a static boolean function SendEmail


    Insert the following code:

        static function bool SendEmail(Domain.Email email, Collection[CommonLib.EmailAttachment] attachments) 
        {
            var to = email.To.Split(',').ToCollection();
    
            if (to.Length <= 0)
            {
                return false;
            }
    
            CommonLib.EmailMessage mail;
            mail.IsBodyHtml = true;
            mail.Subject = email.Subject;
            mail.Body = email.Body;
            mail.To = to;
    
            if attachments != null
            {
                mail.Attachments = attachments; 
            }
    
    
            if email.CC != null
            {
                mail.CC = email.CC.Split(',').ToCollection();
            }
    
            mail.From = "";
    
            try 
            {
                CommonLib.Utilities.SendEmail(mail);
            }
    
            catch Exception x 
            {
                DebugLib.Logger.WriteWarnLine(x);
                return false;
            }
    
            return true;
        }
    
  10. Create a new empty html file at your computer and upload it at Resources.
    At explorer on the left, right click at resources, click add, select your file and click Upload files.
    This will be the body of the email. You can design the file as you wish, using html.

  11. Create a new operation named RenderTemplate

    • Create a static function RenderTemplate


    Insert the following code:

        static function string RenderTemplate(
            string template, Dictionary[string, string] inputs
            ) 
        {
            var filePath = CommonLib.Utilities.GetServerPhysicalPath("~/Resources/" + template);
            var contents = FileIOLib.File.ReadAllFrom(filePath);
    
            foreach key in inputs.GetKeys() 
            {
                contents = contents.Replace("{" + key + "}", inputs.Get(key));
            }
    
            return contents;
        }
    
  12. Create a new operation named CreateAndSend

    • Create a static function CreateAndSend


    Insert the following code:

        static function Domain.Email CreateAndSend(
            string subject, 
            string body,
            Collection[string] to,
            Collection[string] cc,
            Collection[CommonLib.EmailAttachment] attachments
            ) 
        {
            var mail = Domain.EmailHelper.CreateEmail(subject, body, to, cc);
            Domain.EmailHelper.SendEmail(mail, attachments);
    
            return mail;
        }
    
  13. In order to send the email, use the following code in a Domain Model

        function void Email()
        {
            var body = Domain.EmailHelper.RenderTemplate("templateName", null);
    
            Domain.EmailHelper.CreateAndSend(
                "in here type your subject", 
                body, 
                {"testTo@mail.com"}, 
                {"testCc@mail.com"}, 
                null
            );
    
        }
    

    Info

    You can write this code:
    - In a domain model (best practice)
    - In a workflow
    - In a form

    Example

    Go to the form you wish
    Create a new (or select an existing) controller action
    Write the forementioned code

    Application User Class

  14. From Explorer on the left:
    Go to Configuration -> app -> Apllication -> Settings

At System Defined panel, type the SMTP settings for these:

  • Email Host Address
  • Email Password
  • Email Username
  • Email From Account
  • Email Port
Back to top