很多系统现在都要求把联系人信息添入客户端OutLook中,有两种做法,一种是采用ACTIVEX 的做法,直接写入客户端OutLook数据库,但是这种做法受到客户端配置限制,要求客户端必须装有OutLook 软件,并且必须允许运行Activex 控件。另一种就是写成一个Vcard 文件,由客户去下载后自由保存。
功能不是问题,重点是 OutLook 的数据格式
保存联系人到OutLook:
//首先尝试保存联系人到客户端OutLook中,如果异常则转向到下载页面下载Vcard文件
function loadpage() {
//debugger
var url = location.href;
var paraString = url.substring(url.indexOf("?") + 1, url.length).split("=");
var dt = Web_OutLook.GetAContact(paraString[1]).value;// AJAX 方法 用于提取数据到页面上
try {
outLookApp = new ActiveXObject("Outlook.Application");
outlookNS = outLookApp.GetNamespace("MAPI");
theContact = outlookNS.GetDefaultFolder(10);
theContactItems = theContact.Items;
addItem = outLookApp.CreateItem(2);
if (dt.Rows[0].name.split(' ').length >= 2) {
addItem.LastName = dt.Rows[0].name.split(' ')[dt.Rows[0].name.split(' ').length - 1];
for (var i = 0; i <= dt.Rows[0].name.split(' ').length - 2; i++) {
addItem.FirstName += dt.Rows[0].name.split(' ')[i];
}
}
else {
addItem.FirstName = dt.Rows[0].name;
}
addItem.Email1Address = dt.Rows[0].cardemail;
addItem.Email2Address = dt.Rows[0].otherlemail;
addItem.CompanyName = dt.Rows[0].companyname;
addItem.Department = dt.Rows[0].department;
addItem.JobTitle = dt.Rows[0].jobtitle;
//
addItem.BusinessAddressCountry = dt.Rows[0].country;
addItem.BusinessAddressPostalCode = dt.Rows[0].postalcode;
addItem.BusinessAddressState = dt.Rows[0].state;
addItem.BusinessAddressStreet = dt.Rows[0].street;
addItem.BusinessAddressCity = dt.Rows[0].city;
//
addItem.BusinessTelephoneNumber = dt.Rows[0].phone;
addItem.BusinessFaxNumber = dt.Rows[0].fax;
addItem.MobileTelephoneNumber = dt.Rows[0].mobile;
//
addItem.HomeFaxNumber = dt.Rows[0].p_fax;
addItem.HomeTelephoneNumber = dt.Rows[0].p_phone;
addItem.Home2TelephoneNumber = dt.Rows[0].p_mobile;
//
addItem.HomeAddressCity = dt.Rows[0].p_city;
addItem.HomeAddressCountry = dt.Rows[0].p_country;
addItem.HomeAddressStreet = dt.Rows[0].p_street;
addItem.HomeAddressPostalCode = dt.Rows[0].p_postalcode;
//
addItem.IMAddress = dt.Rows[0].workim;
addItem.Save();
window.alert("saved.");
}
catch (Error) {
window.open("url+" + dt.Rows[0].cardname.replace(".png", ".vcf"));
}
}
写Vcard 文件:
#region string GetVcard(MDLCard mycard) 创建Vcard
/// <summary>
///
/// </summary>
/// <param name="mycard"></param>
/// <returns></returns>
public string GetVcard(MDLCard mycard)
{
string[] phonestrs = mycard.Phone.Split('-');
string phonestr = (phonestrs[0] == "" ? "" : "(" + phonestrs[0] + ")") + " " + (phonestrs[1] == "" ? "" : "(" + phonestrs[1] + ")") + " " + phonestrs[2] + " " + (phonestrs[3] == "" ? "" : "x " + phonestrs[3]);
string[] faxstrs = mycard.Fax.Split('-');
string faxstr = (faxstrs[0] == "" ? "" : "(" + faxstrs[0] + ")") + " " + (faxstrs[1] == "" ? "" : "(" + faxstrs[1] + ")") + " " + faxstrs[2];
string[] mobilestrs = mycard.Mobile.Split('-');
string mobilestr = (mobilestrs[0] == "" ? "" : "(" + mobilestrs[0] + ")") + " " + (mobilestrs[1] == "" ? "" : "(" + mobilestrs[1] + ")") + " " + mobilestrs[2];
string[] p_phonestrs = mycard.P_phone.Split('-');
string p_phonestr = (p_phonestrs[0] == "" ? "" : "(" + p_phonestrs[0] + ")") + " " + (p_phonestrs[1] == "" ? "" : "(" + p_phonestrs[1] + ")") + " " + p_phonestrs[2];
string[] p_faxstrs = mycard.P_fax.Split('-');
string p_faxstr = (p_faxstrs[0] == "" ? "" : "(" + p_faxstrs[0] + ")") + " " + (p_faxstrs[1] == "" ? "" : "(" + p_faxstrs[1] + ")") + " " + p_faxstrs[2];
string[] p_mobilestrs = mycard.P_mobile.Split('-');
string p_mobilestr = (p_mobilestrs[0] == "" ? "" : "(" + p_mobilestrs[0] + ")") + " " + (p_mobilestrs[1] == "" ? "" : "(" + p_mobilestrs[1] + ")") + " " + p_mobilestrs[2];
string vcards = "";
vcards += "BEGIN:VCARD" + "/n";
vcards += "VERSION:2.1" + "/n";
vcards += "N:" + mycard.Name + ";" + "" + "/n";
vcards += "FN:" + mycard.Name + "/n";
vcards += "NICKNAME:nickName" + "/n";
vcards += "ORG:" + mycard.CompanyName + ";" + mycard.Department + "/n";
vcards += "TITLE:" + mycard.JobTitle + "/n";
vcards += "NOTE;ENCODING=QUOTED-PRINTABLE:=C6=E4=CB=FB" + "/n";
vcards += "TEL;WORK;VOICE:" + phonestr + "/n";
vcards += "TEL;WORK;VOICE:" + "" + "/n";
vcards += "TEL;HOME;VOICE:" + p_phonestr + "/n";
vcards += "TEL;HOME;VOICE:" + "" + "/n";
vcards += "TEL;CELL;VOICE:" + mobilestr + "/n";
vcards += "TEL;PAGER;VOICE:" + "" + "/n";
vcards += "TEL;WORK;FAX:" + faxstr + "/n";
vcards += "TEL;HOME;FAX:" + p_faxstr + "/n";
vcards += "ADR;WORK:;;" + mycard.street + ";" + mycard.City + ";" + mycard.State + ";" + mycard.Postalcode + ";" + mycard.Country + "/n";
vcards += "LABEL;WORK;ENCODING=QUOTED-PRINTABLE:=B5=A5=CE=BB=B5=D8=D6=B7" + "/n";
vcards += "=C9=EE=DB=DA" + "/n";
vcards += "=B9=E3=B6=AB" + "/n";
vcards += "" + "/n";
vcards += "=B9=FA=BC=D2" + "/n";
vcards += "ADR;HOME;POSTAL;PARCEL:;;" + mycard.P_street + ";" + mycard.P_city + ";" + mycard.P_states + ";" + mycard.P_postalcode + ";" + mycard.P_country + "/n";
vcards += "LABEL;HOME;ENCODING=QUOTED-PRINTABLE:=BD=D6=B5=C0=B5=D8=D6=B7" + "/n";
vcards += "=C9=EE=DB=DA" + "/n";
vcards += "=B9=E3=B6=AB" + "/n";
vcards += "433330" + "/n";
vcards += "=D6=D0=B9=FA" + "/n";
vcards += "URL:" + "http://" + "/n";
vcards += "URL:" + mycard.WebSite + "/n";
vcards += "EMAIL;PREF;INTERNET:" + mycard.cardEmail + "/n";
vcards += "X-QQ:" + "" + "/n";
vcards += "X-ICQ:icq" + "/n";
vcards += "X-WAB-GENDER:2" + "/n";
vcards += "REV:" + "" + "/n";
vcards += "END:VCARD";
return vcards;
}
#endregion
#region void FileWriter(string path, string message) 将指定信息写入指定文件
/// <summary>
/// 将指定信息写入指定文件
/// </summary>
/// <param name="path"></param>
/// <param name="message"></param>
public void FileWriter(string path, string message)
{
try
{
File.Delete(path);
Stream mystream = File.Open(path, FileMode.Create, FileAccess.Write);
StreamWriter mywriter = new StreamWriter(mystream);
mywriter.WriteLine(message);
mywriter.Flush();
mystream.Close();
}
catch (Exception ex)
{
//Response.Write("<script>alert('failed')</script>");
}
}
#endregion