It is very easy to generate a vCARD file from an address with ASP.NET so user can later on import the vCARD file to their Outlook Contacts.
First, let us take a look at a sample vCARD file. The following vCARD file was generated from Microsoft Outlook 2007:
BEGIN:VCARD VERSION:2.1 N;LANGUAGE=en-us:Jeffrey;Jeffrey;;Mr. FN:Mr. Jeffrey Jeffrey ORG:Royal Crown Technologies;Information Technology Department TITLE:Programmer/Analyst III TEL;WORK;VOICE:666-666-6666 ext 666 TEL;HOME;VOICE:666-666-6666 TEL;CELL;VOICE:666-666-6666 TEL;WORK;FAX:666-666-6666 ADR;WORK;PREF:;;Royal Crown Technologies LABEL;WORK;PREF;ENCODING=QUOTED-PRINTABLE:Royal Crown Technologies=0D=0A= P.O. Box 43070=0D=0A= Nowhere, TX 12345 ADR;HOME:;;Jeffrey Jeffrey LABEL;HOME;ENCODING=QUOTED-PRINTABLE:Jeffrey Jeffrey=0D=0A= 1234 Home Address=0D=0A= Nowhere, TX 12345 X-MS-OL-DEFAULT-POSTAL-ADDRESS:2 URL;WORK:http://www.RoyalCrownTech.com EMAIL;PREF;INTERNET:email1@live.com EMAIL;INTERNET:email2@yahoo.com EMAIL;INTERNET:email3@live.com X-MS-CARDPICTURE;TYPE=JPEG;ENCODING=BASE64: /9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQY GBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYa KCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wAAR CACUACcDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAA AgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkK FhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaG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CARD
The blue portion of the above vCARD file is for the embedded picture of the contact, and you can remove this part if you don’t have a picture.
Another thing needs your attention is the string in red: =0D=0A, it is the carriage return character in Windows, and Outlook needs it to properly display the address information.
Now, all you need to do is to write your code to generate a .vcf file with content as above, then use Response stream to open the file save dialog in browser to let user Open or Save the file.
Here is the code snippet I used to generate the above file (the comment should be self-explanatory):
- // The following sample data
- // can be replace with real data
- string firstName = “Jeffrey”;
- string lastName = “Jeffrey”;
- string title = “Mr.”;
- string organization = “Royal Crown Technologies”;
- string department = “Information Technology Department”;
- string jobTitle = “Programmer/Analyst III”;
- string workPhone = “666-666-6666 ext 666”;
- string homePhone = “666-666-6666”;
- string cellPhone = “666-666-6666”;
- string fax = “666-666-6666”;
- string homeAddress1 = “1234 Home Address”;
- string homeAddress2 = “”;
- string homeAddressCity = “Nowhere”;
- string homeAddressState = “TX”;
- string homeAddressZip = “12345”;
- string workAddress1 = “Royal Crown Technologies”;
- string workAddress2 = “P.O. Box 43070”;
- string workAddressCity = “Nowhere”;
- string workAddressState = “TX”;
- string workAddressZip = “12345”;
- string url = “http://www.RoyalCrownTech.com”;
- string email1 = “email1@live.com”;
- string email2 = “email2@yahoo.com”;
- string email3 = “email3@live.com”;
- // Generate the vCARD file content
- StringBuilder sb = new StringBuilder();
- // The following two lines are essential
- sb.Append(“BEGIN:VCARD\r\n”);
- sb.Append(“VERSION:2.1\r\n”);
- // Name and other information of the contact
- sb.AppendFormat(“N;LANGUAGE=en-us:{0};{1};;{2}\r\n”, lastName, firstName, title);
- sb.AppendFormat(“FN:{0} {1} {2}\r\n”, title, firstName, lastName);
- sb.AppendFormat(“ORG:{0};{1}\r\n”, organization, department, “\r\n”);
- sb.AppendFormat(“TITLE:{0}\r\n”, jobTitle, “\r\n”);
- sb.AppendFormat(“TEL;WORK;VOICE:{0}\r\n”, workPhone, “\r\n”);
- sb.AppendFormat(“TEL;HOME;VOICE:{0}\r\n”, homePhone, “\r\n”);
- sb.AppendFormat(“TEL;CELL;VOICE:{0}\r\n”, cellPhone, “\r\n”);
- sb.AppendFormat(“TEL;WORK;FAX:{0}\r\n”, fax, “\r\n”);
- // PREF means it is the default mailing address
- sb.AppendFormat(“ADR;WORK;PREF;ENCODING=QUOTED-PRINTABLE:;;{0}\r\n”, workAddress1);
- sb.AppendFormat(“{0};{1};{2};{3};United State of America\r\n”,
- new Object[] { workAddress2, workAddressCity, workAddressState, workAddressZip });
- sb.AppendFormat(“LABEL;WORK;ENCODING=QUOTED-PRINTABLE:{0}=0D=0A=\r\n”, workAddress1);
- if (workAddress2 != “”)
- {
- sb.AppendFormat(“{0}=0D=0A=\r\n”, workAddress2);
- }
- sb.AppendFormat(“{0}, {1} {2}\r\n”, workAddressCity, workAddressState, workAddressZip);
- sb.AppendFormat(“ADR;HOME;ENCODING=QUOTED-PRINTABLE:;;{0} {1}\r\n”, firstName, lastName);
- // Change the country if not in USA
- sb.AppendFormat(“{0};{1};{2};{3};United States of America\r\n”,
- new object[] { homeAddress1, homeAddressCity, homeAddressState, homeAddressZip });
- sb.AppendFormat(“LABEL;HOME;ENCODING=QUOTED-PRINTABLE:{0} {1}=0D=0A=\r\n”,
- new object[] { firstName, lastName });
- sb.AppendFormat(“{0}=0D=0A=\r\n”, homeAddress1);
- if (homeAddress2 != “”)
- {
- sb.AppendFormat(“{0}=0D=0A=\r\n”, homeAddress2);
- }
- sb.AppendFormat(“{0}, {1} {2}\r\n”, homeAddressCity, homeAddressState, homeAddressZip );
- // 2: Work Address, 1: Home Address, 3: Other
- sb.Append(“X-MS-OL-DEFAULT-POSTAL-ADDRESS:2\r\n”);
- sb.AppendFormat(“URL;WORK:{0}\r\n”, url);
- // PREF means it is the default email address
- sb.AppendFormat(“EMAIL;PREF;INTERNET:{0}\r\n”, email1);
- sb.AppendFormat(“EMAIL;INTERNET:{0}\r\n”,email2);
- sb.AppendFormat(“EMAIL;INTERNET:{0}\r\n”, email3);
- sb.Append(“END:VCARD\r\n”);
- string cardContent = sb.ToString();
- // Time to open the file save dialog
- // by using Response stream
- Response.Clear();
- Response.AddHeader(“Content-Disposition”, “attachment;filename=” + firstName + ” “ + lastName + “.vcf”);
- Response.ContentType = “text/plain”;
- StreamWriter sw = new StreamWriter(Response.OutputStream);
- sw.Write(cardContent);
- sw.Flush();
- Response.End();
Hope this helps.