Mail.dat.Abstractions 10.2.2

dotnet add package Mail.dat.Abstractions --version 10.2.2
                    
NuGet\Install-Package Mail.dat.Abstractions -Version 10.2.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Mail.dat.Abstractions" Version="10.2.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Mail.dat.Abstractions" Version="10.2.2" />
                    
Directory.Packages.props
<PackageReference Include="Mail.dat.Abstractions" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Mail.dat.Abstractions --version 10.2.2
                    
#r "nuget: Mail.dat.Abstractions, 10.2.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Mail.dat.Abstractions@10.2.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Mail.dat.Abstractions&version=10.2.2
                    
Install as a Cake Addin
#tool nuget:?package=Mail.dat.Abstractions&version=10.2.2
                    
Install as a Cake Tool

.NET GitHub License Build and Test

NugetNuget

NugetNuget

NugetNuget

NugetNuget

NugetNuget

NugetNuget

Open Mail.dat

Open Mail.dat is an open-source .NET library ecosystem for managing Mail.dat files — the standardized postal format used by the United States Postal Service for tracking mail processing, postage, components, and postal services. The project provides NuGet libraries, a command-line tool, and utilities to read, write, and process Mail.dat data, with SQLite as the backing store.


Table of Contents


What Is Mail.dat?

Mail.dat is a file format standard developed and maintained by the United States Postal Service (USPS). It is the industry standard for the electronic exchange of mailing job information between mail owners, mail preparers, and the USPS. A Mail.dat job consists of a collection of fixed-width record files (.hdr, .seg, .mpu, .pdr, .pbc, etc.) that together describe every aspect of a mailing, including:

  • Postage and rate information
  • Container and tray planning
  • Piece-level barcode tracking
  • Postal routing and sorting details
  • Certificate of mailing data

Open Mail.dat provides a complete .NET solution for reading, writing, importing, and exporting these files.


Projects & NuGet Packages

The solution consists of nine NuGet library packages and one command-line application, all targeting .NET 10 at version 10.2.1.

Project NuGet Package Description
Mail.dat.Abstractions Open Mail.dat Abstractions Core interfaces and contracts shared across all libraries
Mail.dat.Models Open Mail.dat Models Entity Framework Core entity models and USPS lookup value classes
Mail.dat.Io Open Mail.dat IO Import and export implementation for Mail.dat files
Mail.dat.Json.Specification Open Mail.dat Specification JSON schema models for USPS specification versions
Mail.dat.BulkProcessor Open Mail.dat Bulk Processor High-speed bulk insert/update operations for large datasets
Mail.dat.Hosting Open Mail.dat Hosting Dependency injection and hosting configuration helpers
Mail.dat.ImportCommand (CLI only) CLI handler for the import command
Mail.dat.ExportCommand (CLI only) CLI handler for the export command
Mail.dat.BuildCommand (CLI only) CLI handler for the build code-generation command
MaildatCmd (executable) Main command-line application entry point

All packages are published with symbol packages (.snupkg) for source-level debugging.


Architecture Overview

The project follows a layered, modular, async-first architecture:

┌──────────────────────────────────────────────────┐
│  MaildatCmd  (CLI Entry Point)                    │
├──────────────────────────────────────────────────┤
│  ImportCommand / ExportCommand / BuildCommand     │  ← Command handlers (Spectre.Console progress UI)
├──────────────────────────────────────────────────┤
│  Mail.dat.Io                                      │  ← Core I/O (MaildatImport, MaildatExport)
├──────────────────────────────────────────────────┤
│  Mail.dat.Models                                  │  ← EF Core entities + 155 USPS lookup value classes
│  Mail.dat.BulkProcessor                           │  ← High-performance bulk insert/update
├──────────────────────────────────────────────────┤
│  Mail.dat.Json.Specification                      │  ← USPS spec schemas (versions 23-1 → 26-1)
├──────────────────────────────────────────────────┤
│  Mail.dat.Abstractions                            │  ← Interfaces: IMaildatFile, IImportOptions, …
│  Mail.dat.Hosting                                 │  ← DI registration helpers
└──────────────────────────────────────────────────┘

Design Patterns:

Pattern Usage
Abstraction-First All layers depend on interfaces, not concrete classes
Dependency Injection Microsoft.Extensions.Hosting throughout
Repository Pattern Diamond.Core.Repository for data access abstraction
Command Pattern CLI commands as discrete, isolated handlers
Factory Pattern DbContextFactory for EF Core context creation

Repository Structure

