This client library will allow you to quickly send email from a .NET Core application via the PostageApp API. Specify one or more recipients, your template along with variables to substitute, enjoy open and click tracking, and quick, reliable delivery.
To install PostageApp, run the following command in the Package Manager Console:
PM> Install-Package PostageApp
Visit postageapp.com/register and sign-up for an account. Create one or more projects in your account each project gets its own API key. Click through to the project page and find the API key in the right-hand column.
In general you will use .net core DI infrastructure.
Registration:
services.AddPostageAppClient(options => {
options.ApiKey = "Your Key";
});
Using:
class MyController
{
private readonly IPostageAppClient _postageAppClient;
MyController(IPostageAppClient postageAppClient)
{
_postageAppClient = postageAppClient;
}
public async Task<ActionResult> Action()
{
GetAccountInfoResult result = await _postageAppClient.GetAccountInfoAsync();
if (!result.Succeeded)
{
// TODO error
}
// TODO action!
return Ok();
}
}
The real power of PostageApp kicks in when you start using templates. Templates can be configured in your PostageApp project dashboard. They can inherit from other templates, contain both text and html representations, provide placeholders for variables, headers and more. Your app doesn't need to concern itself with rendering html emails and you can update your email content without re-deploying your app.
Once you have created a template that you want to use, specify its unique slug
in the Template property as in the example below.
await client.SendMessageAsync(new Message
{
Recipients = new MessageRecipient("Alan Smithee <alan.smithee@gmail.com>"),
Template = "YOUR_TEMPLATE_SLUG",
Variables = new Dictionary<string, string>()
{
{"first_name", "Alan"},
{"last_name", "Smithee"},
{"order_id", "555"}
}
});
Emails aren't restricted to just one recipient. Set the Recipients
property
to a list of MessageRecipient
objects, each with its own set of variables.
var message = new Message();
message.Recipients = new []
{
new MessageRecipient("Alan Smithee <alan.smithee@gmail.com>", new Dictionary<string, string>()
{
{"first_name", "Alan"},
{"last_name", "Smithee"},
{"order_id", "555"}
}),
new MessageRecipient("Rick James <rick.james@gmail.com>", new Dictionary<string, string>()
{
{"first_name", "Rick"},
{"last_name", "James"},
{"order_id", "556"}
}),
}
In addition to attaching files to templates in the PostageApp project dashboard, they can be attached by your app at runtime.
Simply add an MessageAttachment
to the Attachments
collection, providing a File bytes, Filename and ContentType for each file attached.
message.Attachments.Add("invoice.pdf", new MessageAttachment(fileBytes, "application/pdf"));
The From
, Subject
and ReplyTo
properties are shortcuts for the following syntax.
message.Headers.Add("from", "Acme Widgets <widgets@acme.com>");
message.Headers.Add("subject", "Your order has shipped!");
message.Headers.Add("reply-to", "Acme Support <support@acme.com>");
You are free to add any necessary email headers using this method.