31#include "wx/msw/private.h"
38#include <w32api/mapi.h>
45class WXDLLIMPEXP_NETUTILS wxMapiData {
51 m_lpfnMAPILogon =
nullptr;
52 m_lpfnMAPILogoff =
nullptr;
53 m_lpfnMAPISendMail =
nullptr;
54 m_lpfnMAPIResolveName =
nullptr;
55 m_lpfnMAPIFreeBuffer =
nullptr;
62 LPMAPILOGON m_lpfnMAPILogon;
63 LPMAPILOGOFF m_lpfnMAPILogoff;
64 LPMAPISENDMAIL m_lpfnMAPISendMail;
65 LPMAPIRESOLVENAME m_lpfnMAPIResolveName;
66 LPMAPIFREEBUFFER m_lpfnMAPIFreeBuffer;
71wxMapiSession::wxMapiSession() {
72 m_data =
new wxMapiData;
77wxMapiSession::~wxMapiSession() {
87void wxMapiSession::Initialise() {
91 (GetProfileInt(L
"MAIL", L
"MAPI", 0) != 0) &&
92 (SearchPath(
nullptr, L
"MAPI32.DLL",
nullptr, 0,
nullptr,
nullptr) != 0);
96 m_data->m_hMapi = ::LoadLibrary(L
"MAPI32.DLL");
97 if (m_data->m_hMapi) {
98 m_data->m_lpfnMAPILogon =
99 (LPMAPILOGON)GetProcAddress(m_data->m_hMapi,
"MAPILogon");
100 m_data->m_lpfnMAPILogoff =
101 (LPMAPILOGOFF)GetProcAddress(m_data->m_hMapi,
"MAPILogoff");
102 m_data->m_lpfnMAPISendMail =
103 (LPMAPISENDMAIL)GetProcAddress(m_data->m_hMapi,
"MAPISendMail");
104 m_data->m_lpfnMAPIResolveName =
105 (LPMAPIRESOLVENAME)GetProcAddress(m_data->m_hMapi,
"MAPIResolveName");
106 m_data->m_lpfnMAPIFreeBuffer =
107 (LPMAPIFREEBUFFER)GetProcAddress(m_data->m_hMapi,
"MAPIFreeBuffer");
110 if (m_data->m_lpfnMAPILogon ==
nullptr ||
111 m_data->m_lpfnMAPILogoff ==
nullptr ||
112 m_data->m_lpfnMAPISendMail ==
nullptr ||
113 m_data->m_lpfnMAPIResolveName ==
nullptr ||
114 m_data->m_lpfnMAPIFreeBuffer ==
nullptr) {
116 "MAIL Error: Failed to get one of the functions pointer in "
123 _(
"MAIL Error: MAPI32.DLL is not installed or invalid on this "
128void wxMapiSession::Deinitialise() {
129 if (m_data->m_hMapi) {
131 FreeLibrary(m_data->m_hMapi);
132 m_data->m_hMapi =
nullptr;
133 m_data->m_lpfnMAPILogon =
nullptr;
134 m_data->m_lpfnMAPILogoff =
nullptr;
135 m_data->m_lpfnMAPISendMail =
nullptr;
136 m_data->m_lpfnMAPIResolveName =
nullptr;
137 m_data->m_lpfnMAPIFreeBuffer =
nullptr;
141bool wxMapiSession::Logon(
const wxString& sProfileName,
142 const wxString& sPassword, wxWindow* pParentWnd) {
143 wxASSERT(MapiInstalled());
144 wxASSERT(m_data->m_lpfnMAPILogon);
147 bool bSuccess = FALSE;
153 int nProfileLength = sProfileName.Length();
155 LPSTR pszProfileName =
nullptr;
156 LPSTR pszPassword =
nullptr;
157 wxCharBuffer cbProfile(1), cbPassword(1);
158 if (nProfileLength) {
160 pszProfileName = (LPSTR)sProfileName.c_str();
161 pszPassword = (LPSTR)sPassword.c_str();
163 cbProfile = sProfileName.mb_str();
164 cbPassword = sPassword.mb_str();
165 pszProfileName = cbProfile.data();
166 pszPassword = cbPassword.data();
173 if (nProfileLength == 0) {
176 nUIParam = (ULONG_PTR)(HWND)pParentWnd->GetHWND();
177 flags |= MAPI_LOGON_UI;
181 if (wxTheApp->GetTopWindow()) {
182 nUIParam = (ULONG_PTR)(HWND)wxTheApp->GetTopWindow()->GetHWND();
183 flags |= MAPI_LOGON_UI;
191 m_data->m_lpfnMAPILogon(nUIParam, pszProfileName, pszPassword,
192 flags | MAPI_NEW_SESSION, 0, &m_data->m_hSession);
193 if (nError != SUCCESS_SUCCESS && nError != MAPI_E_USER_ABORT) {
197 nError = m_data->m_lpfnMAPILogon(nUIParam,
nullptr,
nullptr, 0, 0,
198 &m_data->m_hSession);
199 if (nError == SUCCESS_SUCCESS) {
200 m_data->m_nLastError = SUCCESS_SUCCESS;
204 "MAIL Error: Failed to logon to MAPI using a shared session, "
207 m_data->m_nLastError = nError;
209 }
else if (nError == SUCCESS_SUCCESS) {
210 m_data->m_nLastError = SUCCESS_SUCCESS;
217bool wxMapiSession::LoggedOn()
const {
return (m_data->m_hSession != 0); }
219bool wxMapiSession::MapiInstalled()
const {
220 return (m_data->m_hMapi !=
nullptr);
223bool wxMapiSession::Logoff() {
224 wxASSERT(MapiInstalled());
225 wxASSERT(m_data->m_lpfnMAPILogoff);
228 bool bSuccess = FALSE;
230 if (m_data->m_hSession) {
232 ULONG nError = m_data->m_lpfnMAPILogoff(m_data->m_hSession, 0, 0, 0);
233 if (nError != SUCCESS_SUCCESS) {
234 wxLogMessage(
"MAIL Error: Failed in call to MapiLogoff, Error:%ld",
236 m_data->m_nLastError = nError;
239 m_data->m_nLastError = SUCCESS_SUCCESS;
242 m_data->m_hSession = 0;
248bool wxMapiSession::Resolve(
const wxString& sName,
void* lppRecip1) {
249 lpMapiRecipDesc* lppRecip = (lpMapiRecipDesc*)lppRecip1;
251 wxASSERT(MapiInstalled());
252 wxASSERT(m_data->m_lpfnMAPIResolveName);
253 wxASSERT(LoggedOn());
254 wxASSERT(m_data->m_hSession);
258 LPSTR lpszAsciiName = (LPSTR)sName.c_str();
260 wxCharBuffer cbName(1);
261 cbName = sName.mb_str();
262 LPSTR lpszAsciiName = cbName.data();
264 ULONG nError = m_data->m_lpfnMAPIResolveName(m_data->m_hSession, 0,
265 lpszAsciiName, 0, 0, lppRecip);
266 if (nError != SUCCESS_SUCCESS) {
267 wxLogMessage(
"MAIL Error: Failed to resolve the name: %s, Error:%ld\n",
268 sName.c_str(), nError);
269 m_data->m_nLastError = nError;
272 return (nError == SUCCESS_SUCCESS);
276 wxASSERT(MapiInstalled());
277 wxASSERT(m_data->m_lpfnMAPISendMail);
278 wxASSERT(m_data->m_lpfnMAPIFreeBuffer);
279 wxASSERT(LoggedOn());
280 wxASSERT(m_data->m_hSession);
283 bool bSuccess = FALSE;
287 MapiMessage mapiMessage;
288 ZeroMemory(&mapiMessage,
sizeof(mapiMessage));
290 mapiMessage.lpszSubject = (LPSTR)message.m_subject.c_str();
291 mapiMessage.lpszNoteText = (LPSTR)message.m_body.c_str();
293 wxCharBuffer cbSubject(1), cbBody(1), cbOriginator(1);
294 cbSubject = message.m_subject.mb_str();
295 cbBody = message.m_body.mb_str();
296 mapiMessage.lpszSubject = cbSubject.data();
297 mapiMessage.lpszNoteText = cbBody.data();
299 mapiMessage.nRecipCount = message.m_to.GetCount() + message.m_cc.GetCount() +
300 message.m_bcc.GetCount();
301 wxASSERT(mapiMessage.nRecipCount);
304 mapiMessage.lpRecips =
new MapiRecipDesc[mapiMessage.nRecipCount];
307 if (!message.m_from.IsEmpty()) {
308 mapiMessage.lpOriginator =
new MapiRecipDesc;
309 ZeroMemory(mapiMessage.lpOriginator,
sizeof(MapiRecipDesc));
311 mapiMessage.lpOriginator->ulRecipClass = MAPI_ORIG;
314 mapiMessage.lpOriginator->lpszName = (LPSTR)message.m_from.c_str();
316 cbOriginator = message.m_from.mb_str();
317 mapiMessage.lpOriginator->lpszName = cbOriginator.data();
323 int nToSize = message.m_to.GetCount();
325 for (i = 0; i < nToSize; i++) {
326 MapiRecipDesc& recip = mapiMessage.lpRecips[nRecipIndex];
327 ZeroMemory(&recip,
sizeof(MapiRecipDesc));
328 recip.ulRecipClass = MAPI_TO;
329 wxString& sName = message.m_to[i];
332 lpMapiRecipDesc lpTempRecip;
333 if (Resolve(sName, (
void*)&lpTempRecip)) {
335 sName = wxString(lpTempRecip->lpszName, *wxConvCurrent);
338 m_data->m_lpfnMAPIFreeBuffer(lpTempRecip);
341 recip.lpszName = (LPSTR)sName.c_str();
343 recip.lpszName = sName.mb_str().release();
446 ULONG nError = m_data->m_lpfnMAPISendMail(m_data->m_hSession, 0, &mapiMessage,
448 if (nError == SUCCESS_SUCCESS) {
450 m_data->m_nLastError = SUCCESS_SUCCESS;
452 wxLogMessage(
"MAIL Error: Failed to send mail message, Error:%ld\n",
454 m_data->m_nLastError = nError;
472 for (i = 0; i < nRecipIndex; i++) free(mapiMessage.lpRecips[i].lpszName);
474 delete[] mapiMessage.lpRecips;
476 delete mapiMessage.lpOriginator;
481long wxMapiSession::GetLastError()
const {
return m_data->m_nLastError; }