Skip to content

Send Emails with Templates API

The API for sending email for a template relies on a special entity called EmailEnvelope. This entity contains information about the email recipients. Specifically:

  • a list of email addresses for the "To", "Cc" and "Bcc" fields of the email message
  • a query, also known as a where clause, against the Users database table, which identifies the recipients of the email. The query takes precedence and overrides the email addresses for the "To" field.

The EmailEnvelope class is defined as:

namespace BackendlessAPI.Messaging
{
  public class EmailEnvelope
  {
    public List<string> Cc{ get; set; }
    public List<string> Bcc;
    public List<string> To;
    public string RecipientsQuery;

    // adds addresses specified in the method argument to the existing
    // collection of the "To" addresses
    public EmailEnvelope AddTo( IEnumerable<string> toAddresses )

    // replaces all existing "To" addresses in the envelope
    // with the ones provided in the method argument
    public EmailEnvelope SetTo( List<string> toAddresses )

    // adds addresses specified in the method argument to the existing
    // collection of the "Cc" addresses
    public EmailEnvelope AddCc( IEnumerable<string> ccAddresses )

    // replaces all existing "Cc" addresses in the envelope
    // with the ones provided in the method argument    
    public EmailEnvelope SetCc( List<string> ccAddresses )

    // adds addresses specified in the method argument to the existing
    // collection of the "Bcc" addresses
    public EmailEnvelope AddBcc( IEnumerable<string> bccAddresses )

    // replaces all existing "Bcc" addresses in the envelope
    // with the ones provided in the method argument        
    public EmailEnvelope SetBcc( List<string> bccAddresses )
  }
}

where:

Argument                Description
To A collection of email addresses to deliver an email generated from the template to. These email addresses are ignored if and when the RecipientsQuery parameter is present.
Cc A collection of email addresses to include into the CC (carbon copy) distribution list of the email message generated from the template.
Bcc A collection of email addresses to include into the BCC (blind carbon copy) distribution list of the email message generated from the template.
RecipientsQuery A where clause for the Users table which defined the condition for selecting the users who will be receiving an email message generated from the template. The resulting collection of users takes precedence of the the email addresses (if any are) provided through the To property.

Non-blocking methods

public async Task<MessageStatus> Backendless.Messaging.SendEmailFromTemplateAsync( 
                                                               string templateName, 
                                                               EmailEnvelope envelope )
public async Task<MessageStatus> Backendless.Messaging.SendEmailFromTemplateAsync( 
                                                               string templateName, 
                                                               EmailEnvelope envelope, 
                                                               Dictionary<string, string> templateValues )

public void Backendless.Messaging.SendEmailFromTemplate( 
                                         string templateName, 
                                         EmailEnvelope envelope, 
                                         AsyncCallback<MessageStatus> responder )

public void Backendless.Messaging.SendEmailFromTemplate( 
                                         string templateName, 
                                         EmailEnvelope envelope, 
                                         Dictionary<string, string> templateValues, 
                                         AsyncCallback<MessageStatus> responder )

Blocking methods

public MessageStatus Backendless.Messaging.SendEmailFromTemplate( string templateName, 
                                                                  EmailEnvelope envelope );

public MessageStatus Backendless.Messaging.SendEmailFromTemplate( string templateName, 
                                                                  EmailEnvelope envelope, 
                                                                  Dictionary<string, string> templateValues )

where:

Argument                Description
templateName Name of an email template created in Backendless Console.
envelope An instance of the EmailEnvelope class containing information about the email recipients.
templateValues A dictionary object containing values which will be used Smart and Dynamic text substitutions. The key names in the object are matched against the Smart/Dynamic text placeholder names. The corresponding values are used for substitution in the resulting email message.

Example

The example below sends out an email based on a template called "Marketing Template" to all users registered in the app whose email address does not end with @gmail.com and the related country is USA:

EmailEnvelope envelope = new EmailEnvelope();
envelope.RecipientsQuery = @"email not like '%@gmail.com' and address.country = 'USA'";

var messageStatus = await Backendless.Messaging.SendEmailFromTemplateAsync( "Marketing Template", envelope );

The example below sends out an email based on a template called "Marketing Template" to the users specified in the "to" field of EmailEnvelope. The example demonstrates the usage of the "templateValues" field. It contains two substitution values for the "Users.address.country" and the "discount" placeholders:

List<string> addresses = new List<string>();
addresses.Add("joe@yahoo.com");
addresses.Add("bob@hotmail.com");

Dictionary<string, string> templateValues = new Dictionary<string,string>();
templateValues[ "Users.address.country" ] = "your country";
templateValues[ "discount" ] = "20% off";

EmailEnvelope envelope = new EmailEnvelope();
envelope.SetTo( addresses );

var messageStatus = await Backendless.Messaging.SendEmailFromTemplateAsync( "Marketing Template", envelope, templateValues );