none
Update Message on ItemSend RRS feed

  • Question

  • Is it possible to pass a string into the ItemSend event handler in Outlook 2010 & VSTO C# .Net 4?
    The user chooses an option in a custom ribbon, and I would like the string sent to ThisAddIn so that
    I can update the e-mail message. Any pointers or assistance greatly appreciated.

    Cheers
    Tom Houston
    Monday, February 6, 2012 12:37 PM

Answers

  • Keep in mind that having a user property will cause your message to be sent in the TNEF (winmail.dat) format.

    Either delete the property or explicilty tell Outlook not to use the TNEF format by setting the UseTnef named property (DASL name http://schemas.microsoft.com/mapi/id/{00062008-0000-0000-C000-000000000046}/8582000B).


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.2 is now available!
    Monday, February 6, 2012 4:46 PM
  • as an callback action for your custom ribbon/button clicked, add user property for an item. In item send event try to read that user property. Be advised though that this might not work on meetings/meeting requests.
    Monday, February 6, 2012 1:36 PM
  • Hi Tom,

    Thanks for posting in the MSDN Forum.

    I'm not sure about what you want to do. I suppose a scenario to reproduce your issue:

    1. You created an Outlook add-in application.
    2. You subscribed an ItemSend event in the ThisAddIn_Startup method.
    3. You set an option on your custom ribbon to access a string.
    4. When an e-mail sending you will use the string which you get from your custom ribbon to change your mail.

    Is this scenario right? If I have misunderstood anything, please feel free to let me know.

    This is the snippet which I used to reproduce that scenario:

    Ribbon1.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Office.Tools.Ribbon;
    using Outlook = Microsoft.Office.Interop.Outlook;
    
    namespace OutlookAddIn1
    {
        public partial class Ribbon1
        {
            private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
            {
    
            }
    
            private void button1_Click(object sender, RibbonControlEventArgs e)
            {
                Globals.ThisAddIn.myOption = "This is a test";
            }
        }
    }

    ThisAddIn.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;
    using Outlook = Microsoft.Office.Interop.Outlook;
    using Office = Microsoft.Office.Core;
    using System.Windows.Forms;
    using System.Text.RegularExpressions;
    
    namespace OutlookAddIn1
    {
        public partial class ThisAddIn
        {
            public string myOption { set; get; }
            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                Application.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);
            }
    
            void Application_ItemSend(object Item, ref bool Cancel)
            {
                if (Item is Outlook.MailItem)
                {
                    Outlook.MailItem objMailItem = (Outlook.MailItem)Item;
                    string html = objMailItem.HTMLBody;
                    Regex regex = new Regex("");
                    objMailItem.HTMLBody = regex.Replace(html, "<input +="" ??="" ?\?="" hidden\??="" myOption="" type="\" value="\" />");
                }
            }
    
            private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
            {
            }
    
            #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);
            }
            
            #endregion
        }
    }

    I think you need provide more detailed information to support your goal "update the e-mail". I think what I shown to you can be conside as a kind of update. Also you can update your mail via UserProperty, The snippet like that:

    ThisAddIn.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;
    using Outlook = Microsoft.Office.Interop.Outlook;
    using Office = Microsoft.Office.Core;
    using System.Windows.Forms;
    using System.Text.RegularExpressions;
    
    namespace OutlookAddIn1
    {
        public partial class ThisAddIn
        {
            public string myOption { set; get; }
            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                Application.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);
            }
    
            void Application_ItemSend(object Item, ref bool Cancel)
            {
                if (Item is Outlook.MailItem)
                {
                    Outlook.MailItem objMailItem = (Outlook.MailItem)Item;
                    Outlook.UserProperty objUserProperty = objMailItem.UserProperties.Add("Information", Outlook.OlUserPropertyType.olText);
                    objUserProperty.Value = myOption;
                }
            }
    
            private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
            {
            }
    
            #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);
            }
            
            #endregion
        }
    }

    I hope what I said can help you.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us


    Tuesday, February 7, 2012 7:06 AM
    Moderator

