0
votes

I would like that when a user sends his mail, a copy of it is made, and sent to another person, to be archived. The user uses a keyword to specify whether the mail should be archived. (I need to add some data in the second mail, that should not appear in the 1st)

So I developed in C# an addin, where I intercept the click "sent" event to copy the mail and send it. Only, it is impossible to send a second email in this event. So I thought about adding a callback to my event, so that once the first email is sent, the second one is sent, but I can't implement a callback to my code (I don't know how to do it on the event, or even if it's possible)

    Outlook.MailItem mailCopy; //Copy of my first mail
    bool sndCopy = false;
    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        this.Application.ItemSend += new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend); //event click send
    }
    private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
    {
    }
    private void Application_ItemSend(object Item, ref bool Cancel)
    {
        Outlook.Application oApp;
        Outlook._NameSpace oNS;
        Outlook.MAPIFolder oFolder;
        oApp = new Outlook.Application();
        oNS = (Outlook._NameSpace)oApp.GetNamespace("MAPI");

        Outlook.MailItem mail1 = (Outlook.MailItem)Item;


        this.mailCopy = (Outlook.MailItem)Item;

        Outlook.MailItem mail = (Outlook.MailItem)Item;

        foreach(Outlook.Recipient rcp in mail.Recipients){
           if(rcp.Address == "Robots"){
               this.sndCopy = true;
               break;
            }
        }

    }

two emails must be sent, the first one that can be sent to anyone, a second one that must be sent to a robot for archiving at an email address ([email protected] for example) with other additional data that cannot appear in the 1st

Edit Solution : I modified my code by adding an event on the creation of a file, when I save my message in a directory

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        FileSystemWatcher fileSystemWatcher = new FileSystemWatcher();
        fileSystemWatcher.Path = path;


        this.Application.ItemSend += new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend); //event click send

        fileSystemWatcher.Created += FileSystemWatcher_Created;
        fileSystemWatcher.EnableRaisingEvents = true;
    }

    private void Application_ItemSend(object Item, ref bool Cancel)
    {


        Outlook.Application oApp;
        Outlook._NameSpace oNS;
        oApp = new Outlook.Application();

        oNS = (Outlook._NameSpace)oApp.GetNamespace("MAPI");

        Outlook.MailItem mail = (Outlook.MailItem)Item;

        mail.SaveAs(path + "\\files.msg", Microsoft.Office.Interop.Outlook.OlSaveAsType.olMSG);

    }

    private void FileSystemWatcher_Created(object sender, FileSystemEventArgs e)
    {
        Outlook.Application oApp = new Outlook.Application();
        Outlook.MailItem msg = (Outlook.MailItem)oApp.CreateItemFromTemplate(path + "\\files.msg", Type.Missing);
        msg.Send();
        File.Delete(path + "\\files.msg");
    }
1

1 Answers

1
votes

Create a timer (use the Timer class from the Forms namespace as it fires on the primary thread) and enable it in the Application.ItemSend event. Create the new message, save it, store its entry id in a list. When the timer fires, disable the timer, open the messages (Namespace.GetItemfromID) using the entry ids saved in your list, send them, clear the list.