1

I am trying to write an Outlook Plugin ( 2007 or greater ) using VSTO. When the user presses the send button on a new email , rather than sending the email I want to save the entire email to a folder ( that the addin creates on first run ).
After an unspecified amount of time in the future when a certain event happens , I then programmatically want to send that email from my saved folder.
Is this even doable? To start with can I save the mail to a special folder instead of sending it . I know I can cancel the send event but how do I save the entire email

Rahul
  • 2,194
  • 4
  • 31
  • 46
  • These references can be helpful : [Application.ItemSend Event](https://msdn.microsoft.com/en-us/library/office/ff865076(v=office.14).aspx) (Detect and cancel sending of a MailItem) , [MailItem.SaveAs Method](https://msdn.microsoft.com/en-us/library/office/ff868727.aspx) (Save the MailItem to a file) , [NameSpace.OpenSharedItem](https://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook._namespace.openshareditem.aspx) (Open saved item from file) and an [Example of Read/Write](https://www.add-in-express.com/creating-addins-blog/2013/12/20/create-outlook-files/) – Insane Mar 25 '15 at 11:11

2 Answers2

0

Setup the event to capture the send event of an E-mail and cancel the E-mail send and then do the needful, of the MailItem.

Sample code below -

public partial class ThisAddIn
{
    private Outlook.Inspectors _inspectors;
    public static Outlook.MailItem theCurrentMailItem;

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        _inspectors = Application.Inspectors;
        _inspectors.NewInspector += _inspectors_NewInspector;

        Application.ItemSend += Application_ItemSend;
    }

    void Application_ItemSend(object Item, ref bool Cancel)
    {
        object item = (Outlook.MailItem)Item;

        if(item != null)
        {
            //theCurrentMailItem.Move(..SomeFolder) // Move or save, saveas as needed
            Cancel = true;
        }
    }

    void _inspectors_NewInspector(Outlook.Inspector inspector)
    {
        if (inspector == null) throw new ArgumentNullException("inspector");

        theCurrentMailItem = null;

        object item = inspector.CurrentItem;
        if (item == null) return;

        if (!(item is Outlook.MailItem)) return;

        theCurrentMailItem = inspector.CurrentItem as Outlook.MailItem;
    }

    private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
    {
    }
}
Sanjay Karia
  • 309
  • 1
  • 3
  • 13
  • In your item send event, why are you processing the last inspector window opened and not the item that was passed to your send handler? – public wireless Mar 31 '15 at 14:57
  • Please ignore that, it was extracted from an existing project - the Item passed in the ItemSend event should be used. – Sanjay Karia Mar 31 '15 at 15:03
0

The ItemSend event of the Application class is fired whenever an Microsoft Outlook item is sent, either by the user through an Inspector (before the inspector is closed, but after the user clicks the Send button) or when the Send method for an Outlook item, such as MailItem, is used in a program. The passed boolean parameter allows to cancel further processing, so you will be able to do whatever you need.

Also you may consider repurposing the Ribbon UI controls (the Send button), see Temporarily Repurpose Commands on the Office Fluent Ribbon for more information. In that case you will be able to cancel the default action and do your own instead. Moreover, you can differentiate whether a new MailItem is going to be sent or not. Be aware, the ItemSend event is fired for all outcoming messages (reply, forward and etc.).

It is not clear what folder you are talking about: is it a folder on the disk or in Outlook?

To place the Outlook item into the folder you can use the Move method.

To save the item to a folder on the disk you can use the SaveAs method which saves the Microsoft Outlook item to the specified path and in the format of the specified file type. If the file type is not specified, the MSG format (.msg) is used. Later you can use the CreateItemFromTemplate method to re-construct the MailItem based on the saved item (.msg file).

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • The link to "re purposing Send" says that the content is out of date. Do you know if this method works on OL 2010 and OL 2013. I Know I know I should not be helpless and try it out :-) but this is faster :-). Do you know for a fact that this works for new versions of OL. Also this is for an OL Folder Thanks – Rahul Mar 25 '15 at 19:44
  • The [Temporarily Repurpose Commands on the Office Fluent Ribbon](https://msdn.microsoft.com/en-us/library/bb462633%28v=office.12%29.aspx?f=255&MSPPError=-2147217396) is alive, i.e. not out of date. The code will work in Office 2007 and later (including Office 2013). – Eugene Astafiev Mar 25 '15 at 20:25