Recursive AI

Convert a VCF to an Outlook contact MSG in C#

OneMail is a free, open-source email library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Read the zip’s contact.vcf and write it as an Outlook contact .msg. The converter also lists any vCard values that didn’t fit. Outlook doesn’t need to be installed.

Terminal
dotnet add package RecursiveAI.OneMail
dotnet add package RecursiveAI.OneMail.Calendar

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using RecursiveAI.OneMail.Calendar;
using RecursiveAI.OneMail.Calendar.VCard;
using RecursiveAI.OneMail.Conversion.OxVCard;
using RecursiveAI.OneMail.Msg;
 
var bytes = File.ReadAllBytes("contact.vcf");
var doc = ContentDocument.Parse(bytes);
var card = VCardDocument.Cards(doc).Single();
var result = new VCardToContactConverter()
    .Convert(card.Component);
File.WriteAllBytes("contact.msg",
    MsgWriter.ToArray(result.Value));
Console.WriteLine("Wrote contact.msg");
foreach (var loss in result.Report)
    if (loss.IsContentLoss)
        Console.WriteLine($"{loss.Name}: {loss.Code}");

Paste the code above into Program.cs of a new dotnet new console project and dotnet run, or unzip the example and dotnet run.

Text
Wrote contact.msg
EMAIL: AdditionalValueNotMapped

The zip’s contact.vcf is a synthetic vCard with four email addresses:

Text
BEGIN:VCARD
VERSION:3.0
FN:Morgan Lee
N:Lee;Morgan;;;
EMAIL:morgan@example.com
EMAIL:mlee@example.org
EMAIL:ml@example.net
EMAIL:fourth@example.com
END:VCARD

This example expects exactly one card. Single() throws InvalidOperationException if the file contains none or more than one.

Variations

Require no content loss. To reject a card rather than write a contact missing some values, add this check before writing the file:

C#
if (result.Report.HasContentLoss)
    throw new InvalidDataException(
        "Some vCard values did not carry over.");

The short answer

Parse the .vcf with ContentDocument, then pass the card’s component to VCardToContactConverter. Write result.Value with MsgWriter and read result.Report for fields that were dropped or approximated. The file is an IPM.Contact Outlook item, not an email message, and the conversion does not require Outlook to be installed.

How do I import a VCF contact into Outlook?

If your goal is to add a card to your address book by hand, Microsoft’s Outlook instructions cover opening or saving a vCard directly. Use the C# conversion when your application needs to produce a standalone .msg contact file rather than add a contact to a user’s address book. The example writes a local file; it doesn’t connect to a mailbox or insert the contact into a Contacts folder.

What if a VCF file contains multiple contacts?

VCardDocument.Cards enumerates the vCards in the document. This example uses Single() so that a file with multiple contacts can’t silently become an MSG with just the first contact. For a batch export, iterate the cards and convert each component to its own numbered .msg, pairing each report with the file it describes.

Which vCard fields carry over to an Outlook contact?

The contact mapping assigns values to specific Outlook properties. The first three email addresses go into separate email slots; the fourth is reported as AdditionalValueNotMapped. Some values don’t fit for other reasons: a birthday without a year is reported as PartialDate, and a photo given only as a URI is reported as PhotoNotInline. The converter also records bookkeeping such as the vCard version. Filtering on IsContentLoss keeps those bookkeeping entries separate from values that changed or didn’t make it into the file.

All onemail recipes