Create and Save an Email with ASP.NET Core

GemBox.Email is a standalone .NET component that can be used in web applications that target ASP.NET Core 2.0 - 5.0.

To use GemBox.Email, simply install the GemBox.Email package with NuGet or add the following package reference in the project file.

<Project Sdk="Microsoft.NET.Sdk.Web">

    <PropertyGroup>
        <TargetFramework>net5.0</TargetFramework>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="GemBox.Email" Version="*" />
    </ItemGroup>

</Project>

The following live demos show how you can create web apps that generate emails and download them to your browser.

Instead of saving/downloading an email, you can achieve other email-related tasks as well like sending a feedback email from your Contact page, or a password recovery/reset email from your Login page, or an account confirmation email from your Register page.

Before sending a confirmation email, you could validate the email address your users provide in the registration form to check if it's a valid and existing email address.

Create Email in ASP.NET Core MVC

The following example shows how you can create an ASP.NET Core MVC application that generates an email, saves it into a specified format like EML or MSG, and downloads it with a FileStreamResult.

Sending form data from Razor view and generating email file in ASP.NET Core MVC application
Screenshot of submitted web form and created mail message
@model MessageModel

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>GemBox.Email in ASP.NET Core MVC application</title>
    <link rel="icon" href="~/favicon.ico" />
    <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
    <link rel="stylesheet" href="~/css/site.css" />
</head>
<body>
    <header>
        <nav class="navbar border-bottom box-shadow mb-3">
            <div class="container">
                <a class="navbar-brand" asp-controller="Home" asp-action="Index">Home</a>
            </div>
        </nav>
    </header>

    <div class="container">
        <main class="pb-3 row">
            <h1 class="display-4 p-3">Email generator [Razor View]</h1>
            <div class="col-lg-6">
                <form asp-action="Download">
                    <div class="form-group">*Sender: <input asp-for="Sender" class="form-control" required /></div>
                    <div class="form-group">*Receiver: <input asp-for="Receiver" class="form-control" required /></div>
                    <div class="form-group">Subject: <input asp-for="Subject" class="form-control" /></div>
                    <div class="form-group">Message [HTML]: <input asp-for="Message" class="form-control" /></div>
                    <div class="form-group"><input type="submit" value="Create" class="btn btn-primary" /></div>
                </form>
            </div>
        </main>
    </div>

    <footer class="footer border-top text-muted">
        <div class="container">&copy; GemBox Ltd. — All rights reserved.</div>
    </footer>
</body>
</html>
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using System.IO;
using Microsoft.AspNetCore.Mvc;
using EmailCoreMvc.Models;
using GemBox.Email;
using GemBox.Email.Mime;

namespace EmailCoreMvc.Controllers
{
    public class HomeController : Controller
    {
        static HomeController()
        {
            // If using Professional version, put your serial key below.
            ComponentInfo.SetLicense("FREE-LIMITED-KEY");
        }

        public IActionResult Index()
        {
            return View(new MessageModel());
        }

