Send Email¶
This How-To will show you how to create and send emails.
Steps¶
- Create a new Busines Object, named UtilitiesBO
- Create a new class named EmailHelper
- Make the class unpersisted
(Select the class, at Properties grid on the right make sure the Persisted checkbox is not checked) - Create one more class named Email (This class we want it to be Persisted)
-
Insert the following attributes:
-
Select class EmailHelper
- Go to tab Operations (bottom of the screen)
-
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; }
-
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; }
-
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. -
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; }
-
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; }
-
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 -
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