Working with contact data in professional applications often requires converting between different formats. One common scenario is transforming Outlook contacts into a web-friendly format to preview or share them in a browser, and then converting their content back into VCF format for storage or reuse.
This article demonstrates how to use the Aspose.Email for .NET library to perform these conversions in C#.
Power Your Contact Conversion with an Advanced .NET API
Aspose.Email for .NET is a robust library that enables developers to create, manipulate, and convert Outlook files (MSG, PST, OST), emails, calendar items, distribution lists, and more. In this article, we’ll use it to convert:
- Outlook VCF contacts to HTML documents
- HTML content into MapiContact objects
Getting Started
Start by installing the library into your project using the NuGet Package Manager:
- Open your project in Visual Studio.
- Navigate to Tools > NuGet Package Manager > Manage NuGet Packages for Solution.
- Search for Aspose.Email.
- Select the package and click Install.
Alternatively, you can install the package via the Package Manager Console using the following command:
Install-Package Aspose.Email
You can also download the latest version of the API directly from the Aspose website.
Once installed, you are ready to go.
Export VCF Contacts to HTML
Aspose.Email allows loading a VCF file as a MapiContact and then exporting it to display-friendly layout with customizable options like including vCard details and headers. The following code sample demonstrates how to export VCF to HTML:
- Use the FromVCard() method to read the .vcf file and create a MapiContact object.
- Call GetUnderlyingMessage() on the MapiContact to transform it into a MailMessage object for further export.
- Set up HtmlSaveOptions to control the formatting of the exported data:
- Include vCard-specific details with RenderVCardInfo.
- Add a standard email-style header with WriteHeader .
Import HTML Content into a VCF Contact
You can also generate a new Outlook contact from an HTML string and save it as a VCF file. This is useful when personal details are collected or edited through a web form or imported from web templates. The following code sample demonstrates how to create a contact from HTML:
- Load the contents of an HTML file into a string.
- Initialize a MapiContact with a display name and an email address.
- Assign the loaded content to the contact using SetBodyContent(), specifying the content type as BodyContentType.Html.
- Save the result as a .vcf file using the ContactSaveFormat.VCard format.
Conclusion
We explored one of the extended features of the Aspose.Email for .NET library in email management. You learned how to convert contacts to and from HTML to improve interoperability, enable easier customization, and provide a better user experience when displaying personal information. Using our APIs, you can handle these tasks with just a few lines of code and complete control over formatting.
Additional Resources
To help you make the most of the API, check out the following resources:
