Extract Attachments in ItemSend

  • Thread starter Thread starter amitshinde via OfficeKB.com
  • Start date Start date
A

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 *
CancelDefault)
{
HRESULT hRes = S_OK;
_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**)
&spTaskItem);
if (NULL == spTaskItem)
{
hRes = Ctrl->QueryInterface(__uuidof(_MeetingItem), (void**)
&spCalendarItem);
if (NULL == spCalendarItem)
{

}
else
{
bCalndarItem = TRUE;
}
}
else
{
bTaskItem = TRUE;
}
}
else
{
bMailItem = TRUE;
}

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

CComPtr<Redemption::ISafeMailItem> spSafeMailItem;

hr = ::CoCreateInstance( clsid,
NULL,
CLSCTX_INPROC_SERVER,
__uuidof(ISafeMailItem),
(void**)&spSafeMailItem
);
//RETURN_HR_IF_FAILED(hr)

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


if (spDispatchMailItem)
{
spSafeMailItem->PutItem(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**)
&spAttachment);
//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",
MB_OK);
*CancelDefault = true;
MessageBox(NULL, L"Calendar invite is being blocked !", L"Advertisement",
MB_OK);

}

}

Seeking urgent reply.

Thanks
Amit
 
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)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 
Back
Top