简体   繁体   中英

C# Attach to open outlook email, edit and send

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 methods outApp = Marshal.GetActiveObject("Outlook.Application") as Application , but then I cannot cast it to a MailItem type in order to manipulate its elements eg 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);

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM