MushCOA.EmailLibrary
1.0.1
dotnet add package MushCOA.EmailLibrary --version 1.0.1
NuGet\Install-Package MushCOA.EmailLibrary -Version 1.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MushCOA.EmailLibrary" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MushCOA.EmailLibrary" Version="1.0.1" />
<PackageReference Include="MushCOA.EmailLibrary" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add MushCOA.EmailLibrary --version 1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: MushCOA.EmailLibrary, 1.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package MushCOA.EmailLibrary@1.0.1
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=MushCOA.EmailLibrary&version=1.0.1
#tool nuget:?package=MushCOA.EmailLibrary&version=1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
MushCOA.EmailLibrary
Reusable .NET email library using MailKit.
Features
- Send HTML emails
- Send plain text emails
- Send attachments from file path
- Send attachments from byte array
- Supports To, CC, BCC, and Reply-To
- Supports dependency injection
- SMTP settings from appsettings.json, User Secrets, environment variables, or other .NET configuration sources
Installation
dotnet add package MushCOA.EmailLibrary
Or install from Visual Studio:
Manage NuGet Packages → Browse → Search MushCOA.EmailLibrary → Install
Configuration
Add this to appsettings.json or appsettings.Development.json:
{
"Email": {
"SmtpHost": "smtp.example.com",
"SmtpPort": 587,
"Username": "your-smtp-username",
"Password": "your-smtp-password",
"DefaultFromEmail": "noreply@example.com",
"DefaultFromName": "Example App",
"SecureSocketOption": "StartTls",
"TimeoutMilliseconds": 30000
}
}
Do not store real SMTP passwords in source control. Use User Secrets, environment variables, Azure Key Vault, or another secure configuration provider.
Register the service
In Program.cs:
using EmailLibrary;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEmailLibrary(builder.Configuration);
var app = builder.Build();
Send an email
Inject IEmailService into your controller or service:
using EmailLibrary;
using Microsoft.AspNetCore.Mvc;
public class EmailTestController : Controller
{
private readonly IEmailService _emailService;
public EmailTestController(IEmailService emailService)
{
_emailService = emailService;
}
public async Task<IActionResult> SendTestEmail()
{
await _emailService.SendAsync(new EmailRequest
{
To =
{
new EmailAddress("user@example.com", "Test User")
},
Subject = "Test Email",
TextBody = "Hello, this is a plain text email.",
HtmlBody = "<p>Hello, this is an <strong>HTML email</strong>.</p>"
});
return Content("Email sent successfully.");
}
}
Send email with attachment from file
await _emailService.SendAsync(new EmailRequest
{
To =
{
new EmailAddress("user@example.com", "Test User")
},
Subject = "Document Attached",
HtmlBody = "<p>Please see the attached document.</p>",
Attachments =
{
EmailAttachment.FromFile(
@"C:\Temp\Document.pdf",
"Document.pdf",
"application/pdf")
}
});
Send email with attachment from byte array
var fileBytes = File.ReadAllBytes(@"C:\Temp\Document.pdf");
await _emailService.SendAsync(new EmailRequest
{
To =
{
new EmailAddress("user@example.com", "Test User")
},
Subject = "Generated Document",
HtmlBody = "<p>Your generated document is attached.</p>",
Attachments =
{
EmailAttachment.FromBytes(
fileBytes,
"Document.pdf",
"application/pdf")
}
});
Configuration section name
By default, the library reads from the Email section:
builder.Services.AddEmailLibrary(builder.Configuration);
If your config section has a different name, use:
builder.Services.AddEmailLibrary(builder.Configuration, "SmtpSettings");
| 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net8.0
- MailKit (>= 4.17.0)
- Microsoft.Extensions.Configuration.Abstractions (>= 8.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Options (>= 8.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 8.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.