none
How to know new mail created with "Send to mail recipient" command? RRS feed

  • Question

  • Hi all,

    I am developing new com addin with outlook 2010. In onSendItem event, i am creating new mail item , filling new mail item fields and submitting new mail. And than i am closing user mail inspector and deleting old mail. 

    But when the user makes "Send to mail recipient" on any file and opens new inspector and clicks send button. I am creation new mail item,filling its fields and submitting new mail. It ok no problem. But when i tried to close use new mail inspector and try to delete user mail item . It gives "unknown exception" error. Also if i close inspector with code outlook gives error and closed.

    My question is that,

    How can we know user clicked "Send to mail recipient" and want to send new mail ? How can i know this in onSendItem() event.?

    Thanks

    Tuesday, January 31, 2012 9:09 AM

Answers

  • You can cancel the ItemSend event by setting the Cancel parameter to true, and you do that already.

    After you reopen the message in the timer event handler, call MailItem.GetInspector to get the Inspector object.

     


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.2 is now available!
    • Marked as answer by Ramazan Girgin Thursday, February 2, 2012 12:27 PM
    Wednesday, February 1, 2012 2:40 PM
  • Why are you using Application.ActiveInspector? The item being sent is passed as the parameter to the Application.ItemSend event.

    You should not delete the item while the ItemSend event is being executed. Store the item's entry id in a global/class variable in the ItemSend event and start a timer. When the timer fires, open the item using Namespace.GetItemFromID, then call MailItem.Delete.

    The important point is that you need to be out of the ItemSend event handler. The timer internal can 0 for all practical purposes.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.2 is now available!
    • Marked as answer by Ramazan Girgin Thursday, February 2, 2012 12:26 PM
    Wednesday, February 1, 2012 1:54 PM

