Once you have set up your email component and configured your parameters using EmailParameterCollectionBuilder<TEmail>, you can render the email using the BlazorEmailRenderer. This section outlines how to do this in detail.

Step-by-Step Rendering Process

  1. Instantiate the Email Renderer: Ensure that you have an instance of BlazorEmailRenderer ready to use. You typically do this in your service or context where you handle email operations.
var emailRenderer = new BlazorEmailRenderer(serviceProvider);
  1. Configure Your Email Parameters: Use the EmailParameterCollectionBuilder<TEmail> to set the parameters for your email. This is done through a lambda expression, allowing you to specify which properties of your email template you want to populate.
var emailParams = new EmailParameterCollectionBuilder<EmailTemplate>(builder =>
{
    builder.Add(x => x.EmailTitle, "Welcome to Our Service")
           .Add(x => x.Recipient, "user@example.com")
           .Add(x => x.Body, "Thank you for joining us! We're glad to have you.");
});
  1. Render the Email: Use the RenderEmail method of the BlazorEmailRenderer to render the email template with the specified parameters. This method is asynchronous and will return the rendered HTML as a string.
var emailHtml = await emailRenderer.RenderEmail<EmailTemplate>(emailParams.Build());
  1. Send the Email: After rendering the email, you can proceed to send it through your email service or SMTP client.

Example Code

Here’s a complete example demonstrating the entire flow from building parameters to rendering and sending the email:

using System.Threading.Tasks;

public class EmailService
{
    private readonly BlazorEmailRenderer _emailRenderer;

    public EmailService(BlazorEmailRenderer emailRenderer)
    {
        _emailRenderer = emailRenderer;
    }

    public async Task<string> SendWelcomeEmail(string emailTitle, string recipient)
    {
        // Configure email parameters
        var emailParams = new EmailParameterCollectionBuilder<EmailTemplate>(builder =>
        {
            builder.Add(x => x.EmailTitle, emailTitle)
                   .Add(x => x.Recipient, recipient)
                   .Add(x => x.Body, "Thank you for joining us! We're glad to have you.");
        });

        // Render the email
        var emailHtml = await _emailRenderer.RenderEmail<EmailTemplate>(emailParams.Build());

        // (Optional) Send the email using your SMTP client or email service
        await SendEmailAsync(recipient, emailHtml);

        return emailHtml; // Return the rendered HTML if needed
    }

    private Task SendEmailAsync(string recipient, string emailHtml)
    {
        // Implementation for sending the email goes here.
        // This might involve using an SMTP client or an email sending service.
        return Task.CompletedTask;
    }
}