Working with Outlook Contacts

Creating, Saving and Reading Contacts

Like MapiMessage, Aspose.Email allows you to create Outlook contacts. The MapiContact class provides all the contact related properties required to create an Outlook contact. This article shows how to create, save and read an Outlook contact using the MapiContact class.

Create and Save Outlook Contact

To create a contact and save it to disc:

  1. Instantiate a new object of the MapiContact class.
  2. Enter contact property information.
  3. Add photo data (if any).
  4. Save the contact as MSG or VCard format.

The following code snippet shows you how to create and save outlook contact.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// The path to the File directory.
string dataDir = RunExamples.GetDataDir_Outlook();
MapiContact contact = new MapiContact();
contact.NameInfo = new MapiContactNamePropertySet("Bertha", "A.", "Buell");
contact.ProfessionalInfo = new MapiContactProfessionalPropertySet("Awthentikz", "Social work assistant");
contact.PersonalInfo.PersonalHomePage = "B2BTies.com";
contact.PhysicalAddresses.WorkAddress.Address = "Im Astenfeld 59 8580 EDELSCHROTT";
contact.ElectronicAddresses.Email1 = new MapiContactElectronicAddress("Experwas", "SMTP", "BerthaABuell@armyspy.com");
contact.Telephones = new MapiContactTelephonePropertySet("06605045265");
contact.PersonalInfo.Children = new string[] { "child1", "child2", "child3" };
contact.Categories = new string[] { "category1", "category2", "category3" };
contact.Mileage = "Some test mileage";
contact.Billing = "Test billing information";
contact.OtherFields.Journal = true;
contact.OtherFields.Private = true;
contact.OtherFields.ReminderTime = new DateTime(2014, 1, 1, 0, 0, 55);
contact.OtherFields.ReminderTopic = "Test topic";
contact.OtherFields.UserField1 = "ContactUserField1";
contact.OtherFields.UserField2 = "ContactUserField2";
contact.OtherFields.UserField3 = "ContactUserField3";
contact.OtherFields.UserField4 = "ContactUserField4";
// Add a photo
using (FileStream fs = File.OpenRead(dataDir + "Desert.jpg"))
{
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
contact.Photo = new MapiContactPhoto(buffer,
MapiContactPhotoImageFormat.Jpeg);
}
// Save the Contact in MSG format
contact.Save(dataDir + "MapiContact_out.msg",ContactSaveFormat.Msg);
// Save the Contact in VCF format
contact.Save(dataDir + "MapiContact_out.vcf", ContactSaveFormat.VCard);

Save Contact in Version 3 VCF Format

To save the contact in version 3 VCF format, use the VCardVersion enumerable to set the VCardSaveOptions.Version property. The following sample code demonstrates the use of VCardVersion enumerable to save the contact VCF version 3 format.

Reading a MapiContact

The MapiContact class can be used to load both Outlook MSG and VCard format contacts. The following code snippet shows you how to load Outlook contacts saved as MSG and VCF into a MapiContact.

Loading a Contact from MSG

The following code snippet shows you how to load contacts from MSG.

Loading a Contact from VCard

The following code snippet shows you how to load contacts from VCard.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
var vcfTest = VCardContact.Load(dataDir + "Contact.vcf");
MapiContact contact = MapiContact.FromVCard(dataDir + "Contact.vcf");

Loading a Contact from VCard with Specified Encoding

The following code snippet shows you how to load contacts from VCard with the specified encoding.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
var contactReadFromFile = MapiContact.FromVCard(dataDir + @"Contact.vcf", Encoding.UTF8);

Rendering Contact Information to MHTML

Outlook Contact can be converted to MHTML using Aspose.Email API. This example shows how a VCard is loaded into MapiContact and then converted to MHTML with the help of MailMessage API.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
string dataDir = RunExamples.GetDataDir_Outlook();
//Load VCF Contact and convert to MailMessage for rendering to MHTML
MapiContact contact = MapiContact.FromVCard(dataDir + "Contact.vcf");
MemoryStream ms = new MemoryStream();
contact.Save(ms, ContactSaveFormat.Msg);
ms.Position = 0;
MapiMessage msg = MapiMessage.FromStream(ms);
MailConversionOptions op = new MailConversionOptions();
MailMessage eml = msg.ToMailMessage(op);
//Prepare the MHT format options
MhtSaveOptions mhtSaveOptions = new MhtSaveOptions();
mhtSaveOptions.CheckBodyContentEncoding = true;
mhtSaveOptions.PreserveOriginalBoundaries = true;
MhtFormatOptions formatOp = MhtFormatOptions.WriteHeader | MhtFormatOptions.RenderVCardInfo;
mhtSaveOptions.RenderedContactFields = ContactFieldsSet.NameInfo | ContactFieldsSet.PersonalInfo | ContactFieldsSet.Telephones | ContactFieldsSet.Events;
mhtSaveOptions.MhtFormatOptions = formatOp;
eml.Save(dataDir + "ContactMhtml_out.mhtml", mhtSaveOptions);