I am trying to interop with Outlook from a serviced component See
attached code below.
I am running my component inside a hostprocess (dllhost).
I succeds in creating the application object and the MailItem object,
but fails when I try to add an attachment. When I run the same code as
a standalone exe everything works just fine.
My target os is Win2k server
Please help me..
Component Code:
using System;
using System.EnterpriseServices;
using Interop.Outlook;
using System.Reflection;
[assembly: ApplicationName("OutlookTest")]
[assembly: ApplicationActivation(ActivationOption.Server)]
[assembly: ApplicationAccessControl(false)]
namespace OutlookTestComponent
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class OutlookTestComponent : ServicedComponent
{
public OutlookTestComponent()
{
//
// TODO: Add constructor logic here
//
}
public string test()
{
Interop.Outlook._Application OutlookApp = new
Interop.Outlook.ApplicationClass();
MailItem m = OutlookApp.CreateItem(OlItemType.olMailItem) as
MailItem;
object optional = System.Reflection.Missing.Value;
m.Attachments.Add(@"c:\temp\test.txt", optional, optional,
optional);
return "Success";
}
}
}
Client Code
using System;
using OutlookTestComponent;
namespace oltest
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
OutlookTestComponent.OutlookTestComponent c = new
OutlookTestComponent.OutlookTestComponent();
Console.WriteLine(c.test());
//
// TODO: Add code to start application here
//
}
}