smart developer's blog

This is a C# resource library! Free how to's and best practices…

How to generate a vCard file

with 9 comments

Ever needed to dinamically generate a vCard file? The syntax is very simple. Just generate a .vcf text file and open it!

You can find many link resources here. Also, you can find a nice generator here: http://www.vicintl.com/vcf/.

I managed to create a simple C# script that generates this file. The result was as expected:


BEGIN:VCARD
FN:Mr. John P. Smith, Jr.
TITLE:General Manager
ORG:XYZ Corp.;North American Division;Manufacturing
ADR;POSTAL;WORK:;;P.O. Box 10010;AnyCity;AnyState;00000;U.S.A.
LABEL;POSTAL;WORK;ENCODING=QUOTED-PRINTABLE:P.O. Box 10010=0D=0A=
Anywhere, TN  37849=0D=0A=U.S.A.
ADR;PARCEL;WORK:;133 Anywhere St.;Suite 360;AnyCity;AnyState;00000;U.S.A.
LABEL;POSTAL;WORK;ENCODING=QUOTED-PRINTABLE:133 Anywhere St.=0D=0A=
Anywhere, TN  37849=0D=0A=U.S.A.
TEL;Work;VOICE;MESG;PREF:+1-234-456-7891 x56473
TEL;Home:+1-234-456-7891
TEL;Pager:+1-234-456-7891
TEL;Cell:+1-234-456-7891
TEL;Modem;FAX:+1-234-456-7891,,*3
EMAIL;Internet:webmaster@anywhere.com
URL:http://www.anywhere.com/mrh.vcf
UID:http://www.anywhere.com/mrh.vcf
TZ:-0500
BDAY:1997-11-29
REV:20090401T065518
VERSION:2.1
END:VCARD

generated this:

vcard_noimage

The real challenge camed when I treid to add a image to my vCard file. I couldn’t fint it on the Internet to a quick Export using the Outlook 2007 revealed the secret: all you have to do is to add a new line to your .vcf file:


PHOTO;TYPE=JPEG;ENCODING=BASE64:
YOUR BASE64 ENCODED IMAGE HERE

In order to generate the “YOUR BASE64 ENCODED IMAGE HERE” string in C# you can use the following method:


private string EncodeWithString(string ImagePath)
	{
		System.IO.FileStream inFile;
		byte[] binaryData;

		try
		{
			inFile = new System.IO.FileStream(ImagePath,
											  System.IO.FileMode.Open,
											  System.IO.FileAccess.Read);
			binaryData = new Byte[inFile.Length];
			long bytesRead = inFile.Read(binaryData, 0,
										(int)inFile.Length);
			inFile.Close();
		}
		catch (System.Exception exp)
		{
			// Error creating stream or reading from it.
			//System.Console.WriteLine("{0}", exp.Message);
			//return;
			return exp.Message;
		}

		// Convert the binary input into Base64 UUEncoded output.
		string base64String;
		try
		{
			base64String =
			   System.Convert.ToBase64String(binaryData,
											 0,
											 binaryData.Length);
		}
		catch (System.ArgumentNullException)
		{
			//System.Console.WriteLine("Binary data array is null.");
			//return;
			return "Binary data array is null.";
		}

		// Write the UUEncoded version to the output file.

		return base64String;
	}

Now, the new result is real magic:

vcard_image

Written by smartdev

April 6th, 2009 at 3:20 pm

Posted in .Net,Programming

Tagged with ,

9 Responses to 'How to generate a vCard file'

Subscribe to comments with RSS or TrackBack to 'How to generate a vCard file'.

  1. Does anyone have any idea why the embedded image is not showing in Outlook 2003? This only works in Outlook 2007 for me.

    smartdev

    6 Apr 09 at 3:31 pm

  2. Because it does not support the importing or exporting of photos as part of a vcard. And the only reason I know that is because I spent ages trying and I finally came across a 2005 post from a Microsoft VP.

    Would you countenance humouring a newbie who can’t really code at all? I really really want to find a way of including vcard with photos on outlook messages. If I understand correctly, 2007 will accept the photos for such vcards (even though, sadly, 2003 does not). But though my online search led me to this blog entry, am I right in thinking that the generator basically does the same as creating the vcard in outlook? And that the c# script runs server-side for online generation of vcards (if so, if some kind soul could give me a clue as to where this script would be located on the server and how it would be invoked, that would be really generous).

    Kind regards.

    c

    Christophe

    21 Apr 09 at 8:28 pm

  3. please what if i need to send the vcard as SMS to a phone, how do i go about it.

    I can send regular text SMS from my PHP website to phone but i need to be able to send Vcards also.
    Thanks in advance for your response

    dewteks

    4 Jul 09 at 6:37 pm

  4. I just completed developing an online vCard generator (http://vcardmaker.com). I used this open source C# library that might be useful to anyone creating a similar application:
    http://www.thoughtproject.com/Libraries/vCard/index.htm

    Iyad

    31 Oct 09 at 7:51 pm

  5. Hi,
    Nice post! Quick question. Are vCards (2.1/3) limited to a certain number of URLs?
    Thanks

    The ard in vCard

    11 Mar 10 at 9:43 pm

  6. Hello,
    I can’t find the link to download the source.
    Can you help me?
    Thank you.

    sara

    19 May 10 at 6:21 pm

  7. Holy cow, very nice post!

  8. You are a very smart person!

    fuel-up

    3 Aug 11 at 3:44 pm

  9. Hi, really good article but i have a problem about it. i cant retrieve user’s image. i checked the EncodeWithString method and it’s run correctly,returns a Base64String. do you have any idea?
    Thanks.

    lamarcus

    13 Dec 11 at 3:07 pm

Leave a Reply