"RPC Server is unavailable" when exporting Outlook contacts to CSV

0

I am exporting Outlook contacts with a custom form to CSV from a specific Contacts folder. This folder is not the default location. It is a large folder with almost 7,000 contacts. This is a shared mailbox in Office 365, but we can't use the Outlook or Graph APIs because of the custom data.

My code below works fine, except that after iterating through 200-800 contacts, I get this error: "RPC Server is unavailable. (Exception from HRESULT: 0x800706BA)."

I also tried exporting the folder to a .pst and accessing that folder on my local machine. The result is essentially the same. UPDATE: I've been watching Outlook.exe in the task manager, and it steadily increases its memory consumption to around 300MB before crashing. I've tried the same code on a laptop running Office 2010, and I get an "Out of Memory" error.

I get the error whether Outlook is open or closed, but closing Outlook during program operation will immediately trigger this error. Outlook either starts (or restarts) following this error. I've read a number of SO posts and articles related to this error, but I'm not sure exactly what's going on. Any help is appreciated.

UPDATE: Code has been updated to 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 and StoreID of my folder
                    sharedContacts =
                        ns.GetFolderFromID(
                            "myEntryID",
                            "myStoreID"
                            );
                    Items contacts = sharedContacts.Items;

                    //Writing header row
                    ContactItem first = contacts.GetFirst();
                    var properties = first.ItemProperties;
                    for(int i = 0; i < 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
                    for (int i = 1; i <= contacts.Count; i++)
                    {
                        object o = contacts[i];
                        if (o is ContactItem)
                        {
                            ContactItem contact = (ContactItem)o;
                            if (contact != null)
                            {
                                Console.Write(contact.FullName);
                                StringBuilder dataRow = new StringBuilder();
                                ItemProperties contactProps = contact.ItemProperties;
                                for (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 = "";
                                        //    for (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);
        }

    }
}
c#
outlook
office-interop
asked on Stack Overflow Aug 14, 2017 by steveo • edited Aug 17, 2017 by steveo

1 Answer

1

Looks like you are running out of RPC channels. You need to avoid using foreach loops (they tend to keep all collection members referenced until the loop exits) and explicitly release all COM objects are soon as you are done with them.

Off the top of my head:

for(int i = 1; i <= contacts.Count; i++)
{
    obejct o = contacts[i];
    ContactItem contact = o as ContactItem;
    if (o != null)
    {
        ItemProperties properties = contact.ItemProperties;
        StringBuilder newLine = new StringBuilder();
        for (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);
}

User contributions licensed under CC BY-SA 3.0