All replies

  • as an callback action for your custom ribbon/button clicked, add user property for an item. In item send event try to read that user property. Be advised though that this might not work on meetings/meeting requests.
    Monday, February 6, 2012 1:36 PM
  • Keep in mind that having a user property will cause your message to be sent in the TNEF (winmail.dat) format.

    Either delete the property or explicilty tell Outlook not to use the TNEF format by setting the UseTnef named property (DASL name http://schemas.microsoft.com/mapi/id/{00062008-0000-0000-C000-000000000046}/8582000B).


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.2 is now available!
    Monday, February 6, 2012 4:46 PM
  • Is there any example code/documentation out there on using user properties?

    Cheers
    Tom Houston
    Monday, February 6, 2012 6:20 PM
  • See http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.userproperties.aspx

     


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.2 is now available!
    Monday, February 6, 2012 6:26 PM
  • Thanks.. And out of interest is the property stored in the message header?

    Cheers
    Tom Houston
    Monday, February 6, 2012 7:29 PM
  • Do you mean a MIME header? Outlook does not use MIME internally.

     


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.2 is now available!
    Monday, February 6, 2012 8:22 PM
  • Hi Tom,

    Thanks for posting in the MSDN Forum.

    I'm not sure about what you want to do. I suppose a scenario to reproduce your issue:

    1. You created an Outlook add-in application.
    2. You subscribed an ItemSend event in the ThisAddIn_Startup method.
    3. You set an option on your custom ribbon to access a string.
    4. When an e-mail sending you will use the string which you get from your custom ribbon to change your mail.

    Is this scenario right? If I have misunderstood anything, please feel free to let me know.

    This is the snippet which I used to reproduce that scenario:

    Ribbon1.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Office.Tools.Ribbon;
    using Outlook = Microsoft.Office.Interop.Outlook;
    
    namespace OutlookAddIn1
    {
        public partial class Ribbon1
        {
            private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
            {
    
            }
    
            private void button1_Click(object sender, RibbonControlEventArgs e)
            {
                Globals.ThisAddIn.myOption = "This is a test";
            }
        }
    }

    ThisAddIn.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;
    using Outlook = Microsoft.Office.Interop.Outlook;
    using Office = Microsoft.Office.Core;
    using System.Windows.Forms;
    using System.Text.RegularExpressions;
    
    namespace OutlookAddIn1
    {
        public partial class ThisAddIn
        {
            public string myOption { set; get; }
            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                Application.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);
            }
    
            void Application_ItemSend(object Item, ref bool Cancel)
            {
                if (Item is Outlook.MailItem)
                {
                    Outlook.MailItem objMailItem = (Outlook.MailItem)Item;
                    string html = objMailItem.HTMLBody;
                    Regex regex = new Regex("");
                    objMailItem.HTMLBody = regex.Replace(html, "<input +="" ??="" ?\?="" hidden\??="" myOption="" type="\" value="\" />");
                }
            }
    
            private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
            {
            }
    
            #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);
            }
            
            #endregion
        }
    }

    I think you need provide more detailed information to support your goal "update the e-mail". I think what I shown to you can be conside as a kind of update. Also you can update your mail via UserProperty, The snippet like that:

    ThisAddIn.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;
    using Outlook = Microsoft.Office.Interop.Outlook;
    using Office = Microsoft.Office.Core;
    using System.Windows.Forms;
    using System.Text.RegularExpressions;
    
    namespace OutlookAddIn1
    {
        public partial class ThisAddIn
        {
            public string myOption { set; get; }
            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                Application.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);
            }
    
            void Application_ItemSend(object Item, ref bool Cancel)
            {
                if (Item is Outlook.MailItem)
                {
                    Outlook.MailItem objMailItem = (Outlook.MailItem)Item;
                    Outlook.UserProperty objUserProperty = objMailItem.UserProperties.Add("Information", Outlook.OlUserPropertyType.olText);
                    objUserProperty.Value = myOption;
                }
            }
    
            private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
            {
            }
    
            #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);
            }
            
            #endregion
        }
    }

    I hope what I said can help you.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us


    Tuesday, February 7, 2012 7:06 AM
    Moderator