Sending Single Emails
Relevant source files
The following files were used as context for generating this wiki page:
Sending single emails is the core functionality of the Resend .NET SDK. It enables applications to transmit individual messages via the Resend API using the IResend interface and the EmailMessage data structure.
The SDK translates high-level C# objects into JSON payloads for the Resend REST API. It handles authentication, serialization, and HTTP communication, providing both standard and idempotent sending options.
Sources: src/Resend/README.md, README.md:104-124
Core Components
The sending process relies on two primary components: the EmailMessage class and the IResend client interface.
EmailMessage Data Structure
The EmailMessage class represents the email payload. It contains properties for routing, content, and metadata.
| Property | Type | Description |
|---|---|---|
From | EmailAddress | The sender address. |
To | EmailAddressList | Primary recipients. |
Subject | string | The email subject line. |
HtmlBody | string | The HTML version of the message. |
TextBody | string | The plain text version of the message. |
Attachments | List<EmailAttachment> | Files attached to the email (max 40mb). |
Headers | Dictionary<string, string> | Custom SMTP headers. |
Tags | List<EmailTag> | Metadata tags for tracking. |
Sources: src/Resend/EmailMessage.cs:10-75
IResend Interface
The IResend interface defines the methods available for sending. The ResendClient implementation uses HttpClient to communicate with the Resend API.
public interface IResend
{
Task<ResendResponse<Guid>> EmailSendAsync(EmailMessage email, CancellationToken cancellationToken = default);
Task<ResendResponse<Guid>> EmailSendAsync(string idempotencyKey, EmailMessage email, CancellationToken cancellationToken = default);
}
Sources: src/Resend/ResendClient.cs:64-80
Architecture and Data Flow
The following diagram illustrates the flow of a single email request from the application code to the Resend API.
The SDK wraps the HTTP request in a ResendResponse object, which provides the unique email identifier upon success.
Sources: src/Resend/ResendClient.cs:64-70, tools/Resend.ApiServer/Controllers/EmailController.cs:22-38
Features and Logic
Idempotency
To prevent duplicate emails during network retries, you can provide an idempotencyKey. The SDK adds this value to the Idempotency-Key HTTP header.
Sources: src/Resend/ResendClient.cs:72-80, tools/Resend.ApiServer/Controllers/EmailController.cs:25-30
Content Rendering
Applications can send emails with either HTML or plain text bodies. Setting the HtmlBody property automatically instructs the API to treat the message as an HTML email.
Sources: src/Resend/EmailMessage.cs:56-66, src/Resend.FluentEmail/ResendSender.cs:85-97
Attachment Management
Emails support multiple attachments. Each EmailAttachment requires a filename and either a byte array content or a path to the file.
Implementation Example
The SDK must be configured in the dependency injection container before use.
// Configuration
builder.Services.AddResend( o =>
{
o.ApiToken = "re_123456789";
} );
// Usage
public async Task Send(IResend resend)
{
var message = new EmailMessage();
message.From = "sender@example.com";
message.To.Add("recipient@example.com");
message.Subject = "Hello World";
message.HtmlBody = "<strong>Sent via Resend .NET</strong>";
var response = await resend.EmailSendAsync(message);
}
Sources: src/Resend/README.md:46-77
Summary
Sending single emails via the ResendClient involves constructing an EmailMessage and invoking EmailSendAsync. The system ensures type-safe interaction with the Resend API, supporting advanced features like custom headers, attachments, and idempotency control to maintain reliable delivery.