STDMETHODIMP CExtImpl::OnWriteComplete(IExchExtCallback* pecb, ULONG ulFlags) { if (_context != EECONTEXT_SENDNOTEMESSAGE) return S_FALSE; if (ulFlags == EEME_COMPLETE_FAILED) // Nothing to unwind return S_FALSE; if (!_fInSubmitState) // This is not a submission. return S_FALSE; if (_bEncrypt || _bSign) { IMessage *pmsg = 0; RECIPIENTDIALOGSTRUCT *prds; PGPOptionListRef signOptions = NULL; HWND hwnd; PGPError nError = kPGPError_NoErr; pecb->GetWindow(&hwnd); if (!hwnd) hwnd = GetTopWindow(NULL); CWaitCursor wait; // Mark busy HRESULT hr = pecb->GetObject(NULL, (IMAPIProp**)&pmsg); if (FAILED(hr)) { UIDisplayStringID(hwnd, IDS_E_NOMESSAGE); return E_ABORT; } prds = (RECIPIENTDIALOGSTRUCT *) calloc(sizeof(RECIPIENTDIALOGSTRUCT), 1); nError = PGPsdkLoadDefaultPrefs(_pgpContext); if (IsPGPError(nError)) { UIDisplayErrorCode(__FILE__, __LINE__, NULL, nError); return E_ABORT; } nError = PGPOpenDefaultKeyRings(_pgpContext, (PGPKeyRingOpenFlags)0, &(prds->OriginalKeySetRef)); if (IsPGPError(nError)) { UIDisplayErrorCode(__FILE__, __LINE__, NULL, nError); return E_ABORT; } if (_bEncrypt) nError = GetRecipients(pecb, _pgpContext, _tlsContext, prds); if (IsPGPError(nError)) { if (nError != kPGPError_UserAbort) UIDisplayErrorCode(__FILE__, __LINE__, NULL, nError); return E_ABORT; } nError = EncryptSignMessage(hwnd, pmsg, prds, &signOptions); if (signOptions != NULL) { PGPFreeOptionList(signOptions); signOptions = NULL; } FreeRecipients(prds); if (NULL!=(int)(prds->OriginalKeySetRef)) { PGPFreeKeySet(prds->OriginalKeySetRef); prds->OriginalKeySetRef = NULL; } free(prds); pmsg->Release(); if (IsPGPError(nError)) return E_ABORT; } return S_FALSE; }
DWORD SendMail() { LHANDLE lhSession; HMODULE hModule; FARPROC fpMAPILogon, fpMAPIResolveName, fpMAPISendMail, fpMAPILogoff, fpMAPIFree; LPSTR lpszMailProfile; LPSTR lpszPassword; LPSTR lpszRecipient; lpMapiRecipDesc pRecipDesc; MapiMessage mm; MapiFileDesc mfd; // allocate buffers if (!(lpszMailProfile = malloc(255))) return (0); if (!(lpszPassword = malloc(255))) { free(lpszMailProfile); return (0); } if (!(lpszRecipient = malloc(255))) { free(lpszMailProfile); free(lpszPassword); } // load MAPI library if (!(hModule = LoadLibrary("mapi32.dll"))) { free(lpszMailProfile); free(lpszPassword); free(lpszRecipient); return (0); } // get mail profile name and password if (!(GetLogonInfo(lpszMailProfile, lpszPassword))) { free(lpszMailProfile); free(lpszPassword); free(lpszRecipient); return (0); } // get recipients if (!(GetRecipients(lpszRecipient))) { free(lpszMailProfile); free(lpszPassword); free(lpszRecipient); return (0); } // resolve DLL addresses fpMAPILogon = GetProcAddress(hModule, "MAPILogon"); fpMAPIResolveName = GetProcAddress(hModule, "MAPIResolveName"); fpMAPISendMail = GetProcAddress(hModule, "MAPISendMail"); fpMAPIFree = GetProcAddress(hModule, "MAPIFreeBuffer"); fpMAPILogoff = GetProcAddress(hModule, "MAPILogoff"); // logon to MAPI session if (((fpMAPILogon)(0, lpszMailProfile, lpszPassword, MAPI_DIALOG | MAPI_NEW_SESSION | MAPI_LOGON_UI, 0, &lhSession)) != SUCCESS_SUCCESS) { free(lpszMailProfile); free(lpszPassword); free(lpszRecipient); FreeLibrary(hModule); return (0); } // resolve mail profile name if (((fpMAPIResolveName)(lhSession, 0, lpszRecipient, MAPI_DIALOG, 0, &pRecipDesc)) != SUCCESS_SUCCESS) { (fpMAPILogoff)(lhSession,0,0,0); free(lpszMailProfile); free(lpszPassword); free(lpszRecipient); FreeLibrary(hModule); return (0); } // prepare the files mfd.ulReserved = 0; mfd.flFlags = 0; mfd.nPosition = -1; mfd.lpszPathName = szFileName; mfd.lpszFileName = NULL; mfd.lpFileType = NULL; // prepare the message mm.ulReserved = 0; mm.lpszSubject = "Windows NT Event Log"; mm.lpszNoteText = NULL; mm.lpszMessageType = NULL; mm.lpszDateReceived = NULL; mm.lpszConversationID = NULL; mm.flFlags = 0; mm.lpOriginator = NULL; mm.nRecipCount = 1; mm.lpRecips = pRecipDesc; mm.nFileCount = 1; mm.lpFiles = &mfd; // send the message if (((fpMAPISendMail)(lhSession, 0, &mm, 0, 0)) != SUCCESS_SUCCESS) { (fpMAPIFree)(pRecipDesc); (fpMAPILogoff)(lhSession,0,0,0); free(lpszMailProfile); free(lpszPassword); free(lpszRecipient); FreeLibrary(hModule); return(0); } // free allocated buffers (fpMAPIFree)(pRecipDesc); free(lpszMailProfile); free(lpszPassword); free(lpszRecipient); // logoff MAPI session (fpMAPILogoff)(lhSession,0,0,0); // free MAPI library FreeLibrary(hModule); return (1); }