Send mail using Extended MAPI

  • Thread starter Thread starter inmar_mine
  • Start date Start date
I

inmar_mine

Hi,
I’m trying to send mail to an exchange user from an exchange user using
Extended MAPI ( not to any mobile) . The steps I followed and the code is as
follows:

SendMail()
--------------
1. Initialize MAPI
hRes = MAPIInitialize(NULL)
2. Logon MAPI
hRes = MAPILogonEx(NULL, NULL, NULL, MAPI_EXTENDED | MAPI_LOGON_UI |
MAPI_NEW_SESSION , &pSession)

3. Get message stores table
hRes = pSession->GetMsgStoresTable(NULL, &pStoreTable);
4. Query rows for the entry id of a default store.
SRowSet *pRows = NULL;
SPropTagArray sptaEid = {1, {PR_ENTRYID}};
SRestriction res;
SPropValue spv;
res.rt = RES_PROPERTY;
res.res.resProperty.relop = RELOP_EQ;
res.res.resProperty.ulPropTag = PR_DEFAULT_STORE;
res.res.resProperty.lpProp = &spv;
spv.ulPropTag = PR_DEFAULT_STORE;
spv.Value.b = TRUE;
hRes = HrQueryAllRows(pStoreTable, &sptaEid, &res, NULL, 1, &pRows);

5. Open message store
if(pRows->cRows > 0 && pRows->aRow[0].cValues > 0 &&
pRows->aRow[0].lpProps[0].ulPropTag == PR_ENTRYID)
{
LPSBinary pEntryId = &pRows->aRow[0].lpProps[0].Value.bin;
hRes = pSession->OpenMsgStore(NULL, pEntryId->cb, (LPENTRYID)pEntryId->lpb,
NULL, MAPI_BEST_ACCESS, &pStore);
}

6. Call the function SetMessage()
SetMessage (pSession,pStore);

7. Release memory.
pStore->Release();
FreeProws(pRows);
pStoreTable->Release();
pSession->Logoff (0,0,0);
pSession->Release();
MAPIUninitialize();


SetMessage (IMAPISession *pSession,IMsgStore *pStore)
-----------------------------------------------------------------------
1. SPropTagArray sTags = {1,{PR_IPM_OUTBOX_ENTRYID}};

2. Get outbox entryid
hRes = pStore->GetProps(&sTags,0,&cCount,&lppPropArray);

3. Open outbox folder
hRes = pStore-> OpenEntry(lppPropArray[0].Value.bin.cb,
(LPENTRYID)lppPropArray[0].Value.bin.lpb,
NULL,MAPI_MODIFY,&ulObjType,(IUnknown **)&pFldrOutbox);

4. Create a message
hRes = pFldrOutbox->CreateMessage(NULL, 0, &pMsg);

5. Call the function MAPI_SetMessageProps(pSession,pMsg)

6. Submit the message
hRes = pMsg->SubmitMessage(0);
7. Release memory
pFldrOutbox->Release();
pFldrOutbox = NULL;
MAPIFreeBuffer(lppPropArray);
lppPropArray = NULL;
pMsg->Release();

