Manage MS Contacts

Efficient contact management is vital for professionals and businesses. It ensures that you can easily find and connect with the right people when you need them. Whether you’re scheduling meetings, sending emails, or sharing documents, having organized and up-to-date contacts can significantly improve your productivity.

MSG files are a commonly used format for storing email messages, contacts, and other items. Extracting contact information from Outlook MSG files programmatically can be a crucial task for various scenarios, such as data migration or automated data processing. In this article, we will explore how to load, extract and save contact information from Outlook MSG files.

Get Started with the Advanced C# API

To work with MSG files, we will use Aspose.Email for .NET. It is a powerful and versatile email processing and management library that provides developers with a comprehensive set of tools and functionalities to work with email messages, tasks, calendars, contacts, and various email-related tasks within .NET applications. So, before we begin, ensure that you have Aspose.Email installed and referenced in your project. If you haven’t done so already, you can download its DLL or install it from NuGet.

Loading Contact from MSG Files

To extract contact information from an MSG file, we need to load the file into a MapiMessage object using Aspose.Email’s MapiMessage.Load method. You can load a contact into a MapiMessage, and handle it like a normal MAPI message: read properties, modify, save, add to storage, send. The following code sample will show you how to load and process Outlook contacts:

  1. Load an existing contact item in a MSG file using the MapiMessage.Load method. This method reads the contents of the specified MSG file and creates a MapiMessage object representing the email message.
  2. Set HTML body content using the SetBodyContent method to replace the content.
  3. Specify the BodyContentType.Html parameter which indicates that the provided content is in HTML format. This ensures that the content is correctly interpreted as HTML when viewed by email clients.
  4. Save the modified MapiMessage object back to an MSG file using the Save method.
var msg = MapiMessage.Load("Lynna M. Dyer.msg");
msg.SetBodyContent("<!DOCTYPE html><html><body><h1>Lynna M. Dyer</h1></body></html>", BodyContentType.Html);
msg.Save("Lynna M. Dyer.msg");

Extracting Contact Details

Once we have loaded the MSG file, we can extract contact details using MapiContact class. The MapiContact provides convenient properties to access specific contact fields. The provided code sample demonstrates how to extract and display contact information from an MSG email.

  1. Start by checking if the MSG is a contact item. Use the SupportedType property which indicates the type of Outlook item.
  2. Convert the msg object to a MapiContact object. This conversion allows access to specialized properties and methods specific to contact items.
  3. Retrieve and display contact information. In this example, we access the DisplayName, Email, CompanyName, and Title properties of the MapiContact object, which represent the contact’s name, email address, company name, and job title respectively. You can access other properties of the MapiContact object as per your requirements.
if (msg.SupportedType == MapiItemType.Contact)
{
    var contact = (MapiContact)msg.ToMapiMessageItem();

    Console.WriteLine(contact.NameInfo.DisplayName);
    Console.WriteLine(contact.ElectronicAddresses.Email1);
    Console.WriteLine(contact.ProfessionalInfo.CompanyName);
    Console.WriteLine(contact.ProfessionalInfo.Title);
}

Saving Contact Photo

If the MSG file contains a photo of the contact, we can save it to a file using the following code:

  1. Check if the contact has a photo. This is done by evaluating whether the contact.Photo property is not null.
  2. If a photo is present, the code proceeds to save it as a file.
if (contact.Photo != null)
{
    System.IO.File.WriteAllBytes($"{contact.NameInfo.DisplayName}.{MapiContactPhotoImageFormat.Jpeg.ToString()}", contact.Photo.Data);
}

Saving Contact to VCF

Additionally, it is often necessary to convert the contact from MSG to a more universal format like VCARD. A VCard (Virtual Contact File) is a standard file format used to store contact information. The following code sample demonstrates how to save a contact item represented by the contact object as a VCard file:

The code uses the Save method of the contact object to initiate the process of saving the contact as a VCard.

contact.Save("Lynna M. Dyer.vcf", ContactSaveFormat.VCard);

Load contact from VCF

You can also load an existing VCARD file into a MapiContact object. The following code sample demonstrates how to load contact information from a VCard and create a MapiContact object:

  1. The code starts by using the MapiContact.FromVCard method to load contact information from a VCard.
  2. The contact variable holds this newly created MapiContact object, which can be used to access and manipulate the contact’s details programmatically.
var contact = MapiContact.FromVCard("Lynna M. Dyer.vcf");

Conclusion

In this article, we explored how to extract contact information from Outlook MSG files programmatically using Aspose.Email. We walked through the process of loading an MSG file, extracting contact details using the MapiContact class, and saving the contact in different formats. Aspose.Email simplifies the process of working with MSG files and provides a wide range of functionality for handling email data.

Remember to modify the file paths and adapt the code to suit your specific requirements. Aspose.Email’s comprehensive documentation and helpful community forums will further assist you in leveraging its capabilities to extract contact information effectively.

See Also