https://github.com/ultimaweapon/composer
Send the emails from dynamic templates without re-deploying your .NET app
https://github.com/ultimaweapon/composer
csharp dotnet email
Last synced: 2 months ago
JSON representation
Send the emails from dynamic templates without re-deploying your .NET app
- Host: GitHub
- URL: https://github.com/ultimaweapon/composer
- Owner: ultimaweapon
- License: mit
- Created: 2021-04-20T10:09:24.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2022-03-05T15:14:52.000Z (over 4 years ago)
- Last Synced: 2025-12-27T06:20:36.624Z (6 months ago)
- Topics: csharp, dotnet, email
- Language: C#
- Homepage:
- Size: 14.6 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: COPYING
Awesome Lists containing this project
README
# Composer
[](https://www.nuget.org/packages/Composer)
This is a framework for .NET to compose an email from a configured template so the email format can be update on the fly without updating the application code.
## Usage
First add essential services to `IServiceCollection` by invoke `AddComposer` extension method:
```csharp
services.AddComposer();
```
`AddComposer` return an object for configure Composer. The application required to provider `IEmailSender` and `ITemplateProvider` by invoke `AddSender`
and `AddTemplateProvider`:
```csharp
services
.AddComposer()
.AddSender()
.AddTemplateProvider();
```
### SMTP sender
SMTP sender is shipped with Composer so you don't need to install additional package to use it. To use SMTP sender invoke `AddSmtpSender`:
```csharp
services
.AddComposer()
.AddSmtpSender(options =>
{
options.SmtpServer = "host";
options.SmtpPort = 25;
})
.AddTemplateProvider();
```
### Available sender
- [Amazone SES](https://github.com/ultimicro/composer-aws)
### Available template provider
- [StringTemplate 4](https://github.com/ultimicro/composer-stringtemplate)
### Define an email
You need to create a new class that derived from `Email` class. Each class represents one type of email you want to send (e.g. an email to send when user has
signed up).
```csharp
namespace SampleApp;
using System;
using Composer;
internal sealed class RegistrationCompletedEmail : Email
{
public static readonly Guid Id = new Guid("e8c01835-bc01-4f03-b4f7-197d0d0a3b4a");
public RegistrationCompletedEmail(string username)
{
this.Username = username;
}
public string Username { get; }
protected override object TemplateId => Id;
protected override object? BuildTemplateData() => new { this.Username };
}
```
### Send an email
Inject `IEmailComposer` to the class you want to send email. Then invoke `ComposeAsync`:
```csharp
var email = new RegistrationCompletedEmail("john");
await composer.ComposeAsync("john@example.com", email, cancellationToken);
```
### Add attachments
Override `Email.BuildAttachments` or `Email.BuildAttachmentsAsync` to provide attachments for the email.
## License
MIT