Skip to content

Sending Using a Template

Vlad-Cosmin Sandu edited this page Nov 4, 2020 · 4 revisions

Postmark Templates Overview

Before trying to send email using Postmark Templates from .net, we recommend reviewing how the Postmark Templates feature works.

Sending Using a Template

  1. Create a Template. This can be done via the API, or using our editor from a server dashboard at https://postmarkapp.com. Each template created is assigned an Id, this will be used when sending a templated email:

  2. Create a message object, assigning a TemplateModel

var message = new TemplatedPostmarkMessage();

// Set normal message properties:
message.From = "sender@example.com";
message.To = "recipient@example.com";

// Set which template should be used for this email.
message.TemplateId = <Template Id from Step 1>;

// These are the values that will be merged with your template
// You can use a POCO, or Dictionary<Key,Value> here.
// You **cannot** use scalar values or collections for TemplateModel 
// (though they can be used _inside_ of a model.)
message.TemplateModel = new {
    Name = "Johnny"
    Company = "Wildbit"
}
  1. Finally, send the email:
var client = new PostmarkClient("<server token>");
await client.SendMessageAsync(message);

Batch Sending templated messages

You may send an Array or IEnumerable of TemplatedPostmarkMessage objects in one call to the API. This is more efficient than sending emails individually, but consider bandwidth and message payload limits to determine how many messages to send at once.

Please note that batch endpoints will return a 200-level Http Status, even when validation for individual messages may fail. Users of these endpoints should check the error code for each message in the response from our API (the results are ordered the same as the original messages).

var client = new PostmarkClient("<server token>");

// Generate an array of TemplatedPostmarkMessage objects.
var messages = CreateBatchOfTemplatedEmails();

// Send the messages.
var results = await client.SendMessagesAsync(messages);

// Iterate over each result to confirm the message was sent successfully.
foreach(var result in results){
	if(result.ErrorCode != 0){
		//Recover from issue.
	}
}
Clone this wiki locally