29#include "wx/msw/private.h"
36#include <w32api/mapi.h>
43class WXDLLIMPEXP_NETUTILS wxMapiData {
49 m_lpfnMAPILogon =
nullptr;
50 m_lpfnMAPILogoff =
nullptr;
51 m_lpfnMAPISendMail =
nullptr;
52 m_lpfnMAPIResolveName =
nullptr;
53 m_lpfnMAPIFreeBuffer =
nullptr;
60 LPMAPILOGON m_lpfnMAPILogon;
61 LPMAPILOGOFF m_lpfnMAPILogoff;
62 LPMAPISENDMAIL m_lpfnMAPISendMail;
63 LPMAPIRESOLVENAME m_lpfnMAPIResolveName;
64 LPMAPIFREEBUFFER m_lpfnMAPIFreeBuffer;
69wxMapiSession::wxMapiSession() {
70 m_data =
new wxMapiData;
75wxMapiSession::~wxMapiSession() {
85void wxMapiSession::Initialise() {
88 bool bMapiInstalled = (GetProfileInt(_T(
"MAIL"), _T(
"MAPI"), 0) != 0) &&
89 (SearchPath(
nullptr, _T(
"MAPI32.DLL"),
nullptr, 0,
90 nullptr,
nullptr) != 0);
94 m_data->m_hMapi = ::LoadLibrary(_T(
"MAPI32.DLL"));
95 if (m_data->m_hMapi) {
96 m_data->m_lpfnMAPILogon =
97 (LPMAPILOGON)GetProcAddress(m_data->m_hMapi,
"MAPILogon");
98 m_data->m_lpfnMAPILogoff =
99 (LPMAPILOGOFF)GetProcAddress(m_data->m_hMapi,
"MAPILogoff");
100 m_data->m_lpfnMAPISendMail =
101 (LPMAPISENDMAIL)GetProcAddress(m_data->m_hMapi,
"MAPISendMail");
102 m_data->m_lpfnMAPIResolveName =
103 (LPMAPIRESOLVENAME)GetProcAddress(m_data->m_hMapi,
"MAPIResolveName");
104 m_data->m_lpfnMAPIFreeBuffer =
105 (LPMAPIFREEBUFFER)GetProcAddress(m_data->m_hMapi,
"MAPIFreeBuffer");
108 if (m_data->m_lpfnMAPILogon ==
nullptr ||
109 m_data->m_lpfnMAPILogoff ==
nullptr ||
110 m_data->m_lpfnMAPISendMail ==
nullptr ||
111 m_data->m_lpfnMAPIResolveName ==
nullptr ||
112 m_data->m_lpfnMAPIFreeBuffer ==
nullptr) {
114 _T(
"MAIL Error: Failed to get one of the functions pointer in ")
121 _(
"MAIL Error: MAPI32.DLL is not installed or invalid on this "
126void wxMapiSession::Deinitialise() {
127 if (m_data->m_hMapi) {
129 FreeLibrary(m_data->m_hMapi);
130 m_data->m_hMapi =
nullptr;
131 m_data->m_lpfnMAPILogon =
nullptr;
132 m_data->m_lpfnMAPILogoff =
nullptr;
133 m_data->m_lpfnMAPISendMail =
nullptr;
134 m_data->m_lpfnMAPIResolveName =
nullptr;
135 m_data->m_lpfnMAPIFreeBuffer =
nullptr;
139bool wxMapiSession::Logon(
const wxString& sProfileName,
140 const wxString& sPassword, wxWindow* pParentWnd) {
141 wxASSERT(MapiInstalled());
142 wxASSERT(m_data->m_lpfnMAPILogon);
145 bool bSuccess = FALSE;
151 int nProfileLength = sProfileName.Length();
153 LPSTR pszProfileName =
nullptr;
154 LPSTR pszPassword =
nullptr;
155 wxCharBuffer cbProfile(1), cbPassword(1);
156 if (nProfileLength) {
158 pszProfileName = (LPSTR)sProfileName.c_str();
159 pszPassword = (LPSTR)sPassword.c_str();
161 cbProfile = sProfileName.mb_str();
162 cbPassword = sPassword.mb_str();
163 pszProfileName = cbProfile.data();
164 pszPassword = cbPassword.data();
171 if (nProfileLength == 0) {
174 nUIParam = (ULONG_PTR)(HWND)pParentWnd->GetHWND();
175 flags |= MAPI_LOGON_UI;
179 if (wxTheApp->GetTopWindow()) {
180 nUIParam = (ULONG_PTR)(HWND)wxTheApp->GetTopWindow()->GetHWND();
181 flags |= MAPI_LOGON_UI;
189 m_data->m_lpfnMAPILogon(nUIParam, pszProfileName, pszPassword,
190 flags | MAPI_NEW_SESSION, 0, &m_data->m_hSession);
191 if (nError != SUCCESS_SUCCESS && nError != MAPI_E_USER_ABORT) {
195 nError = m_data->m_lpfnMAPILogon(nUIParam,
nullptr,
nullptr, 0, 0,
196 &m_data->m_hSession);
197 if (nError == SUCCESS_SUCCESS) {
198 m_data->m_nLastError = SUCCESS_SUCCESS;
202 _T(
"MAIL Error: Failed to logon to MAPI using a shared session, ")
205 m_data->m_nLastError = nError;
207 }
else if (nError == SUCCESS_SUCCESS) {
208 m_data->m_nLastError = SUCCESS_SUCCESS;
215bool wxMapiSession::LoggedOn()
const {
return (m_data->m_hSession != 0); }
217bool wxMapiSession::MapiInstalled()
const {
218 return (m_data->m_hMapi !=
nullptr);
221bool wxMapiSession::Logoff() {
222 wxASSERT(MapiInstalled());
223 wxASSERT(m_data->m_lpfnMAPILogoff);
226 bool bSuccess = FALSE;
228 if (m_data->m_hSession) {
230 ULONG nError = m_data->m_lpfnMAPILogoff(m_data->m_hSession, 0, 0, 0);
231 if (nError != SUCCESS_SUCCESS) {
232 wxLogMessage(_T(
"MAIL Error: Failed in call to MapiLogoff, Error:%ld"),
234 m_data->m_nLastError = nError;
237 m_data->m_nLastError = SUCCESS_SUCCESS;
240 m_data->m_hSession = 0;
246bool wxMapiSession::Resolve(
const wxString& sName,
void* lppRecip1) {
247 lpMapiRecipDesc* lppRecip = (lpMapiRecipDesc*)lppRecip1;
249 wxASSERT(MapiInstalled());
250 wxASSERT(m_data->m_lpfnMAPIResolveName);
251 wxASSERT(LoggedOn());
252 wxASSERT(m_data->m_hSession);
256 LPSTR lpszAsciiName = (LPSTR)sName.c_str();
258 wxCharBuffer cbName(1);
259 cbName = sName.mb_str();
260 LPSTR lpszAsciiName = cbName.data();
262 ULONG nError = m_data->m_lpfnMAPIResolveName(m_data->m_hSession, 0,
263 lpszAsciiName, 0, 0, lppRecip);
264 if (nError != SUCCESS_SUCCESS) {
265 wxLogMessage(_T(
"MAIL Error: Failed to resolve the name: %s, Error:%ld\n"),
266 sName.c_str(), nError);
267 m_data->m_nLastError = nError;
270 return (nError == SUCCESS_SUCCESS);
274 wxASSERT(MapiInstalled());
275 wxASSERT(m_data->m_lpfnMAPISendMail);
276 wxASSERT(m_data->m_lpfnMAPIFreeBuffer);
277 wxASSERT(LoggedOn());
278 wxASSERT(m_data->m_hSession);
281 bool bSuccess = FALSE;
285 MapiMessage mapiMessage;
286 ZeroMemory(&mapiMessage,
sizeof(mapiMessage));
288 mapiMessage.lpszSubject = (LPSTR)message.m_subject.c_str();
289 mapiMessage.lpszNoteText = (LPSTR)message.m_body.c_str();
291 wxCharBuffer cbSubject(1), cbBody(1), cbOriginator(1);
292 cbSubject = message.m_subject.mb_str();
293 cbBody = message.m_body.mb_str();
294 mapiMessage.lpszSubject = cbSubject.data();
295 mapiMessage.lpszNoteText = cbBody.data();
297 mapiMessage.nRecipCount = message.m_to.GetCount() + message.m_cc.GetCount() +
298 message.m_bcc.GetCount();
299 wxASSERT(mapiMessage.nRecipCount);
302 mapiMessage.lpRecips =
new MapiRecipDesc[mapiMessage.nRecipCount];
305 if (!message.m_from.IsEmpty()) {
306 mapiMessage.lpOriginator =
new MapiRecipDesc;
307 ZeroMemory(mapiMessage.lpOriginator,
sizeof(MapiRecipDesc));
309 mapiMessage.lpOriginator->ulRecipClass = MAPI_ORIG;
312 mapiMessage.lpOriginator->lpszName = (LPSTR)message.m_from.c_str();
314 cbOriginator = message.m_from.mb_str();
315 mapiMessage.lpOriginator->lpszName = cbOriginator.data();
321 int nToSize = message.m_to.GetCount();
323 for (i = 0; i < nToSize; i++) {
324 MapiRecipDesc& recip = mapiMessage.lpRecips[nRecipIndex];
325 ZeroMemory(&recip,
sizeof(MapiRecipDesc));
326 recip.ulRecipClass = MAPI_TO;
327 wxString& sName = message.m_to[i];
330 lpMapiRecipDesc lpTempRecip;
331 if (Resolve(sName, (
void*)&lpTempRecip)) {
333 sName = wxString(lpTempRecip->lpszName, *wxConvCurrent);
336 m_data->m_lpfnMAPIFreeBuffer(lpTempRecip);
339 recip.lpszName = (LPSTR)sName.c_str();
341 recip.lpszName = sName.mb_str().release();
444 ULONG nError = m_data->m_lpfnMAPISendMail(m_data->m_hSession, 0, &mapiMessage,
446 if (nError == SUCCESS_SUCCESS) {
448 m_data->m_nLastError = SUCCESS_SUCCESS;
450 wxLogMessage(_T(
"MAIL Error: Failed to send mail message, Error:%ld\n"),
452 m_data->m_nLastError = nError;
470 for (i = 0; i < nRecipIndex; i++) free(mapiMessage.lpRecips[i].lpszName);
472 delete[] mapiMessage.lpRecips;
474 delete mapiMessage.lpOriginator;
479long wxMapiSession::GetLastError()
const {
return m_data->m_nLastError; }