How to catch a mail before it is sent

P

pgompel

Hi everyone,

I have quite a big problem here : I try to create a C++ COM add-in fo
Outlook 2000. My goal is to add an information to a mail, then to sig
the mail in a special way before it is sent.

I achieved adding the header to the mail, but I can't manage to catc
the event SendItem. Someone could help me please ?

There is parts of my code :

// Connect.h : Declaration of the CConnect

#pragma once
#include "resource.h" // main symbols


extern _ATL_FUNC_INFO OnClickButtonInfo;
extern _ATL_FUNC_INFO OnMailSendInfo;

// CConnect
class ATL_NO_VTABLE CConnect :
public CComObjectRootEx<CComSingleThreadModel>,
public CComCoClass<CConnect, &CLSID_Connect>,

public IDispatchImpl<AddInDesignerObjects::_IDTExtensibility2
&AddInDesignerObjects::IID__IDTExtensibility2
&AddInDesignerObjects::LIBID_AddInDesignerObjects, 1, 0>,
public IDispEventSimpleImpl<1, CConnect
&__uuidof(Office::_CommandBarButtonEvents)>,
public IDispEventSimpleImpl<2, CConnect
&__uuidof(Outlook::ApplicationEvents)>
{
public:
typedef IDispEventSimpleImpl<1, CConnect
&__uuidof(Office::_CommandBarButtonEvents)> CommnandButton1Events;
typedef IDispEventSimpleImpl<2, CConnect
&__uuidof(Outlook::ApplicationEvents)> Application2Events;
CConnect()
{
}

DECLARE_REGISTRY_RESOURCEID(IDR_ADDIN)
DECLARE_NOT_AGGREGATABLE(CConnect)

BEGIN_COM_MAP(CConnect)
COM_INTERFACE_ENTRY(IDispatch)
COM_INTERFACE_ENTRY(AddInDesignerObjects::IDTExtensibility2)
END_COM_MAP()


BEGIN_SINK_MAP(CConnect)
SINK_ENTRY_INFO(
1,
__uuidof(Office::_CommandBarButtonEvents),
0x01,
OnClickButton,
&OnClickButtonInfo
)
SINK_ENTRY_INFO(
2,
__uuidof(Outlook::ApplicationEvents),
0x01,
OnMailSend,
&OnMailSendInfo
)
END_SINK_MAP()

DECLARE_PROTECT_FINAL_CONSTRUCT()

HRESULT FinalConstruct()
{
return S_OK;
}

void FinalRelease()
{
}

public:
//IDTExtensibility2 implementation:
STDMETHOD(OnConnection)(IDispatch * Application
AddInDesignerObjects::ext_ConnectMode ConnectMode, IDispatc
*AddInInst, SAFEARRAY **custom);
STDMETHOD(OnDisconnection)(AddInDesignerObjects::ext_DisconnectMod
RemoveMode, SAFEARRAY **custom );
STDMETHOD(OnAddInsUpdate)(SAFEARRAY **custom );
STDMETHOD(OnStartupComplete)(SAFEARRAY **custom );
STDMETHOD(OnBeginShutdown)(SAFEARRAY **custom );


void __stdcall OnClickButton(IDispatch * Ctrl,VARIANT_BOOL
CancelDefault);
void __stdcall OnMailSend(IDispatch * Ctrl,VARIANT_BOOL
CancelDefault);


private :
CComQIPtr <Outlook::_Application> spApp;
IDispatch *spApplication;
CComPtr<Office::_CommandBarButton> m_spButton;
};


OBJECT_ENTRY_AUTO(__uuidof(Connect), CConnect)



////////////
and there is the cpp file :

// Connect.cpp : Implementation of CConnect
#include "stdafx.h"
#include "AddIn.h"
#include "Connect.h"

#include "fenetreLabel.h"

extern CAddInModule _AtlModule;

_ATL_FUNC_INFO OnClickButtonInf
={CC_STDCALL,VT_EMPTY,2,{VT_DISPATCH,VT_BYREF | VT_BOOL}};
_ATL_FUNC_INFO OnMailSendInf
={CC_STDCALL,VT_EMPTY,2,{VT_DISPATCH,VT_BYREF | VT_BOOL}};

// When run, the Add-in wizard prepared the registry for the Add-in.
// At a later time, if the Add-in becomes unavailable for reasons suc
as:
// 1) You moved this project to a computer other than which is wa
originally created on.
// 2) You chose 'Yes' when presented with a message asking if yo
wish to remove the Add-in.
// 3) Registry corruption.
// you will need to re-register the Add-in by building th
MyAddin21Setup project
// by right clicking the project in the Solution Explorer, the
choosing install.

