Learning While Aging

Generating vCARD file with ASP.NET

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
 Fh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9xvwpj/fb60UUARnrRRRQAyiiigD/
 2Q==

X-MS-OL-DESIGN;CHARSET=utf-8:<card xmlns=…… </card>
END:VCARD

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):

Code Snippet
  1. // The following sample data
  2. // can be replace with real data
  3. string firstName = “Jeffrey”;
  4. string lastName = “Jeffrey”;
  5. string title = “Mr.”;
  6. string organization = “Royal Crown Technologies”;
  7. string department = “Information Technology Department”;
  8. string jobTitle = “Programmer/Analyst III”;
  9. string workPhone = “666-666-6666 ext 666”;
  10. string homePhone = “666-666-6666”;
  11. string cellPhone = “666-666-6666”;
  12. string fax = “666-666-6666”;
  13. string homeAddress1 = “1234 Home Address”;
  14. string homeAddress2 = “”;
  15. string homeAddressCity = “Nowhere”;
  16. string homeAddressState = “TX”;
  17. string homeAddressZip = “12345”;
  18. string workAddress1 = “Royal Crown Technologies”;
  19. string workAddress2 = “P.O. Box 43070”;
  20. string workAddressCity = “Nowhere”;
  21. string workAddressState = “TX”;
  22. string workAddressZip = “12345”;
  23. string url = “http://www.RoyalCrownTech.com”;
  24. string email1 = “email1@live.com”;
  25. string email2 = “email2@yahoo.com”;
  26. string email3 = “email3@live.com”;
  27. // Generate the vCARD file content
  28. StringBuilder sb = new StringBuilder();
  29. // The following two lines are essential
  30. sb.Append(“BEGIN:VCARD\r\n”);
  31. sb.Append(“VERSION:2.1\r\n”);
  32. // Name and other information of the contact
  33. sb.AppendFormat(“N;LANGUAGE=en-us:{0};{1};;{2}\r\n”, lastName, firstName, title);
  34. sb.AppendFormat(“FN:{0} {1} {2}\r\n”, title, firstName, lastName);
  35. sb.AppendFormat(“ORG:{0};{1}\r\n”, organization, department, “\r\n”);
  36. sb.AppendFormat(“TITLE:{0}\r\n”, jobTitle, “\r\n”);
  37. sb.AppendFormat(“TEL;WORK;VOICE:{0}\r\n”, workPhone, “\r\n”);
  38. sb.AppendFormat(“TEL;HOME;VOICE:{0}\r\n”, homePhone, “\r\n”);
  39. sb.AppendFormat(“TEL;CELL;VOICE:{0}\r\n”, cellPhone, “\r\n”);
  40. sb.AppendFormat(“TEL;WORK;FAX:{0}\r\n”, fax, “\r\n”);
  41. // PREF means it is the default mailing address
  42. sb.AppendFormat(“ADR;WORK;PREF;ENCODING=QUOTED-PRINTABLE:;;{0}\r\n”, workAddress1);
  43. sb.AppendFormat(“{0};{1};{2};{3};United State of America\r\n”,
  44. new Object[] { workAddress2, workAddressCity, workAddressState, workAddressZip });
  45. sb.AppendFormat(“LABEL;WORK;ENCODING=QUOTED-PRINTABLE:{0}=0D=0A=\r\n”, workAddress1);
  46. if (workAddress2 != “”)
  47. {
  48. sb.AppendFormat(“{0}=0D=0A=\r\n”, workAddress2);
  49. }
  50. sb.AppendFormat(“{0}, {1} {2}\r\n”, workAddressCity, workAddressState, workAddressZip);
  51. sb.AppendFormat(“ADR;HOME;ENCODING=QUOTED-PRINTABLE:;;{0} {1}\r\n”, firstName, lastName);
  52. // Change the country if not in USA
  53. sb.AppendFormat(“{0};{1};{2};{3};United States of America\r\n”,
  54. new object[] { homeAddress1, homeAddressCity, homeAddressState, homeAddressZip });
  55. sb.AppendFormat(“LABEL;HOME;ENCODING=QUOTED-PRINTABLE:{0} {1}=0D=0A=\r\n”,
  56. new object[] { firstName, lastName });
  57. sb.AppendFormat(“{0}=0D=0A=\r\n”, homeAddress1);
  58. if (homeAddress2 != “”)
  59. {
  60. sb.AppendFormat(“{0}=0D=0A=\r\n”, homeAddress2);
  61. }
  62. sb.AppendFormat(“{0}, {1}  {2}\r\n”, homeAddressCity, homeAddressState, homeAddressZip );
  63. // 2: Work Address, 1: Home Address, 3: Other
  64. sb.Append(“X-MS-OL-DEFAULT-POSTAL-ADDRESS:2\r\n”);
  65. sb.AppendFormat(“URL;WORK:{0}\r\n”, url);
  66. // PREF means it is the default email address
  67. sb.AppendFormat(“EMAIL;PREF;INTERNET:{0}\r\n”, email1);
  68. sb.AppendFormat(“EMAIL;INTERNET:{0}\r\n”,email2);
  69. sb.AppendFormat(“EMAIL;INTERNET:{0}\r\n”, email3);
  70. sb.Append(“END:VCARD\r\n”);
  71. string cardContent = sb.ToString();
  72. // Time to open the file save dialog
  73. // by using Response stream
  74. Response.Clear();
  75. Response.AddHeader(“Content-Disposition”, “attachment;filename=” + firstName + ” “ + lastName + “.vcf”);
  76. Response.ContentType = “text/plain”;
  77. StreamWriter sw = new StreamWriter(Response.OutputStream);
  78. sw.Write(cardContent);
  79. sw.Flush();
  80. Response.End();

Hope this helps.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x