HypeLab.MailEngine
0.9.5
Inconsistency in SmtpClientInfo class. Please update to higher versions.
See the version list below for details.
dotnet add package HypeLab.MailEngine --version 0.9.5
NuGet\Install-Package HypeLab.MailEngine -Version 0.9.5
<PackageReference Include="HypeLab.MailEngine" Version="0.9.5" />
paket add HypeLab.MailEngine --version 0.9.5
#r "nuget: HypeLab.MailEngine, 0.9.5"
// Install HypeLab.MailEngine as a Cake Addin #addin nuget:?package=HypeLab.MailEngine&version=0.9.5 // Install HypeLab.MailEngine as a Cake Tool #tool nuget:?package=HypeLab.MailEngine&version=0.9.5
Hype-Lab Mail Engine
Note: This library targets .NET 8, so it's not compatible with lower versions of .NET.
HypeLab.MailEngine is a flexible and modular .NET library that targets .NET 8 for managing multiple email clients, including support for both SMTP and SendGrid for now. The package allows to configure and use multiple email clients with distinct settings, leveraging dependency injection for seamless integration.
Features
- Support for multiple email clients: Configure and use multiple email clients with individual settings.
- SendGrid and SMTP support: Easily switch between different email providers.
- Flexible configuration: Define your email client settings in appsettings.json.
- Dependency Injection: Integrate smoothly with ASP.NET Core's DI system.
Installation
Add the NuGet package to your project:
dotnet add package HypeLab.MailEngine
Configuration
In your appsettings.json
, define the settings for your email clients:
{
"SendGrid": {
"ClientId": "Hype-Lab SendGridClient", // required
"IsDefault": true, // optional (if not present, it will be set as true)
"ApiKey": "dummyKey", // required
"RequestHeaders": [ // optional
{
"Key": "Authorization", // required if RequestHeaders is present
"Value": "Bearer SG" // required if RequestHeaders is present
},
// etc.
{
"Key": "Content-Type",
"Value": "application/json"
}
],
"Host": "https://api.sendgrid.com/v3/mail/send", // optional
"Version": "v3", // optional
"UrlPath": "/mail/send", // optional
"Reliability": { // optional
"MaximumNumberOfRetries": 3, // required if Reliability is present
"MinimumBackOffInSeconds": 1, // required if Reliability is present
"DeltaBackOffInSeconds": 1, // required if Reliability is present
"MaximumBackOffInSeconds": 10 // required if Reliability is present
},
"Auth": { // optional
"Scheme": "Bearer", // required if Auth is present
"Parameter": "SG" // optional
},
"HttpErrorAsException": true // optional
},
"Smtp": {
"ClientId": "Hype-Lab SmtpClient", // required
"Server": "smtp.google.com", // required
"Port": 587, // required
"Email": "info@hype-lab.it", // required
"Password": "dummyPassword", // required
"EnableSsl": true, // required
"IsDefault": false, // optional (if not present, it will be set as true)
"UseDefaultCredentials": false, // optional
"Domain": "hype-lab.it", // optional, passed to NetworkCredential constructor
"Timeout": 10000, // optional
"PickupDirectoryLocation": "C:\\inetpub\\mailroot\\Pickup", // optional
"TargetName": "STARTTLS/smtp.google.com", // optional
"DeliveryMethod": "Network", // optional, default is Network
"DeliveryFormat": "International", // optional, default is International
"ClientCertificates": [ // optional
{
"FileName": "C:\\inetpub\\mailroot\\Pickup\\hype-lab.it.pfx", // required if ClientCertificates is present
"Password": "dummyPassword", // optional
"KeyStorageFlags": "MachineKeySet" // optional
}
]
},
}
NOTE: The IsDefault
property is used to set the default email client.
In case you intend to use a single email sender, the class constructors allow the non-presence of IsDefault
, and set it as true
, so be careful when using multiple email senders, the engine will throws an exception if more than one sender is set as default.
Feel free to leave feedback about any outages or issues you may encounter with any of the properties.
Usage
Single Email Client
For applications that need only one email client, use the single email client configuration:
- Retrieve configuration sections in Startup.cs:
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
SendGridAccessInfo sendGridAccessInfo = builder.Configuration.GetSection("SendGrid").Get<SendGridAccessInfo>()
?? throw new InvalidOperationException("SendGrid section not found");
builder.Services.AddMailEngine(sendGridAccessInfo);
// ...other service registrations
WebApplication app = builder.Build();
- Inject and use the email service:
public class MyService
{
private readonly IEmailService _emailService;
public MyService(IEmailService emailService)
{
_emailService = emailService;
}
public async Task SendEmailAsync()
{
CustomMailMessage mailMessage = CustomMailMessage.Create(
emailTo: "your_email@to",
emailSubject: "MailEngine .NET Library Email",
emailFrom: "info@hype-lab.it",
htmlMessage: "<h1>Test email from MailEngine .NET Library</h1>",
plainTextContent: "Test email from MailEngine .NET Library",
emailToName: "Matt P",
emailFromName: "Info Hype-Lab");
EmailServiceResponse resp = await emailService.SendEmailAsync(mailMessage);
if (resp.IsError)
throw new InvalidOperationException(resp.ErrorMessage);
// ...
}
}
Multiple Email Clients
For applications that need to manage multiple email clients, use the multiple email client configuration:
- Retrieve configuration sections and pass them to AddMailEngine in Startup.cs:
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
SendGridAccessInfo sendGridAccessInfo = Configuration.GetSection("SendGrid").Get<SendGridAccessInfo>()
?? throw new InvalidOperationException("SendGrid section not found");
SendGridAccessInfo sendGridAccessInfo2 = Configuration.GetSection("SendGrid2").Get<SendGridAccessInfo>()
?? throw new InvalidOperationException("SendGrid2 section not found");
SmtpAccessInfo smtpAccessInfo = Configuration.GetSection("Smtp").Get<SmtpAccessInfo>()
?? throw new InvalidOperationException("Smtp section not found");
services.AddMailEngine(sendGridAccessInfo, sendGridAccessInfo2, smtpAccessInfo);
// ...other service registrations
WebApplication app = builder.Build();
- Inject and use the email service:
public class MyService
{
private readonly IEmailService _emailService;
public MyService(IEmailService emailService)
{
_emailService = emailService;
}
public async Task SendEmailAsync()
{
CustomMailMessage mailMessage = CustomMailMessage.Create(
emailTo: "your_email@to",
emailSubject: "MailEngine .NET Library Email",
emailFrom: "info@hype-lab.it",
htmlMessage: "<h1>Test email from MailEngine .NET Library</h1>",
plainTextContent: "Test email from MailEngine .NET Library",
emailToName: "Matt P",
emailFromName: "Info Hype-Lab");
EmailServiceResponse resp = await emailService.SendEmailAsync(mailMessage);
if (resp.IsError)
throw new InvalidOperationException(resp.ErrorMessage);
// ...
}
}
2.5 Use a specific sender instead of the default one passing the clientId optional argument to SendEmailAsync:
public async Task SendEmailAsync(string? clientId = null)
{
CustomMailMessage mailMessage = CustomMailMessage.Create(
emailTo: "your_email@to",
emailSubject: "MailEngine .NET Library Email",
emailFrom: "info@hype-lab.it",
htmlMessage: "<h1>Test email from MailEngine .NET Library</h1>",
plainTextContent: "Test email from MailEngine .NET Library",
emailToName: "Matt P",
emailFromName: "Info Hype-Lab");
EmailServiceResponse resp = await emailService.SendEmailAsync(mailMessage, clientId);
if (resp.IsError)
throw new InvalidOperationException(resp.ErrorMessage);
// ...
}
Overloaded AddMailEngine Methods
Single Email Client
Use this overload if you have only one email client:
public static IServiceCollection AddMailEngine(this IServiceCollection services, IMailAccessInfo mailAccessInfo)
Multiple Email Clients
Use this overload if you need to support multiple email clients:
public static IServiceCollection AddMailEngine(this IServiceCollection services, params IMailAccessInfo[] mailAccessInfoParams)
Contributing
Feedbacks and and Contributions are welcome! Also feel free to open an issue or to ask anything if needed.
License
This project is licensed under the MIT License.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net8.0
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
- Microsoft.Extensions.Http (>= 8.0.0)
- SendGrid (>= 9.29.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Extended support, abstraction and nullability handling for most Smtp and SendGrid clients options; README.md updated.