Skip to main content

Email Notifications

The Email Notifications API allows you to send emails to your users programmatically. You can send both custom-formatted emails and template-based emails using SendGrid templates.

Supported Features

  • Send custom emails with HTML and plain text content
  • Include file attachments
  • Use SendGrid templates with dynamic data
  • Customize sender name and email address
  • Track delivery through response codes

Prerequisites

To use the Email Notifications API, you need:

  1. A valid API subscription key
  2. Authorization to access the notification endpoints
  3. Valid sender email addresses configured in your account

Sending a Standard Email

Endpoint

POST /EmailNotification/SendNotification

Request Headers

HeaderDescription
correlationidUnique identifier for tracking the request
x-ls-party-idParty identifier for the sender
Ocp-Apim-Subscription-KeyYour API subscription key

Request Body

{
"subject": "Your Account Update",
"plainTextContent": "Your account has been updated successfully.",
"htmlContent": "<h1>Account Update</h1><p>Your account has been <strong>updated</strong> successfully.</p>",
"to": "recipient@example.com",
"recipientName": "John Doe",
"from": "noreply@yourcompany.com",
"fromName": "Your Company",
"attachments": [
{
"content": "base64encodedcontent",
"type": "application/pdf",
"filename": "receipt.pdf",
"disposition": "attachment",
"contentId": "receipt-123"
}
]
}

Using SendGrid Templates

SendGrid templates allow you to create reusable email templates with dynamic content placeholders.

Endpoint

POST /EmailNotification/SendSendGridEmailTemplate

Request Headers

Same as standard email endpoint.

Request Body

{
"from": "noreply@yourcompany.com",
"fromName": "Your Company",
"recipients": [
"recipient@example.com"
],
"templateId": "d-f3ecde774f944a9e8a7a57848aecc4b7",
"templateData": {
"user_name": "John",
"reset_link": "https://example.com/reset/abc123",
"company_name": "Your Company"
}
}

Implementation Examples

using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

public class EmailService
{
private readonly HttpClient _client;
private readonly string _apiKey;
private readonly string _baseUrl = "https://apis.lightstone.co.za/lightstone-capabilities-notifications-api/v1";

public EmailService(string apiKey)
{
_apiKey = apiKey;
_client = new HttpClient();
_client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", _apiKey);
}

public async Task<bool> SendEmailAsync(string to, string subject, string htmlContent)
{
var endpoint = $"{_baseUrl}/EmailNotification/SendNotification";

var request = new
{
subject = subject,
htmlContent = htmlContent,
plainTextContent = StripHtml(htmlContent),
to = to,
from = "noreply@yourcompany.com",
fromName = "Your Company"
};

var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json");

_client.DefaultRequestHeaders.Add("correlationid", Guid.NewGuid().ToString());

var response = await _client.PostAsync(endpoint, content);
return response.IsSuccessStatusCode;
}

private string StripHtml(string html)
{
// Simple HTML stripping implementation
return System.Text.RegularExpressions.Regex.Replace(html, "<[^>]*>", "");
}
}

Working with Attachments

To include attachments in your email, encode the file content in base64 and add it to the attachments array:

"attachments": [
{
"content": "JVBERi0xLjMNJeLjz9MNCjMgMCBvYmoNPDwgL0ZpbHRlciAvRmxhdGVEZWNvZGUg...",
"type": "application/pdf",
"filename": "report.pdf",
"disposition": "attachment",
"contentId": "report-id-123"
}
]

Best Practices

  1. Always include both HTML and plain text versions of your email
  2. Keep email size under 10MB including attachments
  3. Use SendGrid templates for consistent branding and easier management
  4. Test your emails across different clients and devices
  5. Include appropriate unsubscribe links in marketing emails
  6. Store the correlation ID for troubleshooting

Error Handling

The API returns standard HTTP status codes. Common errors include:

  • 400 Bad Request: Invalid email format or missing required fields
  • 401 Unauthorized: Invalid API key
  • 429 Too Many Requests: Rate limit exceeded
  • 500 Internal Server Error: Server-side issue

Always implement proper error handling and retry logic for production applications.