Message stays in outbox
- From: Ashutosh Bhawasinka <smbs-msdn@xxxxxxxxxxxxx>
- Date: Mon, 02 Jun 2008 21:46:08 +0530
Hi,
I am trying to create & send a message, but the message always stays in outbox. Can someone please check what's wrong in the code. It's attached.
except for
hRes = pAddrBook->ResolveName(0, 0, 0, pAddressList);
which returns -2147221245 (decimal)
all other method returns S_OK
Regards,
Ashutosh
HRESULT MAPISendMail(ULONG ulWindowHandle, LPCTSTR lpwszProfileName,LPCWSTR lpwszToList, LPCWSTR lpwszCCList, LPCWSTR lpwszBCCList, LPCWSTR lpwszEmailSubject, LPCWSTR lpwszEmailBody, LPCWSTR lpwszAttachmentFileName, LPCWSTR lpwszFiller1, LPCWSTR lpwszFiller2, LPCWSTR lpwszFiller3, LPCWSTR lpwszFiller4, LPCWSTR lpwszFiller5)
{
TRACE("SendMail()");
HRESULT hRes = S_OK;
MAPICleanUp CleanUp; //Class to automatically call "Release" on the interfaces
//if(!lpwszToList && !lpwszCCList && !lpwszBCCList) return MAPI_E_INVALID_PARAMETER;
hRes = CoInitialize(0);
if(!SUCCEEDED(hRes))
{
Debug(DebugLevel::Fatal,"Unable to initialize COM ");
Debug(DebugLevel::Fatal,::GetErrorDetails(hRes));
return hRes;
}
Debug(DebugLevel::Trace,"COM Initialized");
hRes = MAPIInitialize(NULL);
if(SUCCEEDED(hRes))
{
Debug(DebugLevel::Trace,"MAPI Initialized");
LPMAPISESSION lpSession = NULL;
if(lpwszProfileName)
{
hRes = MAPILogonEx(ulWindowHandle,(LPTSTR)lpwszProfileName, NULL, fMapiUnicode | MAPI_EXTENDED | MAPI_EXPLICIT_PROFILE | MAPI_NEW_SESSION | MAPI_LOGON_UI, &lpSession);
}
else
{
hRes = MAPILogonEx(ulWindowHandle,NULL, NULL, fMapiUnicode | MAPI_EXTENDED | /*MAPI_NEW_SESSION |*/ MAPI_LOGON_UI, &lpSession);
}
if(FAILED(hRes) || NULL == lpSession) //last try
{
hRes = MAPILogonEx(ulWindowHandle, NULL, NULL, fMapiUnicode | MAPI_EXTENDED | MAPI_EXPLICIT_PROFILE | MAPI_NEW_SESSION | MAPI_LOGON_UI, &lpSession);
}
if(SUCCEEDED(hRes) && lpSession)
{
Debug(DebugLevel::Information,"MAPI Logon Success");
SBinary pDefaultStoreEntryID;
LPMAPITABLE pMsgStoreTable = NULL;
LPSRowSet pRows = NULL;
char str[64]; //To store selected Profie, its max len is 64 charactes
SRestriction sRes;
SPropValue spv;
hRes = lpSession->GetMsgStoresTable(0,&pMsgStoreTable);
if(!SUCCEEDED(hRes))
{
Debug(DebugLevel::Error,"Failed to get Message StoreTable");
return hRes;
}
CleanUp.AddInterface(pMsgStoreTable,L"LPMAPITABLE");
//Setup restriction to get only the default store
sRes.rt = RES_PROPERTY ;
sRes.res.resProperty.relop = RELOP_EQ ;
sRes.res.resProperty.ulPropTag = PR_DEFAULT_STORE;
sRes.res.resProperty.lpProp = &spv;
spv.ulPropTag = PR_DEFAULT_STORE;
spv.Value.b = true; //attribute
//Get the store
hRes = HrQueryAllRows(pMsgStoreTable,NULL, &sRes,NULL, 0,&pRows);
if(!SUCCEEDED(hRes))
{
Debug(DebugLevel::Error,"Failed to open Message StoreTable");
//pMsgStoreTable->Release();
return hRes;
}
for(int j=0 ; j< pRows->aRow[0].cValues ; j++)
{
if(pRows->aRow[0].lpProps[j].ulPropTag == PR_ENTRYID )
{
pDefaultStoreEntryID = pRows->aRow[0].lpProps[j].Value.bin;
}
}
// open the MAPI Public Folder tree
LPMDB pDefaultMsgStore = NULL;
hRes = lpSession->OpenMsgStore(0, pDefaultStoreEntryID.cb, (LPENTRYID)pDefaultStoreEntryID.lpb, NULL,
MDB_WRITE | MAPI_DEFERRED_ERRORS, &pDefaultMsgStore );
if (SUCCEEDED (hRes))
{
CleanUp.AddInterface(pDefaultMsgStore,L"pDefaultMsgStore");
IMessage* pMsg = 0;
SPropValue* pProps = 0;
IMAPIFolder* pOutFolder = 0;
ADRLIST* pAddressList = 0;
ULONG ulObjType;
IAddrBook* pAddrBook = 0;
hRes = HrGetOneProp(pDefaultMsgStore, PR_IPM_OUTBOX_ENTRYID, &pProps);
if(FAILED(hRes))
{
Debug(DebugLevel::Error,L"Failed to find Outbox");
return hRes;
}
// Now open the outbox.
hRes = pDefaultMsgStore->OpenEntry(pProps[0].Value.bin.cb,
(LPENTRYID)pProps[0].Value.bin.lpb, &IID_IMAPIFolder,
MAPI_MODIFY, &ulObjType, (IUnknown**)&pOutFolder);
if(FAILED(hRes))
{
Debug(DebugLevel::Error,L"Failed to open Outbox");
return hRes;
}
//CleanUp.AddInterface(pOutFolder,L"pOutFolder");
hRes = pOutFolder->CreateMessage(0, 0, &pMsg);
if(FAILED(hRes))
{
Debug(DebugLevel::Error,L"Failed to open Outbox");
return hRes;
}
//CleanUp.AddInterface(pMsg,L"pMsg"); //Don't release IMessage
const ULONG cb = CbNewADRLIST(1);
hRes = MAPIAllocateBuffer(cb, (void**)&pAddressList);
if (FAILED(hRes))
{
Debug(DebugLevel::Error,L"Error allocating memory for address list");
return hRes;
}
else
{
ZeroMemory(pAddressList, cb);
}
WCHAR toAdd[]=L"test@xxxxxxxxxxxxxx";
SPropValue toProps[2];
toProps[0].ulPropTag = PR_DISPLAY_NAME;
toProps[0].Value.LPSZ = toAdd;
toProps[1].ulPropTag = PR_RECIPIENT_TYPE;
toProps[1].Value.l = MAPI_TO;
pAddressList->cEntries=1;
pAddressList->aEntries->cValues= 2;
pAddressList->aEntries->ulReserved1=0;
pAddressList->aEntries->rgPropVals = toProps;
hRes = lpSession->OpenAddressBook(0L, NULL, AB_NO_DIALOG, &pAddrBook);
if(FAILED(hRes))
{
Debug(DebugLevel::Error,L"Error opening address book.");
return hRes;
}
//CleanUp.AddInterface(pAddrBook,L"pAddrBook");
// Resolve the names.
hRes = pAddrBook->ResolveName(0, 0, 0, pAddressList);
//hRes = pAddrBook->ResolveName(0, MAPI_DIALOG, 0, pAddressList);
if(FAILED(hRes))
{
if(hRes == MAPI_E_AMBIGUOUS_RECIP)
{
Debug(DebugLevel::Error,L"MAPI_E_AMBIGUOUS_RECIP");
}
else if(hRes == MAPI_E_NOT_FOUND)
{
Debug(DebugLevel::Error,L"MAPI_E_NOT_FOUND");
}
else
Debug(DebugLevel::Error,L"Error resolving names.");
//return hRes;
}
hRes = pMsg->ModifyRecipients(MODRECIP_ADD , pAddressList);
if(FAILED(hRes))
{
Debug(DebugLevel::Error,L"Error modifying the recipients table.");
return hRes;
}
WCHAR szSubject[1024], szBody[1024];
wcscpy(szSubject, L"Test mail from Ashutosh");
wcscpy(szBody,L"Hi, how are you?");
SPropValue props[3];
// Subject and conversation topic
props[0].ulPropTag = PR_SUBJECT;
props[0].Value.LPSZ = (LPTSTR)szSubject;
props[1].ulPropTag = PR_BODY;
props[1].Value.LPSZ = (LPTSTR)szBody;
// Delete sent mail after submitting
props[2].ulPropTag = PR_DELETE_AFTER_SUBMIT;
props[2].Value.b = TRUE;
if(FAILED(hRes = pMsg->SetProps(3,props,NULL)))
{
Debug(DebugLevel::Error,L"Error setting property of mail");
return hRes;
}
//Create & add attachment now
LPATTACH FAR lppAttach;
ULONG lAttachmentNo = 0;
if(FAILED(hRes=pMsg->CreateAttach(NULL,0,&lAttachmentNo,&lppAttach)))
{
Debug(DebugLevel::Error,::GetErrorDetails(hRes));
}
CleanUp.AddInterface(lppAttach,L"lppAttach");
SPropValue pAttachProps[4];
pAttachProps[0].ulPropTag = PR_ATTACH_METHOD;
pAttachProps[0].Value.l = ATTACH_BY_REF_RESOLVE;
pAttachProps[1].ulPropTag = PR_ATTACH_PATHNAME;
pAttachProps[1].Value.lpszW = L"C:\\lakeland.txt";
pAttachProps[2].ulPropTag = PR_ATTACH_FILENAME;
pAttachProps[2].Value.lpszW = L"Lakeland.txt";
//pAttachProps[3].ulPropTag = PR_ATTACH_EXTENSION;
//pAttachProps[3].Value.lpszW = L"txt";
//pAttachProps[3].ulPropTag = PR_RENDERING_POSITION;
//pAttachProps[3].Value.l = -1L;
if(FAILED(hRes=lppAttach->SetProps(3,pAttachProps,NULL)))
{
Debug(DebugLevel::Error,::GetErrorDetails(hRes));
return hRes;
}
if(FAILED(hRes=lppAttach->SaveChanges(KEEP_OPEN_READWRITE)))
{
Debug(DebugLevel::Error,::GetErrorDetails(hRes));
return hRes;
}
//end of attachment
//if(FAILED(hRes = pMsg->SaveChanges(FORCE_SAVE)))
if(FAILED(hRes = pMsg->SaveChanges(KEEP_OPEN_READWRITE)))
{
Debug(DebugLevel::Error,L"Error saving message");
return hRes;
}
if(FAILED(hRes = pMsg->SubmitMessage(FORCE_SUBMIT )))
{
Debug(DebugLevel::Error,L"Error submitting message.");
return hRes;
}
}//Open message store
} //Login
else
{
Debug(DebugLevel::Error,L"Error Loggin on to MAPI");
Debug(DebugLevel::Error,::GetErrorDetails(hRes));
}
MAPIUninitialize();
}
::CoUninitialize();
return hRes;
}
- Follow-Ups:
- Re: Message stays in outbox
- From: Dmitry Streblechenko
- Re: Message stays in outbox
- From: Ashutosh Bhawasinka
- Re: Message stays in outbox
- Prev by Date: Re: SMTP Authentication flag
- Next by Date: Re: Message stays in outbox
- Previous by thread: Re: SMTP Authentication flag
- Next by thread: Re: Message stays in outbox
- Index(es):