Create an Email in Blazor

With GemBox.Email you can build Blazor applications that send, receive, reply, and process mail messages. The following live demos show how you can create Blazor apps that generate emails and download them to your browser.

Create Email in Blazor Server App

The following example shows how you can create a Blazor Server application that generates an EML file and downloads it with a downloadFileFromStream JS function (see ASP.NET Core Blazor file downloads).

Generating an EML file from Blazor Server app
Screenshot of submitted web form and created mail message
@page "/"
@inject IJSRuntime JS
@using BlazorServerApp.Data
@using System.IO
@using GemBox.Email

<h1>Email generator [Blazor Server App]</h1>

<EditForm Model="model" OnSubmit="CreateEmail">
    <div class="form-group">Header text: <InputText @bind-Value="model.Sender" class="form-control"></InputText></div>
    <div class="form-group">Body text: <InputText @bind-Value="model.Receiver" class="form-control"></InputText></div>
    <div class="form-group">Footer text: <InputText @bind-Value="model.Subject" class="form-control"></InputText></div>
    <div class="form-group">Message [HTML]: <InputTextArea @bind-Value="model.Message" class="form-control" rows="8"></InputTextArea></div>
    <div class="form-group"><button class="btn btn-primary mt-2" type="submit">Create</button></div>
</EditForm>

@code {
    private MessageModel model = new();

    private async Task CreateEmail()
    {
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

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

        // Save EML file.
        var stream = new MemoryStream();
        message.Save(stream, MailMessageFormat.Eml);
        stream.Position = 0;

        // Download file.
        using var streamRef = new DotNetStreamReference(stream);
        await JS.InvokeVoidAsync("downloadFileFromStream", "BlazorServerOutput.eml", streamRef);
    }
}
namespace BlazorServerApp.Data
{
    public class MessageModel
    {
        public string Sender { get; set; } = "sender@example.com";
        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'>Blazor Server</strong> application!";
    }
}

Create Email in Blazor WebAssembly App

The following example shows how you can create a Blazor WebAssembly application that generates an EML file and downloads it with a downloadFileFromStream JS function (see ASP.NET Core Blazor file downloads).

Generating an EML file from Blazor WebAssembly app
Screenshot of submitted web form and created mail message
@page "/"
@inject IJSRuntime JS
@using BlazorWebAssemblyApp.Data
@using System.IO
@using GemBox.Email

<h1>Email generator [Blazor WebAssembly App]</h1>

<EditForm Model="model" OnSubmit="CreateEmail">
    <div class="form-group">Header text: <InputText @bind-Value="model.Sender" class="form-control"></InputText></div>
    <div class="form-group">Body text: <InputText @bind-Value="model.Receiver" class="form-control"></InputText></div>
    <div class="form-group">Footer text: <InputText @bind-Value="model.Subject" class="form-control"></InputText></div>
    <div class="form-group">Message [HTML]: <InputTextArea @bind-Value="model.Message" class="form-control" rows="8"></InputTextArea></div>
    <div class="form-group"><button class="btn btn-primary mt-2" type="submit">Create</button></div>
</EditForm>

@code {
    private MessageModel model = new();

    private async Task CreateEmail()
    {
        ComponentInfo.SetLicense(EmailLicense.Value);

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

        // Save EML file.
        var stream = new MemoryStream();
        message.Save(stream, MailMessageFormat.Eml);
        stream.Position = 0;

        // Download file.
        using var streamRef = new DotNetStreamReference(stream);
        await JS.InvokeVoidAsync("downloadFileFromStream", "BlazorWebAssemblyOutput.eml", streamRef);
    }
}
namespace BlazorWebAssemblyApp.Data
{
    public class MessageModel
    {
        public string Sender { get; set; } = "sender@example.com";
        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'>Blazor WebAssembly</strong> application!";
    }
}

Host and deploy Blazor

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.

See also


Next steps

GemBox.Email is a .NET component that enables you to read, write, receive, and send emails from your .NET applications using one simple API.

Download Buy