BOOL CMAPIEx::Init(BOOL bMultiThreadedNotifcations) { #ifdef _WIN32_WCE if (CoInitializeEx(NULL, COINIT_MULTITHREADED) != S_OK) return FALSE; #endif if (bMultiThreadedNotifcations) { MAPIINIT_0 MAPIInit = { 0, MAPI_MULTITHREAD_NOTIFICATIONS }; if (MAPIInitialize(&MAPIInit) != S_OK) return FALSE; } else { if (MAPIInitialize(NULL) != S_OK) return FALSE; } return TRUE; }
bool MapiProfiles::init() { if (m_initialised) { return true; } QString profilePath(QDir::home().path()); profilePath.append(QLatin1String("/.openchange/")); QString profileFile(profilePath); profileFile.append(QString::fromLatin1("profiles.ldb")); // Check if the store exists. QDir path(profilePath); if (!path.exists()) { if (path.mkpath(profilePath)) { error() << "cannot make profile path:" << profilePath; return false; } } if (!QFile::exists(profileFile)) { if (MAPI_E_SUCCESS != CreateProfileStore(profileFile.toUtf8(), mapi_profile_get_ldif_path())) { error() << "cannot create profile store:" << profileFile << mapiError(); return false; } } if (MAPI_E_SUCCESS != MAPIInitialize(&m_context, profileFile.toLatin1())) { error() << "cannot init profile store:" << profileFile << mapiError(); return false; } m_initialised = true; return true; }
IMAPISession * MAPIStorageAdviser::InitIMAPISession() { if(FAILED(MAPIInitialize(NULL))) return NULL; if(MAPILogonEx(0, NULL, NULL, 0, &_pIMapiSession) != S_OK) { MAPIUninitialize(); return NULL; } return _pIMapiSession; }
// flags are ignored on Windows CE BOOL CMAPIEx::Init(BOOL bMultiThreadedNotifications, BOOL bInitAsService) { #ifdef _WIN32_WCE // CoInit fails when calling from .Net because it's already initialized CoInitializeEx(NULL, COINIT_MULTITHREADED); if(MAPIInitialize(NULL)!=S_OK) return FALSE; CPOOM::Init(); #else DWORD dwFlags=0; if(bMultiThreadedNotifications) dwFlags|=MAPI_MULTITHREAD_NOTIFICATIONS; if(bInitAsService) dwFlags|=MAPI_NT_SERVICE; if(dwFlags) { MAPIINIT_0 MAPIInit={ MAPI_INIT_VERSION, dwFlags }; if(MAPIInitialize(&MAPIInit)!=S_OK) return FALSE; } else { if(MAPIInitialize(NULL)!=S_OK) return FALSE; } #endif return TRUE; }
HRESULT ExchangeAdmin::Init() { HRESULT hr = S_OK; Zimbra::Mapi::Memory::SetMemAllocRoutines(NULL, MAPIAllocateBuffer, MAPIAllocateMore, MAPIFreeBuffer); if (FAILED(hr = MAPIInitialize(NULL))) throw ExchangeAdminException(hr, L"Init(): MAPIInitialize Failed.", ERR_GEN_EXCHANGEADMIN, __LINE__, __FILE__); if (FAILED(hr = MAPIAdminProfiles(0, &m_pProfAdmin))) throw ExchangeAdminException(hr, L"Init(): MAPIAdminProfiles Failed.", ERR_GEN_EXCHANGEADMIN, __LINE__, __FILE__); return hr; }
BOOL CMapiApi::Initialize( void) { if (m_initialized) return( TRUE); HRESULT hr; hr = MAPIInitialize( NULL); if (FAILED(hr)) { MAPI_TRACE2( "MAPI Initialize failed: 0x%lx, %d\n", (long)hr, (int)hr); return( FALSE); } m_initialized = TRUE; MAPI_TRACE0( "MAPI Initialized\n"); return( TRUE); }
/************************************************************************** * MAPISendMail (MAPI32.211) * * Send a mail. * * PARAMS * session [I] Handle to a MAPI session. * uiparam [I] Parent window handle. * message [I] Pointer to a MAPIMessage structure. * flags [I] Flags. * reserved [I] Reserved, pass 0. * * RETURNS * Success: SUCCESS_SUCCESS * Failure: MAPI_E_FAILURE * */ ULONG WINAPI MAPISendMail( LHANDLE session, ULONG_PTR uiparam, lpMapiMessage message, FLAGS flags, ULONG reserved ) { WCHAR msg_title[READ_BUF_SIZE], error_msg[READ_BUF_SIZE]; /* Check to see if we have a Simple MAPI provider loaded */ if (mapiFunctions.MAPISendMail) return mapiFunctions.MAPISendMail(session, uiparam, message, flags, reserved); /* Check if we have an Extended MAPI provider - if so, use our wrapper */ if (MAPIInitialize(NULL) == S_OK) return sendmail_extended_mapi(session, uiparam, message, flags, reserved); /* Display an error message since we apparently have no mail clients */ LoadStringW(hInstMAPI32, IDS_NO_MAPI_CLIENT, error_msg, sizeof(error_msg) / sizeof(WCHAR)); LoadStringW(hInstMAPI32, IDS_SEND_MAIL, msg_title, sizeof(msg_title) / sizeof(WCHAR)); MessageBoxW((HWND) uiparam, error_msg, msg_title, MB_ICONEXCLAMATION); return MAPI_E_NOT_SUPPORTED; }
/* mapi login */ bool Mapix::login() { // Mapix initialize MAPIINIT_0 MAPIInit = {MAPI_INIT_VERSION, MAPI_MULTITHREAD_NOTIFICATIONS}; result = MAPIInitialize(&MAPIInit); if(result == S_OK) { result = MAPILogonEx(NULL, L"", L"", MAPI_ALLOW_OTHERS|MAPI_USE_DEFAULT|MAPI_EXTENDED|MAPI_NEW_SESSION, &m_lpSession); if(result != S_OK) { setError(result); return 0; } else return 1; } else { setError(result); return 0; } }
void SaveMsg2File(){ ULONG cbStrSize = 0L; LPWSTR lpWideCharStr = NULL; wchar_t szPath[_MAX_PATH]; // get temp file directory GetTempPath(_MAX_PATH, szPath); #ifdef MAPI32_W wcscat_s(szPath, L"Correct.msg"); #else wcscat_s(szPath, L"Garbage.msg"); #endif IStorage *pStorage = NULL; LPMSGSESS pMsgSession = NULL; LPMESSAGE pMessage = NULL; HRESULT hr = S_OK; //LPWSTR subject = L"テスト日本の";// L"ceshi测试12"; //LPWSTR body = L"テスト日本のテスト日本の"; // L"lhy测试12"; //LPWSTR receipt = L"*****@*****.**"; LPWSTR subject = L"Η πολιτική αβεβαιότητα ανησυχεί τις αγορές";// L"ceshi测试12"; LPWSTR body = L"Η πολιτική αβεβαιότητα ανησυχεί τις αγορές"; // L"lhy测试12"; LPWSTR receipt = L"*****@*****.**"; LPSTR subjectA = ConvertUnicode2Ansi(subject); LPSTR bodyA = ConvertUnicode2Ansi(body); LPSTR receiptA = ConvertUnicode2Ansi(receipt); //LPSTR subjectA = "ceshi测试12"; //LPSTR bodyA = "lhy测试12"; //LPSTR receiptA = "*****@*****.**"; do{ MAPIINIT_0 mapiInit = { 0, MAPI_MULTITHREAD_NOTIFICATIONS }; hr = MAPIInitialize(&mapiInit); DEFINE_IF_HR_NT_OK_BREAK(hr); LPMALLOC pMalloc = MAPIGetDefaultMalloc(); hr = StgCreateDocfile(szPath, STGM_READWRITE | STGM_TRANSACTED | STGM_CREATE, 0, &pStorage); DEFINE_IF_HR_NT_OK_BREAK(hr); hr = OpenIMsgSession(pMalloc, 0, &pMsgSession); DEFINE_IF_HR_NT_OK_BREAK(hr); #ifdef MAPI32_W // lhy comment:if load exmapi32.dll, this function will failed with error code 0x80040106. hr = OpenIMsgOnIStg(pMsgSession, MAPIAllocateBuffer, MAPIAllocateMore, MAPIFreeBuffer, pMalloc, NULL, pStorage, NULL, 0, MAPI_UNICODE, &pMessage); #else hr = OpenIMsgOnIStg(pMsgSession, MAPIAllocateBuffer, MAPIAllocateMore, MAPIFreeBuffer, pMalloc, NULL, pStorage, NULL, 0, 0, &pMessage); #endif DEFINE_IF_HR_NT_OK_BREAK(hr); hr = WriteClassStg(pStorage, CLSID_MailMessage); DEFINE_IF_HR_NT_OK_BREAK(hr); #ifdef MAPI32_W hr = SetPropsW(pMessage, subject, body, receipt, false, false, false, false, FORCE_SAVE); #else hr = SetPropsA(pMessage, subjectA, bodyA, receiptA, false, false, false, false, FORCE_SAVE); #endif DEFINE_IF_HR_NT_OK_BREAK(hr); hr = pStorage->Commit(STGC_DEFAULT); DEFINE_IF_HR_NT_OK_BREAK(hr); } while (0); delete subjectA; delete bodyA; delete receiptA; if (pMessage){ pMessage->Release(); pMessage = NULL; } if (pStorage){ pStorage->Release(); pStorage = NULL; } if (pMsgSession){ CloseIMsgSession(pMsgSession); pMsgSession = NULL; } MAPIUninitialize(); }
BOOL CMailMsg::Send() { if(m_lpMapiSendMail==NULL) return FALSE; TStrStrMap::iterator p; int nIndex = 0; MapiRecipDesc* pRecipients = NULL; int nAttachments = 0; MapiFileDesc* pAttachments = NULL; ULONG status = 0; MapiMessage message; if(!m_bReady && !MAPIInitialize()) return FALSE; pRecipients = new MapiRecipDesc[2 + m_cc.size()]; if(!pRecipients) { m_sErrorMsg = _T("Error allocating memory"); return FALSE; } nAttachments = (int)m_attachments.size(); if (nAttachments) { pAttachments = new MapiFileDesc[nAttachments]; if(!pAttachments) { m_sErrorMsg = _T("Error allocating memory"); delete[] pRecipients; return FALSE; } } // set from pRecipients[0].ulReserved = 0; pRecipients[0].ulRecipClass = MAPI_ORIG; pRecipients[0].lpszAddress = (LPSTR)m_from.c_str(); pRecipients[0].lpszName = ""; pRecipients[0].ulEIDSize = 0; pRecipients[0].lpEntryID = NULL; // set to pRecipients[1].ulReserved = 0; pRecipients[1].ulRecipClass = MAPI_TO; pRecipients[1].lpszAddress = (LPSTR)m_to.c_str(); pRecipients[1].lpszName = (LPSTR)m_to.c_str(); pRecipients[1].ulEIDSize = 0; pRecipients[1].lpEntryID = NULL; // add cc receipients nIndex = 2; for (size_t i = 0; i < m_cc.size(); ++i) { pRecipients[nIndex].ulReserved = 0; pRecipients[nIndex].ulRecipClass = MAPI_CC; pRecipients[nIndex].lpszAddress = (LPSTR)m_cc.at(i).c_str(); pRecipients[nIndex].lpszName = (LPSTR)m_cc.at(i).c_str(); pRecipients[nIndex].ulEIDSize = 0; pRecipients[nIndex].lpEntryID = NULL; nIndex++; } nIndex=0; // add attachments for (p = m_attachments.begin(), nIndex = 0; p != m_attachments.end(); p++, nIndex++) { pAttachments[nIndex].ulReserved = 0; pAttachments[nIndex].flFlags = 0; pAttachments[nIndex].nPosition = 0xFFFFFFFF; pAttachments[nIndex].lpszPathName = (LPSTR)p->first.c_str(); pAttachments[nIndex].lpszFileName = (LPSTR)p->second.c_str(); pAttachments[nIndex].lpFileType = NULL; } message.ulReserved = 0; message.lpszSubject = (LPSTR)m_sSubject.c_str(); message.lpszNoteText = (LPSTR)m_sMessage.c_str(); message.lpszMessageType = NULL; message.lpszDateReceived = NULL; message.lpszConversationID = NULL; message.flFlags = 0; message.lpOriginator = pRecipients; message.nRecipCount = (ULONG)(1 + m_cc.size()); message.lpRecips = &pRecipients[1]; message.nFileCount = nAttachments; message.lpFiles = nAttachments ? pAttachments : NULL; status = m_lpMapiSendMail(NULL, 0, &message, (m_bShowComposeDialog?MAPI_DIALOG:0)|MAPI_LOGON_UI, 0); if(status!=SUCCESS_SUCCESS) { m_sErrorMsg.Format(_T("MAPISendMail has failed with code %X."), status); } if (pRecipients) delete [] pRecipients; if (nAttachments) delete [] pAttachments; return (SUCCESS_SUCCESS == status); }
BOOL CMailMsg::CMCSend() { TStrStrMap::iterator p; int nIndex = 0; CMC_recipient* pRecipients; CMC_attachment* pAttachments; CMC_session_id session; CMC_return_code status = 0; CMC_message message; CMC_boolean bAvailable = FALSE; CMC_time t_now = {0}; if (!m_bReady && !MAPIInitialize()) return FALSE; pRecipients = new CMC_recipient[2]; pAttachments = new CMC_attachment[m_attachments.size()]; // set to pRecipients[nIndex].name = (LPSTR)m_to.c_str(); pRecipients[nIndex].name_type = CMC_TYPE_INDIVIDUAL; pRecipients[nIndex].address = (CMC_string)(LPCSTR)m_to.c_str(); pRecipients[nIndex].role = CMC_ROLE_TO; pRecipients[nIndex].recip_flags = 0; pRecipients[nIndex].recip_extensions = NULL; // set from pRecipients[nIndex+1].name = (LPSTR)m_from.c_str(); pRecipients[nIndex+1].name_type = CMC_TYPE_INDIVIDUAL; pRecipients[nIndex+1].address = (CMC_string)(LPCSTR)m_from.c_str(); pRecipients[nIndex+1].role = CMC_ROLE_ORIGINATOR; pRecipients[nIndex+1].recip_flags = CMC_RECIP_LAST_ELEMENT; pRecipients[nIndex+1].recip_extensions = NULL; // add attachments for (p = m_attachments.begin(), nIndex = 0; p != m_attachments.end(); p++, nIndex++) { pAttachments[nIndex].attach_title = (LPSTR)p->second.c_str(); pAttachments[nIndex].attach_type = NULL; pAttachments[nIndex].attach_filename = (CMC_string)(LPCSTR)p->first.c_str(); pAttachments[nIndex].attach_flags = 0; pAttachments[nIndex].attach_extensions = NULL; } pAttachments[nIndex-1].attach_flags = CMC_ATT_LAST_ELEMENT; message.message_reference = NULL; message.message_type = NULL; message.subject = (LPSTR)m_sSubject.c_str(); message.time_sent = t_now; message.text_note = (LPSTR)m_sMessage.c_str(); message.recipients = pRecipients; message.attachments = pAttachments; message.message_flags = 0; message.message_extensions = NULL; status = m_lpCmcQueryConfiguration( 0, CMC_CONFIG_UI_AVAIL, (void*)&bAvailable, NULL ); if (CMC_SUCCESS == status && bAvailable) { status = m_lpCmcLogon( NULL, NULL, NULL, NULL, 0, CMC_VERSION, CMC_LOGON_UI_ALLOWED | CMC_ERROR_UI_ALLOWED, &session, NULL ); if (CMC_SUCCESS == status) { status = m_lpCmcSend(session, &message, 0, 0, NULL); m_lpCmcLogoff(session, NULL, CMC_LOGON_UI_ALLOWED, NULL); } } delete [] pRecipients; delete [] pAttachments; return ((CMC_SUCCESS == status) && bAvailable); }
BOOL CMailMsg::MAPISend() { if(m_lpMapiSendMail==NULL) return FALSE; TStrStrMap::iterator p; int nIndex = 0; MapiRecipDesc* pRecipients = NULL; int nAttachments = 0; MapiFileDesc* pAttachments = NULL; ULONG status = 0; MapiMessage message; if(!m_bReady && !MAPIInitialize()) return FALSE; LHANDLE hMapiSession = 0; status = m_lpMapiLogon(NULL, NULL, NULL, MAPI_LOGON_UI|MAPI_PASSWORD_UI, NULL, &hMapiSession); if(status!=SUCCESS_SUCCESS) { m_sErrorMsg.Format(_T("MAPILogon has failed with code %X."), status); return FALSE; } pRecipients = new MapiRecipDesc[2]; if(!pRecipients) { m_sErrorMsg = _T("Error allocating memory"); return FALSE; } nAttachments = (int)m_attachments.size(); if (nAttachments) pAttachments = new MapiFileDesc[nAttachments]; if(!pAttachments) { m_sErrorMsg = _T("Error allocating memory"); return FALSE; } // set from pRecipients[0].ulReserved = 0; pRecipients[0].ulRecipClass = MAPI_ORIG; pRecipients[0].lpszAddress = (LPSTR)m_from.c_str(); pRecipients[0].lpszName = ""; pRecipients[0].ulEIDSize = 0; pRecipients[0].lpEntryID = NULL; // set to pRecipients[1].ulReserved = 0; pRecipients[1].ulRecipClass = MAPI_TO; pRecipients[1].lpszAddress = (LPSTR)m_to.c_str(); pRecipients[1].lpszName = (LPSTR)m_to.c_str(); pRecipients[1].ulEIDSize = 0; pRecipients[1].lpEntryID = NULL; // add attachments nIndex=0; for (p = m_attachments.begin(), nIndex = 0; p != m_attachments.end(); p++, nIndex++) { pAttachments[nIndex].ulReserved = 0; pAttachments[nIndex].flFlags = 0; pAttachments[nIndex].nPosition = 0xFFFFFFFF; pAttachments[nIndex].lpszPathName = (LPSTR)p->first.c_str(); pAttachments[nIndex].lpszFileName = (LPSTR)p->second.c_str(); pAttachments[nIndex].lpFileType = NULL; } message.ulReserved = 0; message.lpszSubject = (LPSTR)m_sSubject.c_str(); message.lpszNoteText = (LPSTR)m_sMessage.c_str(); message.lpszMessageType = NULL; message.lpszDateReceived = NULL; message.lpszConversationID = NULL; message.flFlags = 0; message.lpOriginator = pRecipients; message.nRecipCount = 1; message.lpRecips = &pRecipients[1]; message.nFileCount = nAttachments; message.lpFiles = nAttachments ? pAttachments : NULL; status = m_lpMapiSendMail(hMapiSession, 0, &message, 0/*MAPI_DIALOG*/, 0); if(status!=SUCCESS_SUCCESS) { m_sErrorMsg.Format(_T("MAPISendMail has failed with code %X."), status); } m_lpMapiLogoff(hMapiSession, NULL, 0, 0); if (pRecipients) delete [] pRecipients; if (nAttachments) delete [] pAttachments; return (SUCCESS_SUCCESS == status); }
/** * main program */ int main(int argc, const char *argv[]) { enum MAPISTATUS retval; int32_t num_tests_failed; TALLOC_CTX *mem_ctx; struct mapitest mt; poptContext pc; int opt; bool ret; bool opt_dumpdata = false; const char *opt_debug = NULL; const char *opt_profdb = NULL; char *opt_profname = NULL; const char *opt_password = NULL; const char *opt_outfile = NULL; char *prof_tmp = NULL; bool opt_leak_report = false; bool opt_leak_report_full = false; enum { OPT_PROFILE_DB=1000, OPT_PROFILE, OPT_PASSWORD, OPT_CONFIDENTIAL, OPT_OUTFILE, OPT_MAPI_CALLS, OPT_NO_SERVER, OPT_LIST_ALL, OPT_DUMP_DATA, OPT_DEBUG, OPT_COLOR, OPT_SUBUNIT, OPT_LEAK_REPORT, OPT_LEAK_REPORT_FULL }; struct poptOption long_options[] = { POPT_AUTOHELP { "database", 'f', POPT_ARG_STRING, NULL, OPT_PROFILE_DB, "set the profile database", NULL }, { "profile", 'p', POPT_ARG_STRING, NULL, OPT_PROFILE, "set the profile name", NULL }, { "password", 'p', POPT_ARG_STRING, NULL, OPT_PASSWORD, "set the profile or account password", NULL }, { "confidential", 0, POPT_ARG_NONE, NULL, OPT_CONFIDENTIAL, "remove any sensitive data from the report", NULL }, { "color", 0, POPT_ARG_NONE, NULL, OPT_COLOR, "color MAPI retval", NULL }, #if HAVE_SUBUNIT { "subunit", 0, POPT_ARG_NONE, NULL, OPT_SUBUNIT, "output in subunit protocol format", NULL }, #endif { "outfile", 'o', POPT_ARG_STRING, NULL, OPT_OUTFILE, "set the report output file", NULL }, { "mapi-calls", 0, POPT_ARG_STRING, NULL, OPT_MAPI_CALLS, "test custom ExchangeRPC tests", NULL }, { "list-all", 0, POPT_ARG_NONE, NULL, OPT_LIST_ALL, "list suite and tests - names and description", NULL }, { "no-server", 0, POPT_ARG_NONE, NULL, OPT_NO_SERVER, "only run tests that do not require server connection", NULL }, { "dump-data", 0, POPT_ARG_NONE, NULL, OPT_DUMP_DATA, "dump the hex data", NULL }, { "debuglevel", 'd', POPT_ARG_STRING, NULL, OPT_DEBUG, "set debug level", NULL }, { "leak-report", 0, POPT_ARG_NONE, NULL, OPT_LEAK_REPORT, "enable talloc leak reporting on exit", NULL }, { "leak-report-full", 0, POPT_ARG_NONE, NULL, OPT_LEAK_REPORT_FULL, "enable full talloc leak reporting on exit", NULL }, POPT_OPENCHANGE_VERSION { NULL, 0, 0, NULL, 0, NULL, NULL } }; mem_ctx = talloc_named(NULL, 0, "mapitest"); mapitest_init(mem_ctx, &mt); mapitest_register_modules(&mt); pc = poptGetContext("mapitest", argc, argv, long_options, 0); while ((opt = poptGetNextOpt(pc)) != -1) { switch (opt) { case OPT_DUMP_DATA: opt_dumpdata = true; break; case OPT_DEBUG: opt_debug = poptGetOptArg(pc); break; case OPT_PROFILE_DB: opt_profdb = poptGetOptArg(pc); break; case OPT_PROFILE: prof_tmp = poptGetOptArg(pc); opt_profname = talloc_strdup(mem_ctx, prof_tmp); free(prof_tmp); prof_tmp = NULL; break; case OPT_PASSWORD: opt_password = poptGetOptArg(pc); break; case OPT_CONFIDENTIAL: mt.confidential = true; break; case OPT_OUTFILE: opt_outfile = poptGetOptArg(pc); break; case OPT_MAPI_CALLS: ret = mapitest_get_testnames(mem_ctx, &mt, poptGetOptArg(pc)); if (ret == false) exit (-1); mt.mapi_all = false; break; case OPT_NO_SERVER: mt.no_server = true; break; case OPT_COLOR: mt.color = true; break; case OPT_SUBUNIT: mt.subunit_output = true; break; case OPT_LIST_ALL: mapitest_list(&mt, NULL); talloc_free(mem_ctx); poptFreeContext(pc); return 0; break; case OPT_LEAK_REPORT: opt_leak_report = true; talloc_enable_leak_report(); break; case OPT_LEAK_REPORT_FULL: opt_leak_report_full = true; talloc_enable_leak_report_full(); break; } } poptFreeContext(pc); /* Sanity check */ if (mt.cmdline_calls && (mt.mapi_all == true)) { fprintf(stderr, "mapi-calls and mapi-all can't be set at the same time\n"); return -1; } /* Initialize MAPI subsystem */ if (!opt_profdb) { opt_profdb = talloc_asprintf(mem_ctx, DEFAULT_PROFDB, getenv("HOME")); } retval = MAPIInitialize(&(mt.mapi_ctx), opt_profdb); if (retval != MAPI_E_SUCCESS) { mapi_errstr("MAPIInitialize", retval); return -2; } mapitest_init_stream(&mt, opt_outfile); mt.online = mapitest_get_server_info(&mt, opt_profname, opt_password, opt_dumpdata, opt_debug); mapitest_print_headers(&mt); /* Do not run any tests if we couldn't find a profile or if * server is offline and connection to server was implicitly * specified */ if (!opt_profname && mt.online == false && mt.no_server == false) { fprintf(stderr, "No MAPI profile found for online tests\n"); return -2; } /* Run custom tests */ if (mt.cmdline_calls) { struct mapitest_unit *el; for (el = mt.cmdline_calls; el; el = el->next) { printf("[*] %s\n", el->name); mapitest_run_test(&mt, el->name); } } else { mapitest_run_all(&mt); } num_tests_failed = mapitest_stat_dump(&mt); mapitest_cleanup_stream(&mt); /* Uninitialize and free memory */ MAPIUninitialize(mt.mapi_ctx); talloc_free(mt.mem_ctx); if (opt_leak_report) { talloc_report(NULL, stdout); } if (opt_leak_report_full) { talloc_report_full(NULL, stdout); } return num_tests_failed; }
int main(int argc, char *argv[]) { enum MAPISTATUS retval; struct mapi_context *mapi_ctx; TALLOC_CTX *mem_ctx; struct mapi_session *session = NULL; mapi_object_t obj_store; mapi_object_t obj_folder; mapi_object_t obj_table; mapi_object_t obj_message; struct mapi_SPropValue_array props_all; struct SRowSet rowset; struct SPropTagArray *SPropTagArray; mapi_id_t id_inbox; mapi_id_t *fid, *mid; char *profname; char *profdb; uint32_t Numerator; uint32_t Denominator; uint32_t i; mem_ctx = talloc_named(NULL, 0, "fetchmail"); /* Initialize MAPI */ profdb = talloc_asprintf(mem_ctx, DEFAULT_PROFDB, getenv("HOME")); retval = MAPIInitialize(&mapi_ctx, profdb); MAPI_RETVAL_IF(retval, retval, mem_ctx); /* Find Default Profile */ retval = GetDefaultProfile(mapi_ctx, &profname); MAPI_RETVAL_IF(retval, retval, mem_ctx); /* Log on EMSMDB and NSPI */ retval = MapiLogonEx(mapi_ctx, &session, profname, NULL); MAPI_RETVAL_IF(retval, retval, mem_ctx); /* Open Message Store */ mapi_object_init(&obj_store); retval = OpenMsgStore(session, &obj_store); MAPI_RETVAL_IF(retval, retval, mem_ctx); /* Find Inbox default folder */ retval = GetDefaultFolder(&obj_store, &id_inbox, olFolderInbox); MAPI_RETVAL_IF(retval, retval, mem_ctx); /* Open Inbox folder */ mapi_object_init(&obj_folder); retval = OpenFolder(&obj_store, id_inbox, &obj_folder); MAPI_RETVAL_IF(retval, retval, mem_ctx); /* Retrieve Inbox content table */ mapi_object_init(&obj_table); retval = GetContentsTable(&obj_folder, &obj_table, 0x0, NULL); MAPI_RETVAL_IF(retval, retval, mem_ctx); /* Create the MAPI table view */ SPropTagArray = set_SPropTagArray(mem_ctx, 0x2, PR_FID, PR_MID); retval = SetColumns(&obj_table, SPropTagArray); MAPIFreeBuffer(SPropTagArray); MAPI_RETVAL_IF(retval, retval, mem_ctx); talloc_free(mem_ctx); /* Get current cursor position */ retval = QueryPosition(&obj_table, &Numerator, &Denominator); MAPI_RETVAL_IF(retval, retval, mem_ctx); /* Iterate through rows */ while ((retval = QueryRows(&obj_table, Denominator, TBL_ADVANCE, &rowset)) != -1 && rowset.cRows) { for (i = 0; i < rowset.cRows; i++) { fid = (mapi_id_t *)find_SPropValue_data(&(rowset.aRow[i]), PR_FID); mid = (mapi_id_t *)find_SPropValue_data(&(rowset.aRow[i]), PR_MID); mapi_object_init(&obj_message); retval = OpenMessage(&obj_store, *fid, *mid, &obj_message, 0x0); if (retval != MAPI_E_NOT_FOUND) { retval = GetPropsAll(&obj_message, MAPI_UNICODE, &props_all); mapidump_message(&props_all, NULL, &obj_message); mapi_object_release(&obj_message); } } } /* Release MAPI objects */ mapi_object_release(&obj_table); mapi_object_release(&obj_folder); Logoff(&obj_store); /* Uninitialize MAPI */ MAPIUninitialize(mapi_ctx); return (0); }
void Test(){ HRESULT hr = 0; LPMAPISESSION lpMapiSession = NULL; LPMDB lpMdb = NULL; IMAPITable* pIStoreTable = NULL; LPSRowSet pRows = NULL; IUnknown* lpExchManageStroe = NULL; SPropValue* pAllFoldersPropValue = NULL; do{ MAPIINIT_0 mapiInit = { 0, MAPI_MULTITHREAD_NOTIFICATIONS }; hr = MAPIInitialize(&mapiInit); DEFINE_IF_HR_NT_OK_BREAK(hr); //L"Outlook" hr = MAPILogonEx(0, NULL , NULL, MAPI_NEW_SESSION | MAPI_USE_DEFAULT | MAPI_EXTENDED, &lpMapiSession); DEFINE_IF_HR_NT_OK_BREAK(hr); enum{ PR_DEFAULT_STORE_, PR_ENTRYID_, PR_RESOURCE_FLAGS_, PR_MDB_PROVIDER_, PR_DISPLAY_NAME_, COUNT }; SizedSPropTagArray(COUNT, storeEntryID) = { COUNT, { PR_DEFAULT_STORE, PR_ENTRYID, PR_RESOURCE_FLAGS, PR_MDB_PROVIDER, PR_DISPLAY_NAME} }; ULONG ulRowCount = 0; ULONG ulRowIndex = 0; BOOL bFind = FALSE; hr = lpMapiSession->GetMsgStoresTable(0, &pIStoreTable); DEFINE_IF_HR_NT_OK_BREAK(hr); hr = pIStoreTable->SetColumns((LPSPropTagArray)&storeEntryID, 0); DEFINE_IF_HR_NT_OK_BREAK(hr); hr = pIStoreTable->GetRowCount(0, &ulRowCount); DEFINE_IF_HR_NT_OK_BREAK(hr); hr = pIStoreTable->QueryRows(ulRowCount, 0, &pRows); DEFINE_IF_HR_NT_OK_BREAK(hr); ulRowIndex = 0; while (ulRowIndex<pRows->cRows) { _SRow row = pRows->aRow[ulRowIndex]; if (row.lpProps[PR_DEFAULT_STORE_].Value.b == TRUE && (row.lpProps[PR_RESOURCE_FLAGS_].Value.ul & STATUS_DEFAULT_STORE) ) { bFind = TRUE; break; } ulRowIndex++; } if (bFind) { hr = lpMapiSession->OpenMsgStore(0, pRows->aRow[ulRowIndex].lpProps[PR_ENTRYID_].Value.bin.cb, (ENTRYID*)pRows->aRow[ulRowIndex].lpProps[PR_ENTRYID_].Value.bin.lpb, NULL, MDB_WRITE | MAPI_BEST_ACCESS | MDB_NO_DIALOG, (IMsgStore**)&lpMdb); DEFINE_IF_HR_NT_OK_BREAK(hr); } else { break; } enum { PR_IPM_OUTBOX_ENTRYID_, PR_VALID_FOLDER_MASK_, COUNT_ }; SizedSPropTagArray(COUNT_, rgPropTag) = { COUNT_, { PR_IPM_OUTBOX_ENTRYID, PR_VALID_FOLDER_MASK } }; ULONG ulValues = 0; hr = lpMdb->GetProps((LPSPropTagArray)&rgPropTag, 0, &ulValues, &pAllFoldersPropValue); DEFINE_IF_HR_NT_OK_BREAK(hr); ULONG lpObjType = 0; IMAPIFolder* lpMapiFolder = NULL; if (pAllFoldersPropValue[PR_VALID_FOLDER_MASK_].Value.ul & FOLDER_IPM_OUTBOX_VALID){ hr = lpMdb->OpenEntry(pAllFoldersPropValue[PR_IPM_OUTBOX_ENTRYID_].Value.bin.cb, (ENTRYID*)pAllFoldersPropValue[PR_IPM_OUTBOX_ENTRYID_].Value.bin.lpb, NULL, MAPI_BEST_ACCESS | MAPI_MODIFY, &lpObjType, (IUnknown**)&lpMapiFolder); DEFINE_IF_HR_NT_OK_BREAK(hr); } hr = AddMailW(lpMapiSession, lpMapiFolder, L"ceshi测试12", L"lhy测试12", L"*****@*****.**", false, false, true, false); DEFINE_IF_HR_NT_OK_BREAK(hr); } while (0); DWORD dError = GetLastError(); if (pAllFoldersPropValue){ MAPIFREEBUFFER(pAllFoldersPropValue); } if (lpExchManageStroe) { lpExchManageStroe->Release(); lpExchManageStroe = NULL; } if (lpMdb) { ULONG ulLogOffTag = LOGOFF_NO_WAIT; lpMdb->StoreLogoff(&ulLogOffTag); lpMdb->Release(); lpMdb = NULL; } if (pRows) { FreeProws(pRows); pRows = NULL; } if (pIStoreTable) { pIStoreTable->Release(); pIStoreTable = NULL; } if (lpMapiSession){ lpMapiSession->Logoff(0, 0, 0); lpMapiSession->Release(); lpMapiSession = NULL; } MAPIUninitialize(); }
STDMETHODIMP CMapiWrapper::GetProfilelist(VARIANT *Profiles,BSTR *statusmessage) { // TODO: Add your implementation code here dlog.trace(L" Begin Mapiwrapper GetProfilelist"); HRESULT hr = S_OK; CComBSTR status = L""; hr = MAPIInitialize(NULL); if( hr != S_OK) { LPCSTR temp = format_error(hr).c_str(); status.AppendBSTR(L" MapiInitialize error "); status.AppendBSTR(A2BSTR(temp)); dlog.err(status); *statusmessage = status; return hr; } Zimbra::Mapi::Memory::SetMemAllocRoutines(NULL, MAPIAllocateBuffer, MAPIAllocateMore, MAPIFreeBuffer); vector<string> vProfileList; hr = exchadmin->GetAllProfiles(vProfileList); if( hr != S_OK) { LPCSTR temp = format_error(hr).c_str(); status.AppendBSTR(L" GetAllProfiles error "); status.AppendBSTR(A2BSTR(temp)); dlog.err(status); *statusmessage = status; return hr; } if(vProfileList.size() == 0) { dlog.err(L"No profiles returned for GetAllProfiles"); status = L"No profiles"; *statusmessage = status; status.Detach(); return S_OK; } vector<CComBSTR> tempvectors; std::vector<string>::iterator its; for (its = (vProfileList.begin()); its != vProfileList.end(); its++) { string str = (*its).c_str(); CComBSTR temp = SysAllocString(str_to_wstr(str).c_str()); tempvectors.push_back(temp); } VariantInit(Profiles); Profiles->vt = VT_ARRAY | VT_BSTR; SAFEARRAY *psa; SAFEARRAYBOUND bounds = { (ULONG)vProfileList.size(), 0 }; psa = SafeArrayCreate(VT_BSTR, 1, &bounds); BSTR *bstrArray; SafeArrayAccessData(psa, (void **)&bstrArray); std::vector<CComBSTR>::iterator it; int i = 0; for (it = (tempvectors.begin()); it != tempvectors.end(); it++, i++) bstrArray[i] = SysAllocString((*it).m_str); SafeArrayUnaccessData(psa); Profiles->parray = psa; *statusmessage = status; status.Detach(); MAPIUninitialize(); dlog.trace(L" End Mapiwrapper GetProfilelist"); return hr; }
/************************************************************************** * MAPISendMailW (MAPI32.256) * * Send a mail. * * PARAMS * session [I] Handle to a MAPI session. * uiparam [I] Parent window handle. * message [I] Pointer to a MAPIMessageW structure. * flags [I] Flags. * reserved [I] Reserved, pass 0. * * RETURNS * Success: SUCCESS_SUCCESS * Failure: MAPI_E_FAILURE * */ ULONG WINAPI MAPISendMailW(LHANDLE session, ULONG_PTR uiparam, lpMapiMessageW message, FLAGS flags, ULONG reserved) { WCHAR msg_title[READ_BUF_SIZE], error_msg[READ_BUF_SIZE]; /* Check to see if we have a Simple MAPI provider loaded */ if (mapiFunctions.MAPISendMailW) return mapiFunctions.MAPISendMailW(session, uiparam, message, flags, reserved); /* Check if we have an Extended MAPI provider - if so, use our wrapper */ if (MAPIInitialize(NULL) == S_OK) return sendmail_extended_mapi(session, uiparam, message, flags); if (mapiFunctions.MAPISendMail) { MapiMessage messageA; ULONG ret; if (flags & MAPI_FORCE_UNICODE) return MAPI_E_UNICODE_NOT_SUPPORTED; /* Convert to ANSI and send to MAPISendMail */ ZeroMemory(&messageA, sizeof(MapiMessage)); messageA.lpszSubject = convert_from_unicode(message->lpszSubject); messageA.lpszNoteText = convert_from_unicode(message->lpszNoteText); messageA.lpszMessageType = convert_from_unicode(message->lpszMessageType); messageA.lpszDateReceived = convert_from_unicode(message->lpszDateReceived); messageA.lpszConversationID = convert_from_unicode(message->lpszConversationID); messageA.flFlags = message->flFlags; messageA.lpOriginator = convert_recipient_from_unicode(message->lpOriginator, NULL); messageA.nRecipCount = message->nRecipCount; messageA.nFileCount = message->nFileCount; if (message->nRecipCount && message->lpRecips) { lpMapiRecipDesc recipsA; unsigned int i; recipsA = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(MapiRecipDesc) * message->nRecipCount); for (i = 0; i < message->nRecipCount; i++) { convert_recipient_from_unicode(&message->lpRecips[i], &recipsA[i]); } messageA.lpRecips = recipsA; } if (message->nFileCount && message->lpFiles) { lpMapiFileDesc filesA; unsigned int i; filesA = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(MapiFileDesc) * message->nFileCount); for (i = 0; i < message->nFileCount; i++) { filesA[i].flFlags = message->lpFiles[i].flFlags; filesA[i].nPosition = message->lpFiles[i].nPosition; filesA[i].lpszPathName = convert_from_unicode(message->lpFiles[i].lpszPathName); filesA[i].lpszFileName = convert_from_unicode(message->lpFiles[i].lpszFileName); filesA[i].lpFileType = message->lpFiles[i].lpFileType; } messageA.lpFiles = filesA; } ret = mapiFunctions.MAPISendMail(session, uiparam, &messageA, flags, reserved); /* Now free everything we allocated */ if (message->lpOriginator) { HeapFree(GetProcessHeap(), 0, messageA.lpOriginator->lpszName); HeapFree(GetProcessHeap(), 0, messageA.lpOriginator->lpszAddress); HeapFree(GetProcessHeap(), 0, messageA.lpOriginator); } if (message->nRecipCount && message->lpRecips) { unsigned int i; for (i = 0; i < message->nRecipCount; i++) { HeapFree(GetProcessHeap(), 0, messageA.lpRecips[i].lpszName); HeapFree(GetProcessHeap(), 0, messageA.lpRecips[i].lpszAddress); } HeapFree(GetProcessHeap(), 0, messageA.lpRecips); } if (message->nFileCount && message->lpFiles) { unsigned int i; for (i = 0; i < message->nFileCount; i++) { HeapFree(GetProcessHeap(), 0, messageA.lpFiles[i].lpszPathName); HeapFree(GetProcessHeap(), 0, messageA.lpFiles[i].lpszFileName); } HeapFree(GetProcessHeap(), 0, messageA.lpFiles); } HeapFree(GetProcessHeap(), 0, messageA.lpszSubject); HeapFree(GetProcessHeap(), 0, messageA.lpszNoteText); HeapFree(GetProcessHeap(), 0, messageA.lpszDateReceived); HeapFree(GetProcessHeap(), 0, messageA.lpszConversationID); return ret; } /* Display an error message since we apparently have no mail clients */ LoadStringW(hInstMAPI32, IDS_NO_MAPI_CLIENT, error_msg, sizeof(error_msg) / sizeof(WCHAR)); LoadStringW(hInstMAPI32, IDS_SEND_MAIL, msg_title, sizeof(msg_title) / sizeof(WCHAR)); MessageBoxW((HWND) uiparam, error_msg, msg_title, MB_ICONEXCLAMATION); return MAPI_E_NOT_SUPPORTED; }
/************************************************************************** * MAPISendMail (MAPI32.211) * * Send a mail. * * PARAMS * session [I] Handle to a MAPI session. * uiparam [I] Parent window handle. * message [I] Pointer to a MAPIMessage structure. * flags [I] Flags. * reserved [I] Reserved, pass 0. * * RETURNS * Success: SUCCESS_SUCCESS * Failure: MAPI_E_FAILURE * */ ULONG WINAPI MAPISendMail( LHANDLE session, ULONG_PTR uiparam, lpMapiMessage message, FLAGS flags, ULONG reserved ) { WCHAR msg_title[READ_BUF_SIZE], error_msg[READ_BUF_SIZE]; /* Check to see if we have a Simple MAPI provider loaded */ if (mapiFunctions.MAPISendMail) return mapiFunctions.MAPISendMail(session, uiparam, message, flags, reserved); /* Check if we have an Extended MAPI provider - if so, use our wrapper */ if (MAPIInitialize(NULL) == S_OK) { MapiMessageW messageW; ULONG ret; ZeroMemory(&messageW, sizeof(MapiMessageW)); /* Convert the entries we need to Unicode */ messageW.lpszSubject = convert_to_unicode(message->lpszSubject); messageW.lpszNoteText = convert_to_unicode(message->lpszNoteText); messageW.nFileCount = message->nFileCount; if (message->nFileCount && message->lpFiles) { lpMapiFileDescW filesW; unsigned int i; filesW = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(MapiFileDescW) * message->nFileCount); for (i = 0; i < message->nFileCount; i++) { filesW[i].lpszPathName = convert_to_unicode(message->lpFiles[i].lpszPathName); filesW[i].lpszFileName = convert_to_unicode(message->lpFiles[i].lpszFileName); } messageW.lpFiles = filesW; } ret = sendmail_extended_mapi(session, uiparam, &messageW, flags); /* Now free everything we allocated */ if (message->nFileCount && message->lpFiles) { unsigned int i; for (i = 0; i < message->nFileCount; i++) { HeapFree(GetProcessHeap(), 0, messageW.lpFiles[i].lpszPathName); HeapFree(GetProcessHeap(), 0, messageW.lpFiles[i].lpszFileName); } HeapFree(GetProcessHeap(), 0, messageW.lpFiles); } HeapFree(GetProcessHeap(), 0, messageW.lpszSubject); HeapFree(GetProcessHeap(), 0, messageW.lpszNoteText); return ret; } /* Display an error message since we apparently have no mail clients */ LoadStringW(hInstMAPI32, IDS_NO_MAPI_CLIENT, error_msg, sizeof(error_msg) / sizeof(WCHAR)); LoadStringW(hInstMAPI32, IDS_SEND_MAIL, msg_title, sizeof(msg_title) / sizeof(WCHAR)); MessageBoxW((HWND) uiparam, error_msg, msg_title, MB_ICONEXCLAMATION); return MAPI_E_NOT_SUPPORTED; }