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. The following example shows how to create a Blazor Server application that generates an EML file and downloads it with a The following code shows how to 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). GemBox.Document follows a licensing model per individual developer, which includes royalty-free deployment. You are allowed to build an unlimited number of applications and deploy or distribute them across numerous services, servers, or end-user machines without any additional cost. For more information, read our End User License Agreement (EULA).Create Email in Blazor Server App
downloadFileFromStream
JS function (see ASP.NET Core Blazor file downloads).@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
@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("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", "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