Extract Attachments in ItemSend

  • Thread starter amitshinde via OfficeKB.com
  • Start date

amitshinde via OfficeKB.com

Hi All,

Currently I am writing COM Add-in (using ATL) to monitor ItemSend event (
monitor for outgoing mails, task assignment and calendar invites ).
I am able to retireve the Recipient information successfully but I have some
problems retrieveing following information

1. I am not able to retrieve the Sender information.
2. Also Is it possible to extract the attachments (attached to outgoing mail)
to disk using Redemption. I get IAttachmentsPtr but I get Count as 0 even I
fthere are attachments.

Here is my code snippet ->
void __stdcall CAddin::OnItemSend(IDispatch* Ctrl,VARIANT_BOOL *
_MailItemPtr spMailItem = NULL;
_TaskRequestItemPtr spTaskItem = NULL;
_MeetingItemPtr spCalendarItem = NULL;
BOOL bMailItem = FALSE,
bTaskItem = FALSE,
bCalndarItem = FALSE;

hRes = Ctrl->QueryInterface(__uuidof(_MailItem), (void**) &spMailItem);
if (NULL == spMailItem)
hRes = Ctrl->QueryInterface(__uuidof(_TaskRequestItem), (void**)
if (NULL == spTaskItem)
hRes = Ctrl->QueryInterface(__uuidof(_MeetingItem), (void**)
if (NULL == spCalendarItem)

bCalndarItem = TRUE;
bTaskItem = TRUE;
bMailItem = TRUE;

if (bMailItem)
CLSID clsid;
HRESULT hr = ::CLSIDFromProgID(L"Redemption.SafeMailItem", &clsid);

CComPtr<Redemption::ISafeMailItem> spSafeMailItem;

hr = ::CoCreateInstance( clsid,

CComPtr<IDispatch> spDispatchMailItem;
hr = spMailItem->QueryInterface(IID_IDispatch, (void**) &spDispatchMailItem)

if (spDispatchMailItem)

BSTR bstrSender, bstrBody;
bstrBody = spSafeMailItem->GetBody();

//VARIANT bstrSenderAddrType;
//bstrSenderAddrType = spSafeMailItem->GetFields(PR_SENDER_ADDRTYPE);
//bstrSender = spSafeMailItem->GetSenderName();
//bstrSender = spSafeMailItem->GetSenderEmailAddress();

//IAddressEntryPtr spAddressEntry;
//spAddressEntry = spSafeMailItem->GetSender();
//bstrSender = spAddressEntry->GetSMTPAddress();

ISafeRecipientsPtr spRecipients = spSafeMailItem->GetRecipients();
ULONG ulCount = spRecipients->GetCount();
ISafeRecipientPtr spRecipient;
for(int iCount=1; iCount <= ulCount; iCount++)
spRecipients->get__Item(iCount, &spRecipient);
IAddressEntryPtr spAddressEntry = spRecipient->GetAddressEntry();
BSTR bstrRecipient = spAddressEntry->GetSMTPAddress();

CComPtr<IAttach> spAttach;
IUnknownPtr spMapiObj;
IAttachmentsPtr spAttachments;
spAttachments = spSafeMailItem->GetAttachments();
IAttachmentPtr spAttachment;
long ulaCount = spAttachments->GetCount();

_IMAPITablePtr spMapitable;
spMapitable = spAttachments->GetMAPITable();
hr = spMapitable->GoToFirst();
IUnknownPtr unkAttachPtr = spMapitable->GetItem();
hr = unkAttachPtr->QueryInterface(__uuidof(IAttachment), (void**)
//here hr return value is E_NOINTERFACE

_variant_t index = 0;
hr = spAttachments->get__Item(index, &spAttachment);
//it exits here
hr = spAttachments->raw_Item(index, &spAttachment);

//spAttachment = spAttachments->Item(index);
spMapiObj = spAttachment->GetMAPIOBJECT();
hr = spMapiObj->QueryInterface(IID_IAttachment, (void**) &spAttach);


MessageBox(NULL, L"Mail is being sent !", L"Advertisement", MB_OK);
//*CancelDefault = true;
//MessageBox(NULL, L"Mail is being blocked !", L"Advertisement", MB_OK);

if (bTaskItem)
MessageBox(NULL, L"Task is being assigned!", L"Advertisement", MB_OK);

if (bCalndarItem)
MessageBox(NULL, L"Calendar invite is being sent !", L"Advertisement",
*CancelDefault = true;
MessageBox(NULL, L"Calendar invite is being blocked !", L"Advertisement",



Seeking urgent reply.


Dmitry Streblechenko

1. Sender related properties are set only after the message is handed to the
transport provider. These propertiss are not yet available in teh ItemSend
event. The earliest you can access them is in the Items.ItemAdd event
handler on the Sent Items folder.
2. Save the message first (MailItem.Save) to make sure MAPI (and Redemption)
can see the changes.

Dmitry Streblechenko (MVP)
OutlookSpy - Outlook, CDO
and MAPI Developer Tool

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