Open-Mail.dat/
├── Src/
│   ├── Mail.dat.Abstractions/         # Core interfaces & contracts
│   ├── Mail.dat.Models/               # EF Core entity models + lookup values
│   ├── Mail.dat.Io/                   # Import/export implementation
│   ├── Mail.dat.Json.Specification/   # JSON schema models (USPS spec versions)
│   ├── Mail.dat.ImportCommand/        # CLI import command handler
│   ├── Mail.dat.ExportCommand/        # CLI export command handler
│   ├── Mail.dat.BuildCommand/         # CLI code generation command
│   ├── Mail.dat.BulkProcessor/        # High-speed bulk EF Core operations
│   ├── Mail.dat.Hosting/              # DI and hosting configuration
│   └── MaildatCmd/                    # Main CLI application entry point
├── ARCHITECTURE.md                    # Detailed architecture documentation
├── LICENSE.md                         # LGPL-3.0 license
├── LICENSE-ADDENDUM.md                # Attribution and modification addendum
└── README.md

Three Visual Studio solution files are included:

  • Open Mail.dat Solution.sln — Standard solution
  • Open Mail.dat Solution ALL.sln — All projects
  • Open Mail.dat Solution - BUILD ONLY.sln — Code generation focused

Getting Started

Installation via NuGet

For library integration in your own .NET project, add the packages that fit your needs:

# Core I/O (import and export Mail.dat files)
dotnet add package "Open Mail.dat IO"

# Entity models for accessing imported data
dotnet add package "Open Mail.dat Models"

# Hosting/DI helpers for ASP.NET Core or Worker Service applications
dotnet add package "Open Mail.dat Hosting"

# Bulk operations for large datasets
dotnet add package "Open Mail.dat Bulk Processor"

All packages target net10.0 and require .NET 10 or later.

Building from Source

  1. Clone the repository:

    git clone https://github.com/porrey/Open-Mail.dat.git
    cd Open-Mail.dat
    
  2. Open one of the solution files in Visual Studio 2022 (or later), or build from the command line:

    dotnet build "Open Mail.dat Solution.sln"
    
  3. NuGet packages are automatically generated on build (GeneratePackageOnBuild=True). Symbol packages (.snupkg) are also produced.

  4. To publish packages manually, run Src/NuGet.Publish.cmd from a Visual Studio Developer Command Prompt. The script prompts for your NuGet API key and publishes all .nupkg and .snupkg files to nuget.org.


Usage

Command-Line Tool (MaildatCmd)

MaildatCmd is a standalone .NET console application that exposes three commands.

import — Import a Mail.dat file into a SQLite database
MaildatCmd import --source-file-path <path-to-maildat-or-zip> --target-file-path <path-to-sqlite-db>
Option Short Type Required Default Description
--source-file-path -s FileInfo Yes Path to the Mail.dat file or ZIP archive
--target-file-path -t FileInfo Yes Path for the output SQLite database
--skip-pbc -p bool No false Skip the PBC (Piece Barcode) file
--favor-memory-over-performance -f bool No false Use less memory (~40% slower)
--max-records-in-memory -m int No 10000 Records batched before writing to the database (used with -f)

Example:

MaildatCmd import -s "C:\mail\job12345.zip" -t "C:\databases\job12345.db"

A rich progress display (progress bar, percentage, elapsed time, line counter) is shown during import. Any validation errors are grouped by file and reported at the end.


export — Export a Mail.dat file from a SQLite database
MaildatCmd export --source-file-path <path-to-sqlite-db> --target-file-path <path-to-maildat-or-zip>
Option Short Type Required Description
--source-file-path -s FileInfo Yes Path to the source SQLite database
--target-file-path -t FileInfo Yes Path for the output Mail.dat file (or .zip)

If the target file has a .zip extension, the output is automatically compressed.

Example:

MaildatCmd export -s "C:\databases\job12345.db" -t "C:\mail\job12345-out.zip"

build — Generate model code from a USPS JSON specification
MaildatCmd build --specification-files <comma-delimited-paths> --models-directory <path> --hosting-directory <path>
Option Short Type Required Description
--specification-files -s string Yes Comma-delimited list of Mail.dat JSON specification file paths
--models-directory -c DirectoryInfo Yes Path to the Models project directory
--hosting-directory -h DirectoryInfo Yes Path to the Hosting project directory

This command reads the USPS Mail.dat JSON specifications and auto-generates all EF Core entity classes, interface definitions, repository classes, the MaildatContext DbContext, and DI registration code.


Programmatic Usage

Registering Services with Dependency Injection

Use the AddOpenMaildat<TDatabaseContext>() extension method in your service configuration:

