COMException sending email with Outlook 2010 on Windows Server 2008 R2

Nov 9, 2012 at 4:34 PM

In a production server when I access the Recipients object (i.e. mailItem.Recipients.Add(...))

I get this error:

The error occurred:
    Type:       COMException
    Message:    See inner exception(s) for details.
    Data:       0 entries
    Stack trace:

   in NetOffice.Invoker.PropertyGet(COMObject comObject, String name, Object[] paramsArray)
   in NetOffice.OutlookApi._MailItem.get_Recipients()
   in PitagoraDoc.Module.BusinessLogic.OutlookHelper.SendEmail(Dictionary`2 destinatari, String oggetto, String corpo, FileData[] allegati)
   in PitagoraDoc.Module.BusinessLogic.ComunicazioneHelper.SendMail(Comunicazione comunicazione)
   in PitagoraDoc.Module.Controllers.ComunicazioneController.SalvaEInvia_Execute(Object sender, SimpleActionExecuteEventArgs e)
   in DevExpress.ExpressApp.Actions.ActionBase.ExecuteCore(Delegate handler, ActionBaseEventArgs eventArgs)
    ----------------
    InnerException:

            Type:       TargetInvocationException
            Message:    Eccezione generata dalla destinazione di una chiamata.
            Data:       0 entries
            Stack trace:

   in System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters)
   in System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
   in NetOffice.Invoker.PropertyGet(COMObject comObject, String name, Object[] paramsArray)
            ----------------
            InnerException:

                    Type:       COMException
                    Message:    Operazione terminata. (Eccezione da HRESULT: 0x80004004 (E_ABORT))
                    Data:       0 entries
                    Stack trace:


                    InnerException is null

---------------------------------------------------------------------------

My code is pretty simple:

...
Outlook.Application outlookApplication = new Outlook.Application();
Outlook.MailItem mailItem = outlookApplication.CreateItem(OlItemType.olMailItem) as Outlook.MailItem;
mailItem.Recipients.Add("xxx@xxx.xx");    //<- Error
...

I noted that using another NetOffice API in my application (Session.GetDefaultFolder) first and then try to send a email the error go away.

So as a workaround I call:
Outlook.MAPIFolder contactFolder = outlookApplication.Session.GetDefaultFolder(OlDefaultFolders.olFolderContacts);

just after the creation of outlookApplication.

What might have happened?

Thank you, Giorgio.

Coordinator
Nov 10, 2012 at 3:00 AM

hello,

can you translate the message " Eccezione generata dalla destinazione di una chiamata." for me???
It looks like a security exception btw.  is your application a desktop application (console, winforms,...) or IIS hosted ???
please note: outlook use variuos security features and parts of the object model was restricted. the user has to agree the accessing from a third party component in a special dialog box. automating office in a server system is generaly not a good idea. did you see any chance to use the Exchange Server Web API (EWS)?

Sebastian

Nov 12, 2012 at 9:12 AM

Hello Sebastian,

can you translate the message " Eccezione generata dalla destinazione di una chiamata." for me???
Exception has been thrown by the target of an invocation

It looks like a security exception btw. is your application a desktop application (console, winforms,...) or IIS hosted ???
Is a terminal services server, so, OS apart, is a shared desktop pc.

User access my application (Windows forms application) either by remote desktop or remote application.

In the tests I am doing I impersonate the Administrator and the outlook security has been adjusted in "Trust center/Programmatic access/Never warn me about suspicious activity" so no dialog box pop up.

In development environment (Windows 7 64 bit) all it is ok.

User send emails from the application and want to find it later in sent items in Outlook.

did you see any chance to use the Exchange Server Web API (EWS)

Then I don't need it...?

At the end what look me strange, or significant, is that after a call to "Outlook.MAPIFolder contactFolder = outlookApplication.Session.GetDefaultFolder(OlDefaultFolders.olFolderContacts);" something change in internal state of Outlook end subsequent call to "mailItem.Recipients" has success from this point till the application shutdown.

But my knowledge with Outlook is poor and I don't figure out the reason.

Thank you.

giorgio.vaccarino@wpformat.com

Coordinator
Nov 13, 2012 at 11:37 PM

hmm i can't reproduce your scenario because i dont have a terminal server for my vm's. thats a problem.

the Microsoft EWS API is a nice way to deal with the exchange server via XML based communication, send mails, create contacts and all the other stuff.
you dont need automate a local application, all what you need is an impersonation account for the exchange access.

"send e-mail example"
http://msdn.microsoft.com/en-us/library/exchange/dd633628%28v=exchg.80%29.aspx

i use the EWS in a lot  of server solutions.

another possible solution for you is a local desktop proxy ( maybe an outlook addin ) and you send orders to the local addin via remoting. crazy but i think it works also. the google translation example addin in the download section shows a similar scenario and use IPC remoting. you can take look into this. you may need TCP remoting because IPC remoting doesnt works in different user accounts.

Sebastian

Nov 14, 2012 at 8:17 AM

Hello Sebastian,

first I will surely give a try to EWS because I like the idea of a message based communication between applications.

Thank you for your advices.

Giorgio.

Coordinator
Nov 14, 2012 at 8:20 AM

if you need help to start with the EWS API  then please feel free to contact me.

Sebastian