Insert text at the start of mailitem.Body in VSTO Outlook using C# RRS feed

  • Question

  • Hi all, I am trying to create a plugin for Outlook 365 that whenever a user compose a new mail OR Reply OR Reply To All, automatically some greeting text appears at the starting of new mail message.

    I am able to capture all 3 events(New/Reply/Reply All), but whenever inserting text inside the new editor window either the text appends at last of new mail body or all the formatting vanishes.

    I am not so much familiar with VSTO, so took reference from Internet and wrote this piece of code : 

    Outlook.Inspectors inspectors;
            private Outlook.Application _application = null;
            Outlook.Explorer currentExplorer = null;
            private void ThisAddIn_Startup(object sender, System.EventArgs e)
                _application = Globals.ThisAddIn.Application;
                _application.ItemLoad += new Outlook.ApplicationEvents_11_ItemLoadEventHandler(_application_ItemLoad);
                inspectors = this.Application.Inspectors;
                inspectors.NewInspector +=
                new Outlook.InspectorsEvents_NewInspectorEventHandler(Inspectors_NewInspector);
            private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
                // Note: Outlook no longer raises this event. If you have code that 
                //    must run when Outlook shuts down, see
            Outlook.MailItem mi = null;
            private void GenerateItemMessage(object item, string operation)
                mi = item as Outlook.MailItem;
                currentExplorer = this.Application.ActiveExplorer();
                currentExplorer.SelectionChange += new Outlook.ExplorerEvents_10_SelectionChangeEventHandler(CurrentExplorer_Event);
            private void CurrentExplorer_Event()
                if (this.Application.ActiveExplorer().Selection.Count > 0)
                    Outlook.MailItem selObject = this.Application.ActiveExplorer().Selection[1];
                    if (selObject.BodyFormat == Outlook.OlBodyFormat.olFormatHTML)
                        string link = "new string" + Environment.NewLine;
                        selObject.HTMLBody = link + selObject.Body;
            private void ThisAddIn_ReplyAll(object item, ref bool cancel)
                GenerateItemMessage(item, "ReplyToAll");
            private void ThisAddIn_Reply(object item, ref bool cancel)
                GenerateItemMessage(item, "Reply");
            Outlook.ItemEvents_10_Event _item;
            private void _application_ItemLoad(object Item)
                _item = (Outlook.ItemEvents_10_Event)Item;
                _item.ReplyAll += new Outlook.ItemEvents_10_ReplyAllEventHandler(ThisAddIn_ReplyAll);
                _item.Reply += new Outlook.ItemEvents_10_ReplyEventHandler(ThisAddIn_Reply);
            void Inspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector Inspector)
                if (greetings.greetingEnabled == true)
                    Outlook.MailItem mailItem = Inspector.CurrentItem as Outlook.MailItem;
                    if (mailItem != null && mailItem.EntryID == null)
                        mailItem.HTMLBody = mailItem.HTMLBody.Insert(0, "Hi");
            #region DODMethods
            string finalGreets(string recievers)
                    if (recievers is null)
                        return greetings.greetingText + " ," + Environment.NewLine;
                    if (recievers.Split(Convert.ToChar(";")).Length > 1)
                        return greetings.greetingText + " all," + Environment.NewLine;
                        return greetings.greetingText + " " + greetings.greetingGender + " " + recievers + "," + Environment.NewLine;
            #region VSTO generated code
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InternalStartup()
                this.Startup += new System.EventHandler(ThisAddIn_Startup);
                this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);

    Method CurrentExplorer_Event() is responsible to add text inside new mail message body(Where I have wrote the above code provided by you).

    The issue here is, whenever I try to generate text inside new mail body(Or new mail editor), either it appends to the last or it vanishes the format of whole mail.

    I want the new editor opened always with my defined text without modifying any formatting.

    I observed one more issue into this, that whenever reply/reply all event occurs, the defined action is performing twice.

    Please help me in achieving this task.

    Best Regards,

    Ankit Singh Rawat

    Friday, March 6, 2020 9:41 AM

All replies

  • Am having the similar issue, in Reply/ReplyAll am not able to change the HTMLBody of MailItem. Anyone solved this problem?
    Thursday, July 23, 2020 9:02 PM