using Mail.dat.Hosting;
using Mail.dat.Models;

// In your IServiceCollection setup (e.g., in a Worker Service or ASP.NET Core app):
services.AddOpenMaildat<MaildatContext>();

This single call registers:

  • All 29 entity repositories and factories (IRepository<IHdr>, IRepository<ISeg>, etc.)
  • IMaildatImport<TDatabaseContext>MaildatImport<TDatabaseContext>
  • IMaildatExportMaildatExport
  • IBulkProcessorFactoryBulkProcessorFactory
Importing a Mail.dat File
using Mail.dat.Io;
using Mail.dat.Models;

// Create the import options
ImportOptions options = new()
{
    SourceFile          = MaildatFile.Create("/path/to/job.zip"),
    TargetFile          = MaildatFile.Create("/path/to/output.db"),
    TemporaryDirectory  = Path.GetTempPath(),
    CancellationToken   = CancellationToken.None
};

// Create an import instance (with optional progress callback)
IMaildatImport<MaildatContext> importer = MaildatImport<MaildatContext>.Create(
    new DefaultDbContextFactory(),
    async (message) => Console.WriteLine(message.StatusMessage)
);

// Run the import
(bool success, MaildatContext context) = await importer.ImportAsync(options);
Exporting a Mail.dat File
using Mail.dat.Io;

// Create the export options
ExportOptions options = new()
{
    SourceFile      = MaildatFile.Create("/path/to/output.db"),
    TargetFile      = MaildatFile.Create("/path/to/export.zip"),
    TaregtVersion   = "25-1",  // Note: property name has a typo in the interface
    Encoding        = System.Text.Encoding.UTF8,
    LineTerminator  = Environment.NewLine
};

// Create an export instance (with optional progress callback)
IMaildatExport exporter = MaildatExport.Create(
    async (message) => Console.WriteLine(message.StatusMessage)
);

// Run the export
bool success = await exporter.ExportAsync(options.TargetFile, options);

Key Components

Mail.dat.Abstractions

Defines the core interfaces and shared types used across all libraries.

Key Interfaces:

Interface Description
IMaildatEntity Base interface for all Mail.dat entity models; includes ImportDataAsync and ExportData methods
IError Interface for import/export error records
ILoadError Represents a single field-level error encountered during import

Attributes:

Attribute Description
[MaildatFile] Marks a class as representing a specific Mail.dat file type (e.g., HDR, SEG)
[MaildatField] Annotates a property with its fixed-width field definition (position, length, type)
[MaildatVersions] Specifies which USPS specification versions a model or field applies to

Mail.dat.Models

Contains the 29 EF Core entity classes (auto-generated from USPS JSON specifications) and over 155 USPS lookup value classes.

Entity Classes:

Entity File Ext Description
Hdr .hdr Header Record
Seg .seg Segment Record
Mpu .mpu Mail Processing Unit
Mcr .mcr Mail Carrier Route
Mpa .mpa Mail Piece Address
Cpt .cpt Component
Ccr .ccr Container Summary Record
Csm .csm Container Summary
Cqt .cqt Container Quantity
Pqt .pqt Package Quantity
Wsr .wsr Walk Sequence Record
Snr .snr Seed Name Record
Icr .icr Intelligent Mail Range Record
Pdr .pdr Piece Detail Record
Pbc .pbc Piece Barcode Record
Sfr .sfr Special Fees/Charges Record
Sfb .sfb Special Fees/Charges Bulk
Par .par Postage Adjustment Record
Oci .oci Original Container Information
Upa .upa Un-deliverable Piece Address
Epd .epd Electronic Package Detail
Rmr .rmr Referenceable Mail Record
Rmb .rmb Referenceable Mail Bulk
Rms .rms Referenceable Mail Summary
Chr .chr Certificate of Mailing Header
Cbr .cbr Certificate of Mailing Bulk
Cdr .cdr Certificate of Mailing Detail
Cfr .cfr Certificate of Mailing Form
Tsr .tsr Transportation Summary Record

All entities inherit from MaildatEntity and are stored in a [Maildat] schema within the SQLite database.


Mail.dat.Io

Provides the core import and export engine.

MaildatImport<TDatabaseContext>
Reads Mail.dat files (or ZIP archives) and imports all records into a SQLite database in parallel, using transactions for consistency and EFCore.BulkExtensions for throughput.

MaildatExport
Reads records from a SQLite database and writes them as fixed-width Mail.dat files, optionally compressing the output into a ZIP archive.

IMaildatFile / MaildatFile
Represents a Mail.dat file path (ZIP or unzipped). Provides UnzipAsync, ZipAsync, and GetFile(extension) helpers.

