Create and Save an Email with ASP.NET Core

Gembox.Email supports the .NET Standard. As a result of this, various platforms are supported such as .NET Framework, .NET Core, Xamarin, and Universal Windows Platform (UWP).

This allows us to create different kinds of application using, for example ASP.NET, Windows.Forms, or WPF, and run them on different operating systems including Windows, Linux, and MacOS.

The following example is a simple ASP.NET Core MVC application that saves a mail message to a file, based on data provided by the user.

Screenshot
Save mail message in ASP.NET Core MVC Screenshot

See the full code below.

@model GemBox.SaveMessage.Controllers.MessageModel
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Create a Mail Message</title>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
<body style="padding: 20px">
    <h3>New mail message</h3>
    <form asp-action="Create">
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
        <div class="form-group">
            <label asp-for="SenderAddress"></label>
            <input asp-for="SenderAddress" class="form-control" value="sender@example.com" />
            <span asp-validation-for="SenderAddress" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="ReceiverAddress"></label>
            <input asp-for="ReceiverAddress" class="form-control" value="receiver@example.com" />
            <span asp-validation-for="ReceiverAddress" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="Subject"></label>
            <input asp-for="Subject" class="form-control" value="This is an 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 System.ComponentModel.DataAnnotations;
using GemBox.Email;
using Microsoft.AspNetCore.Mvc;
using System.IO;

namespace GemBox.SaveMessage.Controllers
{
    public class MessageController : Controller
    {
        public ActionResult Create()
        {
            return View();
        }

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

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

            MailMessage message = new MailMessage(
                new MailAddress(messageModel.SenderAddress),
                new MailAddress(messageModel.ReceiverAddress));
            message.Subject = messageModel.Subject;
            message.BodyText = messageModel.BodyText;

            if (!string.IsNullOrEmpty(messageModel.BodyHtml))
                message.BodyHtml = messageModel.BodyHtml;

            return File(WriteMessageToByteArray(message), "message/rfc822", "Save.eml");
        }

        private byte[] WriteMessageToByteArray(MailMessage message)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                message.Save(stream, MailMessageFormat.Eml);
                return stream.ToArray();
            }
        }
    }

    public class MessageModel
    {
        [Required]
        [EmailAddress]
        [Display(Name = "Sender address:")]
        public string SenderAddress { get; set; }
        [Required]
        [EmailAddress]
        [Display(Name = "Receiver address:")]
        public string ReceiverAddress { get; set; }
        [Required]
        [Display(Name = "Subject:")]
        public string Subject { get; set; }
        [Required]
        [Display(Name = "Raw email message:")]
        public string BodyText { get; set; }
        [Display(Name = "HTML email message:")]
        public string BodyHtml { get; set; }
    }
}