Class InlineHtmlBuilder

Namespace
Wolfgang.Extensions.Mail
Assembly
Wolfgang.Extensions.Mail.dll

A builder for creating HTML AlternateView instances with inline embedded images. Manages LinkedResource creation and Content-ID wiring automatically.

public sealed class InlineHtmlBuilder
Inheritance
InlineHtmlBuilder
Inherited Members

Examples

using var message = new MailMessage("from@example.com", "to@example.com");

var builder = new InlineHtmlBuilder()
    .Html("<h1>Report</h1><img src='cid:{0}' />")
    .EmbedImage("chart.png");

message.AlternateViews.Add(builder.Build());

Methods

Build()

Builds the AlternateView with all embedded images wired up. The HTML template placeholders ({0}, {1}, etc.) are replaced with the generated Content-IDs.

public AlternateView Build()

Returns

AlternateView

An AlternateView containing the HTML and all linked resources.

Exceptions

InvalidOperationException

No HTML template has been set via Html(string).

EmbedImage(byte[], string, string?)

Embeds an image from a byte array as a LinkedResource.

public InlineHtmlBuilder EmbedImage(byte[] imageBytes, string fileName, string? contentType = null)

Parameters

imageBytes byte[]

The image content as a byte array.

fileName string

A file name used for content type inference (e.g., "chart.png").

contentType string

Optional explicit content type. When null, inferred from fileName.

Returns

InlineHtmlBuilder

This builder instance for chaining.

Exceptions

ArgumentNullException

imageBytes is null.

ArgumentNullException

fileName is null.

EmbedImage(Stream, string, string?)

Embeds an image from a Stream as a LinkedResource. The stream content is copied; the original stream is not modified.

public InlineHtmlBuilder EmbedImage(Stream imageStream, string fileName, string? contentType = null)

Parameters

imageStream Stream

The stream containing the image data.

fileName string

A file name used for content type inference (e.g., "logo.png").

contentType string

Optional explicit content type. When null, inferred from fileName.

Returns

InlineHtmlBuilder

This builder instance for chaining.

Exceptions

ArgumentNullException

imageStream is null.

ArgumentNullException

fileName is null.

EmbedImage(string)

Embeds an image from a file path as a LinkedResource. The content type is inferred from the file extension.

public InlineHtmlBuilder EmbedImage(string filePath)

Parameters

filePath string

The path to the image file.

Returns

InlineHtmlBuilder

This builder instance for chaining.

Exceptions

ArgumentNullException

filePath is null.

Html(string)

Sets the HTML template. Use {0}, {1}, etc. as placeholders for embedded image Content-IDs, which are filled in order of EmbedImage(string) calls.

public InlineHtmlBuilder Html(string htmlTemplate)

Parameters

htmlTemplate string

The HTML template string with {n} placeholders for inline images.

Returns

InlineHtmlBuilder

This builder instance for chaining.

Exceptions

ArgumentNullException

htmlTemplate is null.