MailingNinja is a simple boilerplate solution that demonstrates sending mails with attachments from an ASP.NET Core (.NET 6) application using MailKit and MimeKit.
The application demonstrates three key functionalities which are common in business applications.
- Generating HTML from Razor Views which are rendered with data using RazorViewEngine
- Generating a PDF report from HTML and downloading via ASP.NET Core (.NET 6) MVC using DinkToPdf
- Sending HTML content (with and without Attachments) from the application using MailKit and MimeKit
RazorViewEngine is a view engine that converts the razor powered cshtml files in an MVC application into plain HTML documents which the browser can understand. It takes care of the variable resolution, loops and other control structures of the Razor syntax and processes them to form the HTML only which the browser can render and display to the user.
For starters, An MVC application developed over the ASP.NET framework internally uses Razor to render html views and data over layout files during a request. Applications developed using ASP.NET can make use of an external library called RazorEngine which levarages the Razor engine capabilities of the MVC framework.
Although RazorEngine plugin has no direct support for ASP.NET Core, the aspnetcore framework still comes with the razor engine which provides helper methods which can by-produce the rendered HTML over the passed model data and return the plain HTML along.
DinkToPdf is a .NET library which can generate PDF from an input content. The library offers customizations such as Page Layouts, Numbering, Headers, Footers and so on. We can pass in a HTML string to the library and it returns PDF bytes which can be further written onto a File.
- Demonstration of Generating Dynamic HTML content for input data through Razor Views
- Writing the generated dynamic HTML content into PDF and downloading it (through FileResult)
- Configuring SMTP and sending out the dynamic HTML content as an Email
- Sending out the generated PDF content as an attachment to the above sent Email
- CQRS with MediatR
- Repositories and UnitOfWork
- SQLite Database integration
- Clean Architecture with decoupled layers
- ASP.NET Core (.NET 6)
- EF Core and SQLite
- MediatR
- MailKit and MimeKit
- DinkToPdf
You can refer to the below articles to better understand the solution:
- Fetch Rendered View HTML in ASP.NET Core using Razor
- Sending HTML Content over Email Using MailKit
- Embedding Images to Content using MimeKit
For more detailed articles and how-to guides, visit https://referbruv.com
Liked my work? You can support me by buying me a coffee!