Kira
Kira

Reputation: 1207

Get number of attachments of a sent email

I'm trying to get the number of attachments of an email when it's being sent (Application.Send event), here is the code I'm using.

_ApplicationPtr pApp("Outlook.Application");
_NameSpacePtr pNameSpace;
MAPIFolderPtr pOutbox;
_ItemsPtr pOutboxItems;
_MailItemPtr mi;
pNameSpace = pApp->GetNamespace(L"MAPI");
pOutbox = pNameSpace->GetDefaultFolder(olFolderOutbox);
pOutboxItems = pOutbox->Items;
mi=pOutboxItems->GetLast();
long att_Sent=mi->GetAttachments()->Count;//Exception here
mi->Delete();   

I always get an exception at the mi->GetAttachments->Count line (access violation). I think the mail item pointer is null. What am I doing wrong?

Upvotes: 0

Views: 130

Answers (1)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66286

Why are you retrieving the last item in the Outbox, which is not at all guaranteed to be the one being sent, and most likely you will get back null since there are no messages yet in the outbox folder when Application.ItemSend event fires?

Application.ItemSend event passes the item as an argument, why not use it?

Upvotes: 1

Related Questions