Key interfaces in this project:

Interface Description
IMaildatImport<TContext> Contract for importing Mail.dat into a database context
IMaildatExport Contract for exporting Mail.dat from a database context
IMaildatFile Represents a Mail.dat file (zipped or unzipped)
IImportOptions Import configuration (source/target paths, temp directory, memory settings)
IExportOptions Export configuration (target version, encoding, line terminator)
IProgressMessage Progress reporting message contract

Mail.dat.Hosting

Provides the AddOpenMaildat<TDatabaseContext>() extension method for IServiceCollection. This is a convenience wrapper that chains three registration calls:

  • AddOpenMaildatModels() — registers all entity repositories and factories
  • AddOpenMaildatIo<TDatabaseContext>() — registers import/export services
  • AddOpenMaildatBulkProcessor<TDatabaseContext>() — registers the bulk processor factory

Mail.dat.BulkProcessor

Wraps EFCore.BulkExtensions.Sqlite to provide high-performance bulk insert and update operations. Used internally by MaildatImport when importing large Mail.dat files to dramatically reduce database write time compared to standard EF Core SaveChanges.


Mail.dat.Json.Specification

Contains the official USPS Mail.dat JSON specification files and the C# models for deserializing them. These specifications define every file type, record layout, and field definition for each supported version. The build command uses these specifications to auto-generate all entity models and repository code.


USPS Spec Versions Supported

Version Status
23-1 Supported
24-1 Supported
25-1 Supported
26-1 Supported (latest)

Versions are detected automatically during import from the Mail.dat header record. Entity models and field attributes are version-aware — a single entity class can support multiple spec versions with different field layouts via [MaildatVersions] attributes.


Key Technologies

Category Technology Version Purpose
Language/Runtime C# / .NET 10 Async-first, implicit usings
ORM Entity Framework Core 10.0.5 SQLite database access
Database SQLite Stores imported Mail.dat records
Bulk Operations EFCore.BulkExtensions.Sqlite 10.0.1 High-performance large dataset inserts
CLI Framework Diamond.Core.CommandLine 10.1.0 Extensible command-line commands
DI/Hosting Microsoft.Extensions.Hosting 10.0.5 Dependency injection
Logging Serilog Structured file/console logging
JSON Newtonsoft.Json 13.0.4 Deserializing USPS spec files
Console UI Spectre.Console 0.54.0 Progress bars, rich CLI formatting
Compression SharpZipLib 1.4.2 ZIP file handling
Text Utilities Humanizer.Core 3.0.10 Human-readable output

Logging

MaildatCmd uses Serilog with a rolling-file sink. Logs are written to:

%TEMP%/MaildatCmd/Logs/MaildatCmd-Log.txt

Configuration is in MaildatCmd/appsettings.json. Separate appsettings.Development.json and appsettings.Production.json files allow environment-specific overrides.


License

This project is licensed under the GNU Lesser General Public License v3.0 (LGPL-3.0), with an additional Attribution and Public Use Addendum.

By using this library you agree to both the LGPL-3.0 terms and the addendum, which requires:

  1. Public Attribution — Applications using this library in any public or commercial product must include a visible acknowledgment:

    "This software uses Open Mail.dat, available under the LGPL-3.0 license with additional terms. See https://github.com/Open-Mail-dat for details."

  2. Modification Disclosure — Any modifications to the library itself must be published in a public code repository and include a prominent notice of the change date and author.

See LICENSE.md and LICENSE-ADDENDUM.md for the full license text.


Attribution

This software uses Open Mail.dat, available under the LGPL-3.0 license with additional terms.
See LICENSE-ADDENDUM.md for details.


Author

Daniel M Porrey
Copyright © 2025–2026 Daniel Porrey, Open Mail.dat. All rights reserved.
GitHub: https://github.com/porrey

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Mail.dat.Abstractions:

Package Downloads
Mail.dat.Models

Set of Entity Framwork compatible models intended for reading and writing Mail.dat data aginst a database such as SQLite.

Mail.dat.BulkProcessor

Adds support for processing large bulk transactions.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
10.2.2 0 4/4/2026
10.2.1 115 3/23/2026
10.2.0 119 3/16/2026
10.1.0 117 2/19/2026
0.0.10 197 11/15/2025
0.0.8 319 11/12/2025
0.0.7 225 10/29/2025
0.0.5 161 8/22/2025
0.0.4 217 8/21/2025
0.0.3 195 8/21/2025
0.0.2 221 8/14/2025
0.0.1 221 8/12/2025