All replies

  • What is your code?

     


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.2 is now available!
    Tuesday, January 31, 2012 1:10 PM
  • My code like this code block.
    STDMETHODIMP CConnect::OnConnection(IDispatch *pApplication, ext_ConnectMode iConnectMode, IDispatch *pAddInInst, SAFEARRAY ** iCustom )
    {
    	//Save a reference to the application object...
    	pApplication->QueryInterface(__uuidof(IDispatch), (LPVOID*)&m_pApplication);
    	if (m_pApplication)
    	{
    		EStartOprManager::getInstance(m_pApplication)->handleStart();
    	}			
    	// If we are connecting during startup, we should wait for OnStartupComplete
    	// before modifying the user-interface and prompting the user. Otherwise, we
    	// can call OnStartupComplete to do the work now...
    	if (iConnectMode != ext_cm_Startup)
    	{
    		OnStartupComplete(iCustom);
    	}	
    	return S_OK;
    }
    
    STDMETHODIMP CConnect::OnStartupComplete (SAFEARRAY ** /*custom*/ )
    {		
    	ESYA_OUTLOOK_ADDIN_FUNC_BEGIN;
    	mpAppEventHandler = new EApplicationEventHandler();
    	mpAppEventHandler->AddRef();
    	mpAppEventHandler->AttachToSource(m_pApplication);
    	mpAppEventHandler->setApplication(m_pApplication);
    }
    
    
    
    STDMETHODIMP EApplicationEventHandler::Invoke(DISPID dispIdMember, REFIID riid, LCID lcid,   
                                      WORD wFlags, DISPPARAMS* pDispParams,   
                                      VARIANT* pVarResult, EXCEPINFO* pExcepInfo,   
                                      UINT* puArgErr)   
    { 	
    	 switch(dispIdMember)   
        {   
    	 
         case 0x0000f002:// OnItemSend()             
    			if(pDispParams->cArgs !=0)
    			{
    				Outlook::_InspectorPtr pInspector;
    				hr = mpApplication->ActiveInspector(&pInspector);
    				if (FAILED(hr))
    				{			
    					break;
    				}				
    
    				Outlook::_MailItemPtr pMailItem=NULL;
    				hr = pInspector->get_CurrentItem((IDispatch **)&pMailItem);
    				if (FAILED(hr))
    				{
    					ESYA_OUTLOOK_ADDIN_FUNC_ERROR("Mail Item alýnamadý.");
    					break;
    				}			
                                     //In that point how can i know is user clicked "Send to mail recipient" or new mail button ?
                           
    				/***********************/
    				//In here i am creating new tmp mail with MAPI object,filling some fields and calling MAPI SubmitMessage()
    				/***********************/	
    							
    				//In that point i want to cancel sending user mail, close inspector
    				//Delete saved mail.
    					
    				VARIANT_BOOL * retValue = pDispParams->rgvarg[0].pboolVal;
    				*retValue = VARIANT_TRUE;
    				pMailItem->Delete();//Gives -2147352567(Exception Occured error)
    				pInspector->Close(Outlook::OlInspectorClose::olSave);//If i call this code outlook closed while giving unexpected error
    				return S_OK;
    		      }					
                }   
                break;
        }	
    }


    Tuesday, January 31, 2012 2:06 PM
  • I want to point a one situation.

    When user clicks new mail button in main explorer and fills mail fields and submits mail above code works well.

    But when user selects "Send to -> Mail Recipient" on any file and submits mail above code gives error which i wrote above.

    I don't understand why can't i delete mail item and close inspector on "Send to -> Mail Recipient" case.

    Thanks in advance

    Tuesday, January 31, 2012 6:37 PM
  • Is outlook running when user uses 'send to' ? Try to change order : first close inspector then delete mail item.

    Wednesday, February 1, 2012 9:24 AM
  • Yes,Outlook is running when user clicks "send to".

    I have tried closing first inspector and only closing inspector. It didn't work. Outlook gived unexpected error and closed

    Wednesday, February 1, 2012 9:52 AM
  • Why are you using Application.ActiveInspector? The item being sent is passed as the parameter to the Application.ItemSend event.

    You should not delete the item while the ItemSend event is being executed. Store the item's entry id in a global/class variable in the ItemSend event and start a timer. When the timer fires, open the item using Namespace.GetItemFromID, then call MailItem.Delete.

    The important point is that you need to be out of the ItemSend event handler. The timer internal can 0 for all practical purposes.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.2 is now available!
    • Marked as answer by Ramazan Girgin Thursday, February 2, 2012 12:26 PM
    Wednesday, February 1, 2012 1:54 PM
  • you are right i can get mailitem from parameter.

    You are also right timer can be another method for deleting old item but i can not find how cancel sending item in Application.ItemSend event and close the inspector.

    I wrote in my previous post if close inspector ,outlook gives unexpected error.

    Is there any way to abort sending this mail and close inspector screen. I thing  I can delete item later with timer.



    Wednesday, February 1, 2012 2:29 PM
  • You can cancel the ItemSend event by setting the Cancel parameter to true, and you do that already.

    After you reopen the message in the timer event handler, call MailItem.GetInspector to get the Inspector object.

     


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.2 is now available!
    • Marked as answer by Ramazan Girgin Thursday, February 2, 2012 12:27 PM
    Wednesday, February 1, 2012 2:40 PM
  • Closing the item in any other event handler such as ItemSend() also won't work and should be done from the timer handler.If your timer handler both closes and deletes the item it would work.You just need to make sure that you get the EntryID of the item, which might involve saving the item, so you can get it later and delete it if you want.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "Ramazan Girgin" <=?utf-8?B?UmFtYXphbiBHaXJnaW4=?=> wrote in message news:8a41b720-8f1a-47f5-8741-01c059857d57...

    you are right i can get mailitem from parameter.

    You are also right timer can be another method for deleting old item but i can not find how cancel sending item in Application.ItemSend event and close the inspector.

    I wrote in my previous post if close inspector ,outlook gives unexpected error.

    Is there any way to abort sending this mail and close inspector screen. I thing  I can delete item later with timer.




    Ken Slovak MVP - Outlook
    Wednesday, February 1, 2012 3:30 PM
    Moderator
  • Dmitry,

    I will try closing and deleting mail in timer event handler.

     

    Ken,

    You said "Closing the item in any other event handler such as ItemSend() also won't work and should be done from the timer handler.". I don't understand that if user i clicks new mail button and submits mail,in ItemSend() method closing and deleting mail item works well. But if user selects "Send to->Mail recipient" it doesn't work.

     What is the difference  between clicking new mail button and selecting send to mail recipient for ItemSend() event ? 

    I'll try timer and i will say results to you.

    Thanks,


    Wednesday, February 1, 2012 7:27 PM
  • Deleting the item or closing it explicitly in a Send() event handler for the item should throw an exception. You should not be able to close or delete the item while the event handler is running.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "Ramazan Girgin" <=?utf-8?B?UmFtYXphbiBHaXJnaW4=?=> wrote in message news:24a50b76-ce74-4ba5-a956-9d8bf2b0efca...

    Dmitry,

    I will try closing and deleting mail in timer event handler.

     

    Ken,

    You said "Closing the item in any other event handler such as ItemSend() also won't work and should be done from the timer handler.". I don't understand that if user i clicks new mail button and submits mail,in ItemSend() method closing and deleting mail item works well. But if user selects "Send to->Mail recipient" it doesn't work.

     What is the difference  between clicking new mail button and selecting send to mail recipient for ItemSend() event ? 

    I'll try timer and i will say results to you.

    Thanks,



    Ken Slovak MVP - Outlook
    Wednesday, February 1, 2012 8:08 PM
    Moderator
  • Hi,

    I have used timer and it is working now. In first i closed inspector and after i deleted mail item.

    Thanks

    Thursday, February 2, 2012 12:26 PM