// CConnect
STDMETHODIMP CConnect::OnConnection(IDispatch *pApplication
AddInDesignerObjects::ext_ConnectMode ConnectMode, IDispatc
*pAddInInst, SAFEARRAY ** /*custom*/ )
{
spApplication = pApplication;

CComPtr <Office::_CommandBars> spCmdBars;
CComPtr <Office::CommandBar> spCmdBar;



CComQIPtr <Outlook::_Application> spApp(pApplication);
ATLASSERT(spApp);

CComPtr<Outlook::_Explorer> spExplorer;
spExplorer = spApp->ActiveExplorer();

HRESULT hr = spExplorer->get_CommandBars(&spCmdBars);
if (FAILED(hr)) {
return hr;
}

ATLASSERT(spCmdBars);

CComVariant vName("My nice add-in");
CComPtr <Office::CommandBar> spNewCmdBar;

CComVariant vPos(1);

CComVariant vTemp(VARIANT_TRUE);
CComVariant vEmpty(DISP_E_PARAMNOTFOUND, VT_ERROR);

spCmdBars->Add(vName, vPos, vEmpty, vTemp, &spNewCmdBar);


CComPtr <Office::CommandBarControls> spBarControls;
spNewCmdBar->get_Controls(&spBarControls);
ATLASSERT(spBarControls);

CComVariant vToolBarType(1);
CComVariant vShow(VARIANT_TRUE);

CComPtr <Office::CommandBarControl> spNewBar;

spBarControls->Add(vToolBarType, vEmpty, vEmpty, vEmpty, vShow,
&spNewBar);
ATLASSERT(spNewBar);

CComQIPtr <Office::_CommandBarButton> spCmdButton(spNewBar);
ATLASSERT(spCmdButton);

HBITMAP hBmp = (HBITMAP)::LoadImage(_AtlModule.GetResourceInstance(),
MAKEINTRESOURCE(IDB_BITMAP_ICONE_LABEL), IMAGE_BITMAP, 0, 0,
LR_LOADMAP3DCOLORS);

::OpenClipboard(NULL);
::EmptyClipboard();
::SetClipboardData(CF_BITMAP, (HANDLE)hBmp);
::CloseClipboard();
::DeleteObject(hBmp);

spCmdButton->put_Style(Office::msoButtonIconAndCaption);

hr = spCmdButton->PasteFace();
if (FAILED(hr)) {
return hr;
}

spCmdButton->put_Visible(VARIANT_TRUE);
spCmdButton->put_Caption(OLESTR("Mail with header"));
spCmdButton->put_Enabled(VARIANT_TRUE);
spCmdButton->put_TooltipText(OLESTR("Adds a special header to a
mail"));
spCmdButton->put_Tag(OLESTR("Marked mail !!"));

spNewCmdBar->put_Visible(VARIANT_TRUE);

m_spButton = spCmdButton;


CommnandButton1Events::DispEventAdvise((IDispatch*)m_spButton);

////////////////////////////

Application2Events::DispEventAdvise((IDispatch*)spApp);


return S_OK;
}

STDMETHODIMP
CConnect::OnDisconnection(AddInDesignerObjects::ext_DisconnectMode
/*RemoveMode*/, SAFEARRAY ** /*custom*/ )
{

CommnandButton1Events::DispEventUnadvise((IDispatch*)m_spButton);
Application2Events::DispEventUnadvise((IDispatch*)spApp);

return S_OK;
}

STDMETHODIMP CConnect::OnAddInsUpdate (SAFEARRAY ** /*custom*/ )
{
return S_OK;
}

STDMETHODIMP CConnect::OnStartupComplete (SAFEARRAY ** /*custom*/ )
{
return S_OK;
}

STDMETHODIMP CConnect::OnBeginShutdown (SAFEARRAY ** /*custom*/ )
{
return S_OK;
}



void __stdcall CConnect::OnClickButton(IDispatch* Ctrl,VARIANT_BOOL *
CancelDefault) {

PluginOutlookCpp::fenetreLabel * fLabel;
fLabel = new PluginOutlookCpp::fenetreLabel(spApplication);

fLabel->ouvrir();



}


void __stdcall CConnect::OnMailSend(IDispatch* Ctrl,VARIANT_BOOL *
CancelDefault) {

MessageBox(NULL, "Mail is being sent !","Advertisement", MB_OK);



}







Thanks a lot for your help !!!
 
T

Tim Pulley

The dispid in your sink map is wrong. It should be 0xF002


SINK_ENTRY_INFO( 2, __uuidof(Outlook::ApplicationEvents), 0xF002,
OnMailSend, &OnMailSendInfo )
 
T

Tim Pulley

I noticed that you call the ActiveExplorer function and use the returned
pointer without validating it. (I'm sure that this is sample code and that
you would never do anything like that in production code.) I just wanted to
make sure that you're aware that ActiveExplorer can return a NULL pointer.
When started programmatically Outlook doesn't automatically create an
Explorer. An example of this would be when a user selects any of the "Send
To" options in the other Office apps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top