1

I am trying get some C# to attach to an open reply-email (triggered manually by user), on the already running instance of Outlook (opened manually by user). The code should identify the open reply email, edit the subject line and body of the email and send the email.

The problem is that I get as far as identifying the running instance of Outlook and assigning it to an object using one of the Marshal methodsoutApp = Marshal.GetActiveObject("Outlook.Application") as Application, but then I cannot cast it to a MailItem type in order to manipulate its elements e.g. the subject line, body, etc...something like MailItem mailItem = (MailItem)outApp.CreateItem((OlItemType.olMailItem)); throws an invalid cast exception at runtime.

Apologies if I am wrong, but could not find a single example close to this exact sequence of events, one of the closer ones is this post c# outlook open existing instance and reply to email but then it goes a whole different way. There are tons of posts on how to use the Microsoft.Office.Interop.Outlook to OPEN and then use an instance of Outlook, but hardly anything (that I could find) on how to use an open instance. Any help is appreciated, thank you.

EDIT 08102019:
The code is used from an RPA platform, so there is no risk of it being picked up as malware. The "user" is just a virtual user on an account with purpose-made permissions and a controlled environment...sorry, nothing dark here :-). Anyway, here is the code I am using at the moment which creates a new instance and saves it to drafts in Outlook. It is not what I set out to do, as I explained above, this is just a temporary fix:

        OutlookApp outlookApp = new OutlookApp();
        MailItem mailItem = (MailItem)outlookApp.CreateItem(OlItemType.olMailItem);
        mailItem.To = "test@test.com";
        mailItem.Subject = "Test Email Generation";
        mailItem.HTMLBody = "<html><body>This is the body of the email.</strong>.<br/> This is another line in the body of the email.</body></html>";
        mailItem.Display(false);

        System.Threading.Thread.Sleep(3000);

        mailItem.Close(OlInspectorClose.olSave);
        Marshal.ReleaseComObject(outlookApp);
RoboPuppy
  • 13
  • 4
  • You should post your full code – Sorceri Oct 07 '19 at 17:00
  • Communicating with the Exchange server in background would be easier. – Holger Oct 07 '19 at 17:09
  • 1
    Whoa. A background application that changes the contents of an email that the user is *actively working with*? A lot of malware is designed to function like this, be forewarned it could trigger enterprise infection catching software very easily. – gravity Oct 07 '19 at 17:21

1 Answers1

1

To get the opened mail item in the inspector window you need:

  1. Use the ActiveInspector method to get an instance of the Inspector class.
  2. The Inspector.CurrentItem property returns an Object representing the current item being displayed in the inspector.
  3. Set any properties like Subject, Body, Recipients and etc.

To get the inline response in the Explorer window you need to use the Explorer.ActiveInlineResponse property which returns an item object representing the active inline response item in the explorer reading pane.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • I created an ActiveInspector instance and used its CurrentItem method as a MailItem object to access the email properties ' MailItem myMailItem = inspector.CurrentItem; ' then ' myMailItem.Subject', etc... This works well and grants access to the email properties. I note that it deletes the rest of the email contents however. I am not sure I understand how to use the Explorer.ActiveInlineResponse property. Would this allow to preserve the existing text in the reply email body and just add to it at the top? – RoboPuppy Oct 08 '19 at 09:59