Having problem in Microsoft Outlook Automation


S

Sunil Sachdeva

hi guys,

I am working on a windows service in which i'm trying to
automate outlook to send emails periodically based on some criteria.

I developed and tested the code in a console application first and it worked
fine. But when i migrated from console to windows service it started
misbehaving. Its giving various exception as:

1) Here is the exception that I get when running the service. Service cannot
be started. System.Runtime.InteropServices.COMException (0x80020009): Cannot
complete the operation. You are not connected. at
Microsoft.Office.Interop.Outlook.NameSpaceClass.GetDefaultFolder(OlDefaultFolders
FolderType) at ClassLibrary.BaseService.SortInbox() in
C:\Users\Neemus\Documents\Visual Studio
2008\Projects\ClassLibrary\ClassLibrary\BaseService.cs:line 48 at
ClassLibrary.BaseService.Update() in C:\Users\Neemus\Documents\Visual Studio
2008\Projects\ClassLibrary\ClassLibrary\BaseService.cs:line 311 at
ServiceAtBase.UpdateService.OnStart(String[] args) in
C:\Users\Neemus\Documents\Visual Studio
2008\Projects\ServiceAtBase\ServiceAtBase\UpdateService.cs:line 24 at
System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object state)

2)Microsoft Outlook has encountered an error and needs to close.

3)outlookObj.AnserWizard threw an exception of type
'System.Runtime.InteropServices.COMException'

Here is my code:

bool isOutlookRunning = false;
Process[] process = null;
process = Process.GetProcessesByName("OUTLOOK");
if (process.Length > 0)
isOutlookRunning = true;
try
{
obj2 = new Microsoft.Office.Interop.Outlook.Application();
}
catch
{
if (isOutlookRunning)
{
foreach (Process pr in process)
{
if (pr.ProcessName.Contains("OUTLOOK"))
{
pr.CloseMainWindow();
}
}
}
else
Process.Start("OUTLOOK");
obj2 = new Microsoft.Office.Interop.Outlook.Application();
}
ns = obj2.GetNamespace("MAPI");
inboxFolder =
ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
outboxFolder =
ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderOutbox);
process = null;



These errors are not fixed as well.

At one time one exception will pop up and other time some other.

Any help will appreciated...

Thanks
 
Ad

Advertisements

S

Sue Mosher [MVP]

Outlook is not suitable to automate from a Windows service. If your aim is
simply to send email messages, have you considered using System.Net.Mail
instead?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top