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
base64ContentstringThe attachment content as a base64-encoded string.
fileNamestringThe file name for the attachment (used for content type inference and display).
contentTypestringOptional explicit content type. When
null, inferred fromfileName.
Returns
- Attachment
A new Attachment backed by a MemoryStream.
Exceptions
- ArgumentNullException
base64Contentis null.- ArgumentNullException
fileNameis null.- FormatException
base64Contentis 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
contentbyte[]The attachment content as a byte array.
fileNamestringThe file name for the attachment (used for content type inference and display).
contentTypestringOptional explicit content type. When
null, inferred fromfileName.
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
contentis null.- ArgumentNullException
fileNameis 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
contentStreamThe stream containing the attachment content.
fileNamestringThe file name for the attachment (used for content type inference and display).
contentTypestringOptional explicit content type. When
null, inferred fromfileName.
Returns
- Attachment
A new Attachment backed by a MemoryStream.
Exceptions
- ArgumentNullException
contentis null.- ArgumentNullException
fileNameis 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
fileNamestringThe file name to infer the content type from.
Returns
- string
The inferred MIME content type string.
Exceptions
- ArgumentNullException
fileNameis 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
extensionstringThe file extension (with leading dot, e.g.
".heic").contentTypestringThe 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
extensionis null.- ArgumentNullException
contentTypeis null.- ArgumentException
extensionis 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
extensionstringThe file extension (with leading dot).
contentTypestringWhen this method returns, contains the registered content type if found; otherwise,
null.
Returns
- bool
trueif a content type was registered for the extension; otherwise,false.
Exceptions
- ArgumentNullException
extensionis null.