MAPI_SetMessageProps(IMAPISession *pSession,LPMESSAGE pmsg
----------------------------------------------------------------------------------------
1. SPropValue rgprops[2] = {0};
ULONG cProps = 0;
LPWSTR pszSubject = L"Subject";
LPWSTR pszBody = L"Body.";

2. Call the function MAPI_SetMessageRecipients(pSession,pmsg)

3. Set the message properties
rgprops[0].ulPropTag = PR_MESSAGE_FLAGS;
rgprops[0].Value.ul = MSGFLAG_FROMME | MSGFLAG_SUBMIT;
++cProps;

if (pszSubject != NULL)
{
rgprops[1].ulPropTag = PR_SUBJECT;
rgprops[1].Value.lpszW = pszSubject;
++cProps;
}

4. hr = pmsg->SetProps(cProps, rgprops, NULL);

MAPI_SetMessageRecipients(IMAPISession *pSession,LPMESSAGE pmsg
----------------------------------------------------------------------------------------------
1. ULONG cRecipients = 1;
ULONG cchTotalRecips = 0;
LPWSTR pszTo = L"(e-mail address removed)";
ADRLIST* plst = NULL;
LPSPropValue pval = NULL;
BYTE* pb = NULL;
LPADRBOOK pAddrBook;

2. Open address book
hr = pSession->OpenAddressBook(0,0,0,&pAddrBook);
3. Allocate memory for ADRLIST + ADRENTRY + SPropValue
DWORD cb = sizeof(ADRLIST)+((sizeof(ADRENTRY)+ sizeof(SPropValue)* 4)*
cRecipients);
4. Find the length of the recipient email address
cchTotalRecips += wcslen(pszTo)+3;
5. Allocate memory for recipient email address
pb = new BYTE[cb +(cchTotalRecips * sizeof(WCHAR))];
6. Set memory to zero
ZeroMemory(pb, cb +(cchTotalRecips * sizeof(WCHAR)));
7. Assign the address to ADRLIST
plst =(ADRLIST*)pb;
8. Allocate memory for SPropValue
pb += sizeof(ADRLIST)+(sizeof(ADRENTRY)* cRecipients);
9.Assign address to LPSPropValue
pval =(SPropValue*)pb;

10. Number of properties
plst->aEntries->cValues = 4;
plst->aEntries->rgPropVals = pval;

11. pval[0].ulPropTag = PR_RECIPIENT_TYPE;
pval[0].Value.ul = MAPI_TO;

pval[1].ulPropTag = PR_ADDRTYPE;
pval[1].Value.lpszW = L"SMTP";

pval[2].ulPropTag = PR_EMAIL_ADDRESS;
pval[2].Value.lpszW = pszTo;


pval[3].ulPropTag = PR_DISPLAY_NAME;
pval[3].Value.lpszW = L"user2";

12. Resolve the recipient address
hr = pAddrBook->ResolveName(0,0,0,plst);
13. Add the address to recipient list
hr = pmsg->ModifyRecipients(MODRECIP_ADD, plst);
14. Save the changes
hr = pmsg->SaveChanges(KEEP_OPEN_READWRITE);
15. Free memory
delete[](BYTE*)plst;


Result:
--------
hRes = pMsg->SubmitMessage(0); //Here the SubmitMessage fails with no reason



Can anyone please help to resolve the problem ?

Thanks
Inmar
 
You'd be better off asking this in the appropriate developers newsgroup down
the hall. They all start with outlook.program_*

Good luck!



inmar_mine said:
Hi,
I’m trying to send mail to an exchange user from an exchange user using
Extended MAPI ( not to any mobile) . The steps I followed and the code is
as
follows:

SendMail()
--------------
1. Initialize MAPI
hRes = MAPIInitialize(NULL)
2. Logon MAPI
hRes = MAPILogonEx(NULL, NULL, NULL, MAPI_EXTENDED | MAPI_LOGON_UI |
MAPI_NEW_SESSION , &pSession)

3. Get message stores table
hRes = pSession->GetMsgStoresTable(NULL, &pStoreTable);
4. Query rows for the entry id of a default store.
SRowSet *pRows = NULL;
SPropTagArray sptaEid = {1, {PR_ENTRYID}};
SRestriction res;
SPropValue spv;
res.rt = RES_PROPERTY;
res.res.resProperty.relop = RELOP_EQ;
res.res.resProperty.ulPropTag = PR_DEFAULT_STORE;
res.res.resProperty.lpProp = &spv;
spv.ulPropTag = PR_DEFAULT_STORE;
spv.Value.b = TRUE;
hRes = HrQueryAllRows(pStoreTable, &sptaEid, &res, NULL, 1, &pRows);

5. Open message store
if(pRows->cRows > 0 && pRows->aRow[0].cValues > 0 &&
pRows->aRow[0].lpProps[0].ulPropTag == PR_ENTRYID)
{
LPSBinary pEntryId = &pRows->aRow[0].lpProps[0].Value.bin;
hRes = pSession->OpenMsgStore(NULL, pEntryId->cb,
(LPENTRYID)pEntryId->lpb,
NULL, MAPI_BEST_ACCESS, &pStore);
}

6. Call the function SetMessage()
SetMessage (pSession,pStore);

7. Release memory.
pStore->Release();
FreeProws(pRows);
pStoreTable->Release();
pSession->Logoff (0,0,0);
pSession->Release();
MAPIUninitialize();


SetMessage (IMAPISession *pSession,IMsgStore *pStore)
-----------------------------------------------------------------------
1. SPropTagArray sTags = {1,{PR_IPM_OUTBOX_ENTRYID}};

2. Get outbox entryid
hRes = pStore->GetProps(&sTags,0,&cCount,&lppPropArray);

3. Open outbox folder
hRes = pStore-> OpenEntry(lppPropArray[0].Value.bin.cb,
(LPENTRYID)lppPropArray[0].Value.bin.lpb,
NULL,MAPI_MODIFY,&ulObjType,(IUnknown **)&pFldrOutbox);

4. Create a message
hRes = pFldrOutbox->CreateMessage(NULL, 0, &pMsg);

5. Call the function MAPI_SetMessageProps(pSession,pMsg)

6. Submit the message
hRes = pMsg->SubmitMessage(0);
7. Release memory
pFldrOutbox->Release();
pFldrOutbox = NULL;
MAPIFreeBuffer(lppPropArray);
lppPropArray = NULL;
pMsg->Release();

MAPI_SetMessageProps(IMAPISession *pSession,LPMESSAGE pmsg)
----------------------------------------------------------------------------------------
1. SPropValue rgprops[2] = {0};
ULONG cProps = 0;
LPWSTR pszSubject = L"Subject";
LPWSTR pszBody = L"Body.";

2. Call the function MAPI_SetMessageRecipients(pSession,pmsg)

3. Set the message properties
rgprops[0].ulPropTag = PR_MESSAGE_FLAGS;
rgprops[0].Value.ul = MSGFLAG_FROMME | MSGFLAG_SUBMIT;
++cProps;

if (pszSubject != NULL)
{
rgprops[1].ulPropTag = PR_SUBJECT;
rgprops[1].Value.lpszW = pszSubject;
++cProps;
}

4. hr = pmsg->SetProps(cProps, rgprops, NULL);

MAPI_SetMessageRecipients(IMAPISession *pSession,LPMESSAGE pmsg)
----------------------------------------------------------------------------------------------
1. ULONG cRecipients = 1;
ULONG cchTotalRecips = 0;
LPWSTR pszTo = L"(e-mail address removed)";
ADRLIST* plst = NULL;
LPSPropValue pval = NULL;
BYTE* pb = NULL;
LPADRBOOK pAddrBook;

2. Open address book
hr = pSession->OpenAddressBook(0,0,0,&pAddrBook);
3. Allocate memory for ADRLIST + ADRENTRY + SPropValue
DWORD cb = sizeof(ADRLIST)+((sizeof(ADRENTRY)+ sizeof(SPropValue)* 4)*
cRecipients);
4. Find the length of the recipient email address
cchTotalRecips += wcslen(pszTo)+3;
5. Allocate memory for recipient email address
pb = new BYTE[cb +(cchTotalRecips * sizeof(WCHAR))];
6. Set memory to zero
ZeroMemory(pb, cb +(cchTotalRecips * sizeof(WCHAR)));
7. Assign the address to ADRLIST
plst =(ADRLIST*)pb;
8. Allocate memory for SPropValue
pb += sizeof(ADRLIST)+(sizeof(ADRENTRY)* cRecipients);
9.Assign address to LPSPropValue
pval =(SPropValue*)pb;

10. Number of properties
plst->aEntries->cValues = 4;
plst->aEntries->rgPropVals = pval;

11. pval[0].ulPropTag = PR_RECIPIENT_TYPE;
pval[0].Value.ul = MAPI_TO;

pval[1].ulPropTag = PR_ADDRTYPE;
pval[1].Value.lpszW = L"SMTP";

pval[2].ulPropTag = PR_EMAIL_ADDRESS;
pval[2].Value.lpszW = pszTo;


pval[3].ulPropTag = PR_DISPLAY_NAME;
pval[3].Value.lpszW = L"user2";

12. Resolve the recipient address
hr = pAddrBook->ResolveName(0,0,0,plst);
13. Add the address to recipient list
hr = pmsg->ModifyRecipients(MODRECIP_ADD, plst);
14. Save the changes
hr = pmsg->SaveChanges(KEEP_OPEN_READWRITE);
15. Free memory
delete[](BYTE*)plst;


Result:
--------
hRes = pMsg->SubmitMessage(0); //Here the SubmitMessage fails with no
reason



Can anyone please help to resolve the problem ?

Thanks
Inmar
 
Back
Top