MapiContact.FromVCard

Hello,
Just checking how MapiContact.FromVCard will handle the code if source vcf is multi contacts?
Thanks.

@australian.dev.nerds

The MapiContact.FromVCard method is designed to load a single contact from a VCard (VCF) file. If the source VCF file contains multiple contacts, the method will only process the first contact it encounters and ignore the rest.

To handle multiple contacts from a VCF file, you would need to read the file manually and parse each contact entry. You can use the VCardContact.Load method to load the entire VCF file, which will allow you to access all contacts within it. Here’s a basic example of how you might implement this:

using Aspose.Email.Mapi;
using Aspose.Email.PersonalInfo.VCard;
using System;
using System.Collections.Generic;

namespace Aspose.Email.Examples.CSharp.Email
{
    class LoadingMultipleContactsFromVCard
    {
        public static void Run()
        {
            string dataDir = "path_to_your_vcf_file.vcf";
            List<MapiContact> contacts = new List<MapiContact>();

            // Load all contacts from the VCard file
            var vCardContacts = VCardContact.Load(dataDir);
            foreach (var vCardContact in vCardContacts)
            {
                MapiContact contact = MapiContact.FromVCard(vCardContact.ToString());
                contacts.Add(contact);
                Console.WriteLine("Loaded contact: " + contact.NameInfo.DisplayName);
            }
        }
    }
}

In this example, you would replace "path_to_your_vcf_file.vcf" with the actual path to your VCF file. The code loads all contacts from the VCF file and processes each one individually, allowing you to handle multiple contacts effectively.

For more detailed examples and information, you can refer to the official Aspose documentation and examples available on their GitHub repository.

If you have further questions or need additional assistance, feel free to ask!

1 Like

Hi,
Can you please be kind and consider using vCardLoadOptions with MapiContact.FromVCard?
MapiContact.FromVCard (inFile, vCardLoadOptions)
VCardLoadOptions with PreferredEncoding property is the key, now, have to use:

Select Case encEnabled
Case False
MyMapiContact = MapiContact.FromVCard(path)
Case True
MyMapiContact = MapiContact.FromVCard(path, GetDecoder)
End Select

Thanks :slight_smile:

@australian.dev.nerds,

Just to better understand your scenario - could you please share why you prefer using the overload with VCardLoadOptions for MapiContact.FromVCard? Are you encountering any issues that the overload with Encoding doesn’t handle well?
Yes, we’ll consider using vCardLoadOptions with MapiContact.FromVCard.

1 Like

Thanks indeed,
Yep, currently the usage would be like this:

Select Case encEnabled
 Case False
  MyMapiContact = MapiContact.FromVCard(path)
 Case True
  MyMapiContact = MapiContact.FromVCard(path, GetDecoder)
End Select

If implemented, it will be:

MyMapiContact = MapiContact.FromVCard(path, GlobalvCardLoadOptions)

And will handle the select case inside a custom GlobalvCardLoadOptions function, not critical but will surely help for smaller/nicer code.

@australian.dev.nerds,
We have created a new enhancement ticket in our internal issue tracking system to add an overload of the MapiContact.FromVCard method that accepts a VCardLoadOptions parameter.

Issue ID(s): EMAILNET-41555