How to get mailitem of a new mail in C++

K

Kumar

Hello all,

I am writing a COM Add-In program in MSVC++ to move spam messages to
spam folder. The program has to check message body for specific words
and move it to folder when the new mail arrives in outlook. My problem
is how to get mailitem of new message in OnNewMail method when it
arrives. My code sample is as following.


_ATL_FUNC_INFO OnNewMailInfo = {CC_STDCALL, VT_EMPTY, 0 };
void __stdcall OnNewMail( );

BEGIN_SINK_MAP(CApplication)
SINK_ENTRY_INFO(1, __uuidof(Outlook::ApplicationEvents), 0xf003,
OnNewMail, &OnNewMailInfo)
END_SINK_MAP()

void __stdcall CApplication::OnNewMail( )
{
//
// get mail item here
// check mail body for specific words
// move it to spam folder
//
}


Please help.

Thank You,
Kumar
 
D

Dmitry Streblechenko

In case of Outlook 2003 you can use NewMailEx event which passes a list of
entry ids to the event handler, otherwise you can use
MAPIFolder.Items.ItemAdd event instead (where MAPIFolder points to the
Inbox)

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 
Joined
Aug 13, 2010
Messages
1
Reaction score
0
Hi Dmitry,


In that case, how do I hook my function to the Outlook events?
I have created an application pointer _ApplicationPtr, which I thought was the event source. But when I use __hook() function, it told me that my class must be a coclass. This is how I inilitialize my Outlook COM:

COutlookAutomation::OutlookAutomation()

{

try

{

HRESULT hr;

hr = p_OutlookApplication.CreateInstance(
"Outlook.Application");

if (FAILED(hr))

{

MessageBox(NULL, L
"Outlook instance failed to load.", L"ERROR", MB_OK);

return;

}

p_Namespace = p_OutlookApplication->GetNamespace(L
"MAPI");

if (p_Namespace == 0x00000000)

{

MessageBox(NULL, L
"Failed to gain Outlook namespace.", L"ERROR", MB_OK);

return;

}

__hook(&ApplicationEvents_11::NewMailEx, p_OutlookApplication, &COutlookAutomation::TestFunction);
}
catch (_com_error e)

{

MessageBox(NULL, e.Description(), L
"ERROR", MB_OK);

return;
}
}
I used CoInitializeEx instead of CoClass.

Will you be able to advice me what went wrong here? Or anybody?

Thanks
 

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