• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 857
  • Last Modified:

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

Hi,

I am writing an email application in C# ASP.net 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:

//Declaration

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)
        outlookApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
     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);
       
        template.ActiveWindow.Selection.WholeStory();
        template.ActiveWindow.Selection.Copy();
     
//        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 = "nsou9oti@bmtit78i.com";
     
        eMail = (Microsoft.Office.Interop.Outlook.MailItem)template.Email;
        //eMail.Body = ;
        eMail.Importance = Microsoft.Office.Interop.Outlook.OlImportance.olImportanceNormal;
        try
        {
            ((Microsoft.Office.Interop.Outlook._MailItem)eMail).Send();
        }
        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

Thanks.
0
fantasylan
Asked:
fantasylan
2 Solutions
 
nordtorpCommented:
I am not that familiar with C# but could it be that your code should read

 try
        {
            ((Microsoft.Office.Interop.Outlook._MailItem)eMail).Send();
            break;
        }
        catch (System.Exception ex)
        {
         
        }
0
 
arunallimuthuCommented:
Hi,

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.

Thanks,
Arun.
0
 
fantasylanAuthor Commented:
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.

Thanks
0
 
Miguel OzSoftware EngineerCommented:
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)
http://stackoverflow.com/questions/158706/how-to-properly-clean-up-excel-interop-objects-in-c#159419
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:
GC.Collect();
GC.WaitForPendingFinalizers();
Marshal.FinalReleaseComObject(template);
Marshal.FinalReleaseComObject(wordApp);

Any particular reason for using outlook client, you ar better off using SmtpClient:
http://social.msdn.microsoft.com/Forums/en/netfxnetcom/thread/a75533eb-131b-4ff3-a3b2-b6df87c25cc8
http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.aspx

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now