        public FileStreamResult Download(MessageModel model)
        {
            // Create email.
            var message = new MailMessage(model.Sender, model.Receiver)
            {
                Subject = model.Subject,
                BodyHtml = model.Message
            };

            // Save email in specified file format.
            var stream = new MemoryStream();
            message.Save(stream, MailMessageFormat.Eml);
            stream.Position = 0;

            // Download file.
            return File(stream, MediaTypes.MessageRfc822, "OutputFromView.eml");
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel() { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}

namespace EmailCoreMvc.Models
{
    public class MessageModel
    {
        [EmailAddress, Required]
        public string Sender { get; set; } = "sender@example.com";
        [EmailAddress, Required]
        public string Receiver { get; set; } = "receiver@example.com";
        public string Subject { get; set; } = "Example Message";
        public string Message { get; set; } =
            "This is an example message created with " +
            "<a href='https://www.gemboxsoftware.com/email'>GemBox.Email</a> from " +
            "<strong style='color:red'>ASP.NET Core</strong> application!";
    }
}

Create Email in ASP.NET Core Razor Pages

The following example shows how you can create an ASP.NET Core Razor Pages application that generates an email, saves it into a specified format like EML or MSG, and downloads it with a FileContentResult.

Sending form data from Razor page and generating email file in ASP.NET Core Razor Pages application
Screenshot of submitted web form and created mail message
@page
@model IndexModel

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>GemBox.Email in ASP.NET Core Razor Pages application</title>
    <link rel="icon" href="~/favicon.ico" />
    <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
    <link rel="stylesheet" href="~/css/site.css" />
</head>
<body>
    <header>
        <nav class="navbar border-bottom box-shadow mb-3">
            <div class="container">
                <a class="navbar-brand" asp-page="/Index">Home</a>
            </div>
        </nav>
    </header>

    <div class="container">
        <main class="pb-3 row">
            <h1 class="display-4 p-3">Email generator [Razor View]</h1>
            <div class="col-lg-6">
                <form asp-action="Download">
                    <div class="form-group">*Sender: <input asp-for="Message.Sender" class="form-control" required /></div>
                    <div class="form-group">*Receiver: <input asp-for="Message.Receiver" class="form-control" required /></div>
                    <div class="form-group">Subject: <input asp-for="Message.Subject" class="form-control" /></div>
                    <div class="form-group">Message [HTML]: <input asp-for="Message.Message" class="form-control" /></div>
                    <div class="form-group"><input type="submit" value="Create" class="btn btn-primary" /></div>
                </form>
            </div>
        </main>
    </div>

    <footer class="footer border-top text-muted">
        <div class="container">&copy; GemBox Ltd. — All rights reserved.</div>
    </footer>
</body>
</html>
using System.ComponentModel.DataAnnotations;
using System.IO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using EmailCorePages.Models;
using GemBox.Email;
using GemBox.Email.Mime;

namespace EmailCorePages.Pages
{
    public class IndexModel : PageModel
    {
        [BindProperty]
        public MessageModel Message { get; set; }

        public IndexModel()
        {
            this.Message = new MessageModel();

            // If using Professional version, put your serial key below.
            ComponentInfo.SetLicense("FREE-LIMITED-KEY");
        }

        public void OnGet() { }

        public FileContentResult OnPost()
        {
            // Create email.
            var message = new MailMessage(this.Message.Sender, this.Message.Receiver)
            {
                Subject = this.Message.Subject,
                BodyHtml = this.Message.Message
            };

            // Save email in specified file format.
            var stream = new MemoryStream();
            message.Save(stream, MailMessageFormat.Eml);
            stream.Position = 0;

            // Download file.
            return File(stream.ToArray(), MediaTypes.MessageRfc822, "OutputFromPage.eml");
        }
    }
}

namespace EmailCorePages.Models
{
    public class MessageModel
    {
        [EmailAddress, Required]
        public string Sender { get; set; } = "sender@example.com";
        [EmailAddress, Required]
        public string Receiver { get; set; } = "receiver@example.com";
        public string Subject { get; set; } = "Example Message";
        public string Message { get; set; } =
            "This is an example message created with " +
            "<a href='https://www.gemboxsoftware.com/email'>GemBox.Email</a> from " +
            "<strong style='color:red'>ASP.NET Core</strong> application!";
    }
}

Host and deploy ASP.NET Core

GemBox.Email is licensed per individual developer, and the licenses include a royalty-free deployment. You can feel free to build an unlimited number of applications and deploy or distribute them to an unlimited number of services, servers, or end-user machines with no extra cost.

GemBox.Email licenses are compatible with SaaS or PaaS solutions, as long as they don't offer similar or competing functionality to our component, or expose our features through an API for use by an unlicensed third party. For more information, please check the EULA.

Want more?

Next example GitHub

Check the next example or select an example from the menu. You can also download our examples from the GitHub.


Like it?

Download Buy

If you want to try the GemBox.Email yourself, you can download the free version. It delivers the same performance and set of features as the professional version, but with some operations limited. To remove the limitation, you need to purchase a license.