Sending E-mails using API

   —   

This article will provide you with short example of sending e-mails using the code.

You can send an email programmatically through our API in the following way:

CMS.EmailEngine.EmailMessage em = new CMS.EmailEngine.EmailMessage(); em.EmailFormat = CMS.EmailEngine.EmailFormatEnum.Html; em.From = "from@mail.com"; em.Recipients = "to@mail.com"; em.Subject = "Subject"; em.Body = "body of email"; CMS.EmailEngine.EmailSender.SendEmail("site_name", em);


Please make sure you have SMTP server specified in site settings for e-mails being sent properly. There are two basic variations of the SendEmail method:

CMS.EmailEngine.EmailSender.SendEmail(em);


You can also send e-mails with e-mail template:

EmailMessage msg = new CMS.EmailEngine.EmailMessage(); EmailTemplateInfo eti = EmailTemplateProvider.GetEmailTemplate("EmailTemplateCodeName", CMSContext.CurrentSiteID); MacroResolver mcr = new MacroResolver(); mcr.SpecialMacros = new String[,] { { "#macro#", "text" } }; //here you can specify text for multiple macros specified in template msg.EmailFormat = EmailFormatEnum.Both; msg.From = eti.TemplateFrom; //make sure this is specified in the template settings msg.Recipients = "name@domain.com"; msg.Subject = eti.TemplateSubject; EmailSender.SendEmailWithTemplateText(CMSContext.CurrentSiteName, msg, eti, mcr, true); //if send immeditaley is true, e-mail queue is not used


The first one sends e-mail using global settings, whilest the second one sends e-mail using settings for specified site. If you use the first option and it doesn't work for you, it can be caused by SMTP settings  specified only for appropriate site - not globally.

 

 

Share this article on   LinkedIn

Juraj Ondrus

Hi, I am the Technical support leader at Kentico. I'm here to help you use Kentico and get as much as possible out of it.