dotnet add package RecursiveAI.OneContactRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneContact; var seen = new HashSet<string>(); foreach (var row in new[] { "Ada@example.org", "ada@EXAMPLE.ORG" }) if (seen.Add(EmailAddress.Parse(row) .GetKey(KeyVersion.V1))) Console.WriteLine(row);
The example is the full program below: unzip it and dotnet run. To run only the code above, paste it into Program.cs of a new dotnet new console project and dotnet run.
Prints Ada@example.org once, keeping the first spelling.
The full program
This example also shows provider-specific aliases, an unrelated domain with a plus tag, and an invalid row.
using RecursiveAI.OneContact; string[] rows = [ "Ada <Ada.Lovelace+news@gmail.com>", "adalovelace@googlemail.com", "ada+news@example.org", "ada@example.org", "not-an-address" ]; var seen = new HashSet<string>(StringComparer.Ordinal); foreach (string row in rows) { if (!EmailAddress.TryParse(row, out var email)) { Console.WriteLine($"Invalid: {row}"); continue; } string key = email.GetKey(KeyVersion.V1); string status = seen.Add(key) ? "Keep" : "Duplicate"; Console.WriteLine($"{status}: {row} -> {key}"); }
dotnet run prints:
Keep: Ada <Ada.Lovelace+news@gmail.com> -> adalovelace@gmail.com Duplicate: adalovelace@googlemail.com -> adalovelace@gmail.com Keep: ada+news@example.org -> ada+news@example.org Keep: ada@example.org -> ada@example.org Invalid: not-an-address
Parse throws ContactParseException on malformed input; use TryParse to set those rows aside without stopping the import.
Variations
Keep the comparison key separate from the sending address. Canonical omits the display name but retains the local part for mail routing.
using RecursiveAI.OneContact; var email = EmailAddress.Parse("Ada <Ada@EXAMPLE.ORG>"); Console.WriteLine(email.Canonical); Console.WriteLine(email.GetKey(KeyVersion.V1));
Ada@example.org ada@example.org
Related recipes
The short answer
Parse each mailbox with EmailAddress, take GetKey(KeyVersion.V1) and keep the first row for each distinct key. HashSet.Add returns false for a key you have already seen. Store the original or canonical address separately: the comparison key is not the address you should send to.
Are email addresses case-sensitive when removing duplicates?
Case is not the same on both sides of @. RFC 5321 §2.4 says the domain is case-insensitive but the local part must be treated as case-sensitive for SMTP, even though relying on local-part case is discouraged. OneContact preserves that case in its canonical address but folds it in the comparison key. Two keys can therefore match without proving that two delivery addresses are interchangeable.
The domain in both forms is lowercase and uses the ASCII form for internationalized domains. The key also composes non-ASCII local parts to Unicode NFC and removes wrapping quotes when the unquoted text is a valid dot-atom, such as "ada" becoming ada. See the V1 key rules for the order of these steps.
Should I strip plus tags before deduplicating email?
Not across every domain. V1 drops the first + and everything after it for gmail.com, googlemail.com, outlook.com, hotmail.com, live.com, and fastmail.com, provided the part before @ isn’t left empty. The first two inputs in the full program match because V1 also removes dots for gmail.com and googlemail.com and maps both to gmail.com. The example.org rows keep distinct keys: OneContact has no provider rule for that domain.
A key match makes a useful review candidate when merging records with conflicting information. It does not prove that two rows belong to the same person, that a mailbox exists, or that both spellings accept mail. Keep the address the user supplied so you can decide what to do with the match.
How do I prevent duplicate email entries in a database?
An in-memory set only covers one run. To catch duplicates across imports, persist the version and key with the record. If your application rejects duplicate keys, use a unique constraint on that pair with exact comparison rather than relying on a separate “does it exist?” read before inserting. If you retain duplicates for review, index the pair without making it unique.
Pinning V1 matters for stored keys. OneContact documents that existing key versions keep their output; rule changes use a new version. Don’t mix keys from different versions in one comparison without rebuilding them from the retained addresses. The key itself contains mailbox text, not an anonymized identifier.