Create and Save an Email with ASP.NET Core

Gembox.Email supports the .NET Standard, and as a result, you can use it on various platforms like Xamarin, Mono, Unity, .NET Core, etc.

GemBox.Email provides a royalty-free deployment, and there are no server or OEM licenses, so you can feel free to build an unlimited number of applications and deploy them to an unlimited number of servers.

The following example and the provided demo show a simple ASP.NET Core MVC application that creates an email and downloads it to the client's browser. Note, you can accomplish the same in the ASP.NET Core Razor Pages application as well.

Screenshot of downloaded email from ASP.NET Core
Creating and saving an email from ASP.NET Core application
@model GemBox.SaveMessage.Controllers.MessageModel
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Create and Save an Email Message</title>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/AiS6JXm" crossorigin="anonymous">
    <style>
        label::after { content: ':' }
        .required::before { content: '*' }
    </style>
</head>
<body class="p-md-3">
    <h3>New email message</h3>
    <form asp-action="Create">
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
        <div class="form-group">
            <label asp-for="Sender" class="required"></label>
            <input asp-for="Sender" class="form-control" value="sender@example.com"/>
            <span asp-validation-for="Sender" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="Receiver" class="required"></label>
            <input asp-for="Receiver" class="form-control" value="receiver@example.com"/>
            <span asp-validation-for="Receiver" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="Subject" class="required"></label>
            <input asp-for="Subject" class="form-control" value="Example Message"/>
            <span asp-validation-for="Subject" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="BodyText"></label>
            <textarea asp-for="BodyText" class="form-control"></textarea>
            <span asp-validation-for="BodyText" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="BodyHtml"></label>
            <textarea asp-for="BodyHtml" class="form-control"></textarea>
            <span asp-validation-for="BodyHtml" class="text-danger"></span>
        </div>
        <div class="form-group">
            <input type="submit" value="Save" class="btn btn-default"/>
        </div>
    </form>
</body>
</html>
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel.DataAnnotations;
using System.IO;
using GemBox.Email;
using GemBox.Email.Mime;

public class MessageController : Controller
{
    public ActionResult Create() => View();

    [HttpPost]
    public ActionResult Create(MessageModel model)
    {
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        if (!ModelState.IsValid)
            return View(model);

        var message = new MailMessage(model.Sender, model.Receiver)
        {
            Subject = model.Subject,
            BodyText = model.BodyText,
            BodyHtml = model.BodyHtml
        };

        using (var stream = new MemoryStream())
        {
            message.Save(stream, MailMessageFormat.Eml);
            return File(stream.ToArray(), MediaTypes.MessageRfc822, "Save.eml");
        }
    }
}

public class MessageModel
{
    [Display(Name = "Sender"), Required, EmailAddress]
    public string Sender { get; set; }

    [Display(Name = "Receiver"), Required, EmailAddress]
    public string Receiver { get; set; }

    [Display(Name = "Subject"), Required]
    public string Subject { get; set; }

    [Display(Name = "Plain message")]
    public string BodyText { get; set; }

    [Display(Name = "HTML message")]
    public string BodyHtml { get; set; }
}

Instead of saving/downloading an email, you can achieve other email-related tasks like sending a feedback email from your Contact Us page or a password recovery/reset email from your Login page, or receiving and replying to emails, etc.

You can also use it for an account confirmation from your Register page; for instance, the following example shows how you can implement an IEmailSender interface.

using Microsoft.AspNetCore.Identity.UI.Services;
using System.Threading.Tasks;
using GemBox.Email;
using GemBox.Email.Smtp;

public class EmailConfirmationSender : IEmailSender
{
    private const string Host = "<HOST> (e.g. smtp.gmail.com)";
    private const string Username = "<USERNAME>";
    private const string Password = "<PASSWORD>";
    private const string From = "<FROM> (e.g. john.doe@gmail.com";

    public EmailConfirmationSender() => ComponentInfo.SetLicense("FREE-LIMITED-KEY");

    public Task SendEmailAsync(string to, string subject, string htmlMessage)
    {
        return Task.Run(() =>
        {
            using (var smtp = new SmtpClient(Host))
            {
                smtp.Connect();
                smtp.Authenticate(Username, Password);

                var message = new MailMessage(From, to)
                {
                    Subject = subject,
                    BodyHtml = htmlMessage
                };

                smtp.SendMessage(message);
            }
        });
    }
}

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

Download examples from GitHub.