c# - "RPC Server is unavailable" when exporting Outlook contacts to CSV -
i exporting outlook contacts custom form csv specific contacts folder. folder not default location. large folder 7,000 contacts. shared mailbox in office 365, can't use outlook or graph apis because of custom data.
my code below works fine, except after iterating through 200-800 contacts, error: "rpc server unavailable. (exception hresult: 0x800706ba)."
i tried exporting folder .pst , accessing folder on local machine. result same. update: i've been watching outlook.exe in task manager, , steadily increases memory consumption around 300mb before crashing. i've tried same code on laptop running office 2010, , "out of memory" error.
i error whether outlook open or closed, closing outlook during program operation trigger error. outlook either starts (or restarts) following error. i've read number of posts , articles related error, i'm not sure what's going on. appreciated.
update: code has been updated use for
loops instead of foreach
loops per dmitry streblechenko's suggestion.
using system; using microsoft.office.interop.outlook; using system.text; using system.io; using system.runtime.interopservices; namespace outlookcontacts { class program { static void main(string[] args) { var encoding = encoding.utf8; string csvpath = @"mycsvpath"; application outlook = new application(); namespace ns = outlook.getnamespace("mapi"); mapifolder sharedcontacts; string recipientname = "myemail@mydomain"; stringbuilder headerrow = new stringbuilder(); recipient recip = ns.createrecipient(recipientname); streamwriter writer = new streamwriter(csvpath, false, encoding); recip.resolve(); if (recip.resolved) { try { //entryid , storeid of folder sharedcontacts = ns.getfolderfromid( "myentryid", "mystoreid" ); items contacts = sharedcontacts.items; //writing header row contactitem first = contacts.getfirst(); var properties = first.itemproperties; for(int = 0; < properties.count; i++) { try { headerrow.append(string.format("\"{0}\",", properties[i].name)); } catch (system.exception ex) { headerrow.append(string.format("{0},", ex.message)); } } headerrow.appendline(environment.newline); writetocsv(writer, headerrow); console.writeline("header row written;"); marshal.releasecomobject(properties); marshal.releasecomobject(first); //writing records (int = 1; <= contacts.count; i++) { object o = contacts[i]; if (o contactitem) { contactitem contact = (contactitem)o; if (contact != null) { console.write(contact.fullname); stringbuilder datarow = new stringbuilder(); itemproperties contactprops = contact.itemproperties; (int j = 0; j < contactprops.count; j++) { itemproperty property = contactprops[j]; try { if (property.value == null) { string value = "null,"; datarow.append(value); } //else if (property.name == "attachments") //{ // //attachment file names // string attachment = ""; // (int k = 1; k < contact.attachments.count; k++) // { // attachment = (string.format("\"{0}\"; ", contact.attachments[k].filename)); // datarow.append(attachment); // } // datarow.append(","); //} else { string value = property.value.tostring(); value = value.replace("\r\n\r\n\r\n", "\r\n") .replace("\r\n\r\n", "\r\n") .replace("\"", "'"); value = (string.format("\"{0}\",", value)); datarow.append(value); } } catch (system.exception ex) { string value = string.format("{0}: {1},", property.name, ex.message); datarow.append(value); } marshal.releasecomobject(property); } datarow.append(environment.newline); writetocsv(writer, datarow); marshal.releasecomobject(contactprops); marshal.releasecomobject(contact); } marshal.releasecomobject(o); counter++; console.writeline(": written " + counter); } } } catch (system.exception ex) { console.writeline(datarow.tostring(), ex.message); console.readkey(); } } } static void writetocsv(streamwriter writer, stringbuilder row) { var data = row.tostring(); writer.writeasync(data); } } }
looks running out of rpc channels. need avoid using foreach loops (they tend keep collection members referenced until loop exits) , explicitly release com objects done them.
off top of head:
for(int = 1; <= contacts.count; i++) { obejct o = contacts[i]; contactitem contact = o contactitem; if (o != null) { itemproperties properties = contact.itemproperties; stringbuilder newline = new stringbuilder(); (int j = 1; j <= properties.count; j++) { itemproperty property = properties[j]; var value = ""; if (property.value == null) { value = "null,"; console.writeline(value); newline.append(value); } else { value = property.value.tostring() + ","; newline.append(value); } marshal.releasecomobject(property); } newline.append(environment.newline); writetocsv(writer, newline); marshal.releasecomobject(properties); marshal.releasecomobject(contact); } marshal.releasecomobject(o); }
Comments
Post a Comment