Skip to content

Sending Email

Vlad-Cosmin Sandu edited this page Apr 26, 2021 · 11 revisions

The Postmark API allows sending and tracking emails with as little as a To, From, Subject, and Body. The API is powerful, and supports the inclusion of Custom Headers, HTML bodies, attachments, and automatic open & read tracking. Let's cover some common uses:

Sending a Simple Email:

public async Task SendTestMessage(){
	
	var client = new PostmarkClient("<server token>");

	try{
		var sendResult = await client.SendMessageAsync("<sender signature>", 
			"ben@example.com", 
			"Hello from Postmark!",
			"This is just a friendly hello from your friends at Postmark.");

		// If there's a problem with the content of your message,
		// the API will still return, but with an error status code, 
		// you should take appropriate steps to resolve/retry if this 
		// happens.
		if (sendResult.Status == PostmarkStatus.Success){ /* Handle success */ }
    	else { /* Resolve issue.*/ }

	}catch{
		// Calls to the client can throw an exception 
		// if the request to the API times out.
	}
}

Note: By default, the message will be sent via the default transactional Message Stream ("outbound").

Sending Complex Emails:

If you'd like to send more sophisticated emails, you can use the PostmarkMessage class:

var message = new PostmarkMessage
{
    To = "recipient@example.com",
    Cc = "cc@example.com",
    From = "sender@example.com",
    TrackOpens = true,
    Subject = "A complex email",
    TextBody = "Plain Text Body",
    HtmlBody = "<html><body><img src=\"cid:embed_name.jpg\"/></body></html>",
    Tag = "New Year's Email Campaign",
    Headers = new HeaderCollection(new Dictionary<string, string> { { "X-CUSTOM-HEADER", "Header content" } }),
    MessageStream = "outbound" // here you can set your custom Message Stream
};

var imageContent = File.ReadAllBytes("test.jpg");
message.AddAttachment(imageContent, "test.jpg", "image/jpg", "embed_name.jpg");

var client = new PostmarkClient("<server token>");
var sendResult = await client.SendMessageAsync(message);

Take special note that you can enable and disable message tracking (only messages with an HTML body can be tracked). You may also set custom headers, as well include attachments (that can be referenced by an HTML body).

Send a Batch of Emails:

You may send an Array or IEnumerable of PostmarkMessage 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 the /batch endpoint 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 some messages to send 
// (An array of PostmarkMessage will work)
var messages = CreateBatchOfEmails();

// 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