How do I open a Word document and feed its contents as the body of an Outlook email message ?

Posted on 2009-12-28
Last Modified: 2013-11-25

I am writing an email application in C# which involves reading from a file located in a server and feeding the contents as the body of the email message.

I am using Microsoft Office COM Libraries for Outlook and Word.

I am pasting the relevant code snippets below:


static Microsoft.Office.Interop.Outlook.Application outlookApp= new Microsoft.Office.Interop.Outlook.Application();
     static Microsoft.Office.Interop.Outlook.MailItem eMail = (Microsoft.Office.Interop.Outlook.MailItem)
     static Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
     static Microsoft.Office.Interop.Word.Document template = new Document();
     static string documentsLocation = "";

// Code snippet for the mentioned task:

object fpath =  documentsLocation+ "IMEcvrltr.doc";
        object nullObject = System.Reflection.Missing.Value;
        template = wordApp.Documents.Open(ref fpath,ref nullObject, ref nullObject, ref nullObject, ref nullObject, ref nullObject, ref nullObject, ref nullObject, ref nullObject, ref nullObject, ref nullObject, ref nullObject, ref nullObject, ref nullObject, ref nullObject, ref nullObject);
//        IDataObject data = Clipboard.GetDataObject();

        //string text = data.GetData(DataFormats.Text).ToString();
        template.Close(ref nullObject,ref nullObject,ref nullObject);

        //eMail.Subject = "TESTING FROM CODE";
        //eMail.Attachments.Add("f:\\test.txt", Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue, 1, "Test");
        eMail.To = "";
        eMail = (Microsoft.Office.Interop.Outlook.MailItem)template.Email;
        //eMail.Body = ;
        eMail.Importance = Microsoft.Office.Interop.Outlook.OlImportance.olImportanceNormal;
        catch (System.Exception ex)
Currently I am getting the following error:
The object invoked has disconnected from its clients. (Exception from HRESULT: 0x80010108 (RPC_E_DISCONNECTED)). Also, when the application tries to open the file, I get a message that the file is locked for editing by the admin and only after selecting an option I can continue with my operation to get the above error.

Please help

Question by:fantasylan
    LVL 11

    Expert Comment

    I am not that familiar with C# but could it be that your code should read

            catch (System.Exception ex)
    LVL 2

    Accepted Solution


    create a copy of the document into your local system[temp file]. Then you read the content from the local doc. I hope this will solve ur issue.


    Author Comment

    Hi Arun,

    How do I  programatically what you mentioned above ? If possible, could you add it to my code above or provide me with a code snippet so that I can move in the right direction.

    LVL 35

    Assisted Solution

    by:Miguel Oz
    You can not declare the automation objects static, because it will keep the reference for the lifetime of your process (your web page)
    Use them as local as local variables and clean up your automation objects as per link below.

    You need to close and garbage collect: (Even though is for Excel but the same applies to any object automation)
    Note: This will ensure you do not have file locking errors, because the word object has been clean up properly, in your case for Word:

    Any particular reason for using outlook client, you ar better off using SmtpClient:

    Note: let me know how you go because I do not think you can use clipboard in pages becuse clipboard is used only on STA threads (main thread) and that can not be guaranteed in apps

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    If you haven’t already, I encourage you to read the first article ( in my series to gain a basic foundation of R and R Studio.  You will also find the …
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now