get the currently selected in inspector account 's address

Y

Yaroslav

Hello,

I need to get the sender address using MAPI.
I have several accounts in outlook. When I select one of them in the inspector window and send message I need to know the address of this account.
I tried this code
Outlook::_NameSpacePtr pNS = pApplication->GetNamespace(L"MAPI");
Outlook::RecipientPtr pSender = pNS->GetCurrentUser();
senderAdr =pSender->GetAddress();

but it always give me the same address (address of the first account). Also I tried get the property PR_SENDER_EMAIL_ADDRESS from the message but get an error that this property is not found.

Please, advise what to do to get the current sender address?
 
K

Ken Slovak - [MVP - Outlook]

When are you trying to get PR_SENDER_EMAIL_ADDRESS? Is it before the item
was sent out? That property is only added to email items after the message
has been sent.

What version of Outlook are you using? If this is Outlook 2007 you could use
the Outlook property item.SendUsingAccount, which returns an Account object.
That can then be matched against the corresponding Account in the
NameSpace.Accounts collection. Each Account object has an SmtpAddress
property.

If this is an earlier version of Outlook you'd have to fall back to using
different MAPI properties, InetAcctName and InetAcctID.

Both are named properties so you'd have to use GetIdsFromNames to get the
property tags:

InetAcctName has a GUID of "{00062008-0000-0000-C000-000000000046}" with an
ID of 0x8580 (in DASL
"http://schemas.microsoft.com/mapi/id/{00062008-0000-0000-C000-000000000046}/8580001E").
That would have the name given to the account. It's a PT_STRING8 property.

InetAcctID has a GUID of "{00062008-0000-0000-C000-000000000046}" with an ID
of 0x8581 (in DASL
"http://schemas.microsoft.com/mapi/id/{00062008-0000-0000-C000-000000000046}/8581001E").
That would have the email address for the account. It's also a PT_STRING8
property, but formatted as: accountNumber + 0x01 + SMTPAddress.

You can see the formatting of those properties using a MAPI viewer such as
OutlookSpy or MFCMAPI.

Those properties aren't available however until the item has been saved.
They won't be there in an unsaved item.

See if that helps.
 
Y

Yaroslav

I am trying to get the property before the message is send out. This is in Outlook 2003.
I have tried the OutlookSpy and it do not show me the properties having IDs 0x8580 and 0x8581 for the IMessage in the inspector window. I see these properties when I select a received message in the outlook main window and click outlookspy 'IMessage' button but these properties concerns to receiver of the message.
I assume that these properties are set after I click send button in the inspector and they should containg sender name and address, no?
Please point me to any good internet resource with the example of how to get the named properties from IMessage interface (or any other mapi interface)?
 
Y

Yaroslav

I am trying to get the property before the message is send out. This is in Outlook 2003.
I have tried the OutlookSpy and it did not show me the properties having IDs 0x8580 and 0x8581 for the IMessage in the inspector window. I able to see these properties after I select a received message in the outlook main window and click outlookspy 'IMessage' button but these properties concern to receiver of the message.
I assume that these properties are set for outgoing message after I click send button in the inspector and they should contain sender name and address, no?
 
K

Ken Slovak - [MVP - Outlook]

As I said, you must save the item or retrieve it after it is sent to get
those properties. If you do not want to do either then I know of no way to
retrieve those properties.
 
Y

Yaroslav

Actually I do save the item invoking spMailItem->Save() before retrieving those properties, but the GetIDsFromNames returns the E_INVALIDARG error. I provided the code snippet how I use the GetIDsFromNames, but I am not sure whether I use it correctly?
 
K

Ken Slovak - [MVP - Outlook]

I don't see any code showing how you are using GetIdsFromNames().

With a GUID of "{00062008-0000-0000-C000-000000000046}" and an ID of 0x8580
and a type of PT_STRING8 here's how I would I use it in C#, calling
Redemption's GetIDsFromNames():

int tag = item.GetIdsFromNames("{00062008-0000-0000-C000-000000000046}",
0x8580) || 0x1E;

string acctName = (string)item.get_Fields(tag);

Maybe that will help.
 
Y

Yaroslav

Hi Ken,

Thanks a lot for all your efforts.
But I am still not able to get sender address before the message is sent out. I am saving the mail item in the BeforeMessageSend handler and on the IMAPIAdviseThink notification 'fnevObjectCreated' I am checking whether the message is in outgoing folder and try to retrieve the sender address in the following manner
static const GUID InetAcct_GUID = { 0x00062008, 0x0000, 0x0000, { 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46 } };
LPSPropTagArray lpNamedPropTags = NULL;
MAPINAMEID NamedID = {0};
LPMAPINAMEID lpNamedID = &NamedID;
NamedID.lpguid = (LPGUID)&InetAcct_GUID;
NamedID.ulKind = MNID_ID;
NamedID.Kind.lID = 0x8581;
hResult = pMessage->GetIDsFromNames(
1,
&lpNamedID,
NULL,
&lpNamedPropTags);
if (SUCCEEDED(hResult) && lpNamedPropTags)
{
lpNamedPropTags->aulPropTag[0] = CHANGE_PROP_TYPE(lpNamedPropTags->aulPropTag[0],PT_STRING8);
LPSPropValue lpProps = NULL;
ULONG cProps = 0;
hResult = pMessage->GetProps(
lpNamedPropTags,
NULL,
&cProps,
&lpProps);
if (SUCCEEDED(hResult) &&
1 == cProps &&
lpProps &&
PT_STRING8 == PROP_TYPE(lpProps[0].ulPropTag) &&
lpProps[0].Value.lpszA)
{
printf("Sender Email address = \"%s\"\n",lpProps[0].Value.lpszA);
}
MAPIFreeBuffer(lpProps);
MAPIFreeBuffer(lpNamedPropTags);
}

but PROP_TYPE(lpProps[0].ulPropTag) equals to PT_ERROR. (pMessage is ATL::CComPtr<IMessage>)
 
K

Ken Slovak - [MVP - Outlook]

I don't do Extended MAPI or C++ so I can't help you with that code. I'd
suggest posting in the Extended MAPI group where you'll find more
appropriate help: microsoft.public.win32.programmer.messaging.
 

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