Class AttachmentFactory

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

Provides factory methods for creating Attachment objects from common sources with automatic content type inference from file extensions.

public static class AttachmentFactory
Inheritance
AttachmentFactory
Inherited Members

Methods

FromBase64(string, string, string?)

Creates an Attachment from a base64-encoded string with automatic content type inference.

public static Attachment FromBase64(string base64Content, string fileName, string? contentType = null)

Parameters

base64Content string

The attachment content as a base64-encoded string.

fileName string

The file name for the attachment (used for content type inference and display).

contentType string

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

Returns

Attachment

A new Attachment backed by a MemoryStream.

Exceptions

ArgumentNullException

base64Content is null.

ArgumentNullException

fileName is null.

FormatException

base64Content is not valid base64.

FromBytes(byte[], string, string?)

Creates an Attachment from a byte array with automatic content type inference.

public static Attachment FromBytes(byte[] content, string fileName, string? contentType = null)

Parameters

content byte[]

The attachment content as a byte array.

fileName string

The file name for the attachment (used for content type inference and display).

contentType string

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

Returns

Attachment

A new Attachment backed by a MemoryStream.

Examples

byte[] pdfBytes = await File.ReadAllBytesAsync("report.pdf");
var attachment = AttachmentFactory.FromBytes(pdfBytes, "report.pdf");

Exceptions

ArgumentNullException

content is null.

ArgumentNullException

fileName is null.

FromStream(Stream, string, string?)

Creates an Attachment from a Stream with automatic content type inference. The stream content is copied to a new MemoryStream; the original stream is not modified.

public static Attachment FromStream(Stream content, string fileName, string? contentType = null)

Parameters

content Stream

The stream containing the attachment content.

fileName string

The file name for the attachment (used for content type inference and display).

contentType string

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

Returns

Attachment

A new Attachment backed by a MemoryStream.

Exceptions

ArgumentNullException

content is null.

ArgumentNullException

fileName is null.

InferContentType(string)

Infers the MIME content type from a file name extension. Returns application/octet-stream for unknown extensions.

public static string InferContentType(string fileName)

Parameters

fileName string

The file name to infer the content type from.

Returns

string

The inferred MIME content type string.

Exceptions

ArgumentNullException

fileName is null.

RegisterContentType(string, string)

Registers or overrides a custom content type mapping for a file extension. Extension matching is case-insensitive. Registrations persist for the lifetime of the process.

public static void RegisterContentType(string extension, string contentType)

Parameters

extension string

The file extension (with leading dot, e.g. ".heic").

contentType string

The MIME content type to associate with the extension.

Examples

AttachmentFactory.RegisterContentType(".heic", "image/heic");
byte[] data = await File.ReadAllBytesAsync("photo.heic");
var attachment = AttachmentFactory.FromBytes(data, "photo.heic");
// attachment.ContentType.MediaType == "image/heic"

Remarks

The registry is a process-wide static map shared by every caller in the process. Registration is thread-safe, and concurrent registrations of the same extension resolve to last-write-wins — there is no notification when a mapping is overwritten.

Exceptions

ArgumentNullException

extension is null.

ArgumentNullException

contentType is null.

ArgumentException

extension is empty or does not start with ".".

TryGetRegisteredContentType(string, out string?)

Attempts to retrieve the registered content type for a file extension.

public static bool TryGetRegisteredContentType(string extension, out string? contentType)

Parameters

extension string

The file extension (with leading dot).

contentType string

When this method returns, contains the registered content type if found; otherwise, null.

Returns

bool

true if a content type was registered for the extension; otherwise, false.

Exceptions

ArgumentNullException

extension is null.