1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Simple MAPI classes
4 // Author: PJ Naughter <pjna@naughter.com>
5 // Modified by: Julian Smart
8 // Copyright: (c) PJ Naughter
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "smapi.h"
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
27 #include "wx/string.h"
28 #include "wx/msw/private.h"
32 #include "wx/net/smapi.h"
34 class WXDLLIMPEXP_NETUTILS wxMapiData
42 m_lpfnMAPILogon
= NULL
;
43 m_lpfnMAPILogoff
= NULL
;
44 m_lpfnMAPISendMail
= NULL
;
45 m_lpfnMAPIResolveName
= NULL
;
46 m_lpfnMAPIFreeBuffer
= NULL
;
50 LHANDLE m_hSession
; //Mapi Session handle
51 long m_nLastError
; //Last Mapi error value
52 HINSTANCE m_hMapi
; //Instance handle of the MAPI dll
53 LPMAPILOGON m_lpfnMAPILogon
; //MAPILogon function pointer
54 LPMAPILOGOFF m_lpfnMAPILogoff
; //MAPILogoff function pointer
55 LPMAPISENDMAIL m_lpfnMAPISendMail
; //MAPISendMail function pointer
56 LPMAPIRESOLVENAME m_lpfnMAPIResolveName
; //MAPIResolveName function pointer
57 LPMAPIFREEBUFFER m_lpfnMAPIFreeBuffer
; //MAPIFreeBuffer function pointer
61 ////////////////////////////////// Implementation /////////////////////////////
63 wxMapiSession::wxMapiSession()
65 m_data
= new wxMapiData
;
70 wxMapiSession::~wxMapiSession()
81 void wxMapiSession::Initialise()
83 //First make sure the "WIN.INI" entry for MAPI is present aswell
84 //as the MAPI32 dll being present on the system
85 bool bMapiInstalled
= (GetProfileInt(_T("MAIL"), _T("MAPI"), 0) != 0) &&
86 (SearchPath(NULL
, _T("MAPI32.DLL"), NULL
, 0, NULL
, NULL
) != 0);
90 //Load up the MAPI dll and get the function pointers we are interested in
91 m_data
->m_hMapi
= ::LoadLibrary(_T("MAPI32.DLL"));
94 m_data
->m_lpfnMAPILogon
= (LPMAPILOGON
) GetProcAddress(m_data
->m_hMapi
, "MAPILogon");
95 m_data
->m_lpfnMAPILogoff
= (LPMAPILOGOFF
) GetProcAddress(m_data
->m_hMapi
, "MAPILogoff");
96 m_data
->m_lpfnMAPISendMail
= (LPMAPISENDMAIL
) GetProcAddress(m_data
->m_hMapi
, "MAPISendMail");
97 m_data
->m_lpfnMAPIResolveName
= (LPMAPIRESOLVENAME
) GetProcAddress(m_data
->m_hMapi
, "MAPIResolveName");
98 m_data
->m_lpfnMAPIFreeBuffer
= (LPMAPIFREEBUFFER
) GetProcAddress(m_data
->m_hMapi
, "MAPIFreeBuffer");
100 //If any of the functions are not installed then fail the load
101 if (m_data
->m_lpfnMAPILogon
== NULL
||
102 m_data
->m_lpfnMAPILogoff
== NULL
||
103 m_data
->m_lpfnMAPISendMail
== NULL
||
104 m_data
->m_lpfnMAPIResolveName
== NULL
||
105 m_data
->m_lpfnMAPIFreeBuffer
== NULL
)
107 wxLogDebug(_T("Failed to get one of the functions pointer in MAPI32.DLL\n"));
113 wxLogDebug(_T("Mapi is not installed on this computer\n"));
116 void wxMapiSession::Deinitialise()
120 //Unload the MAPI dll and reset the function pointers to NULL
121 FreeLibrary(m_data
->m_hMapi
);
122 m_data
->m_hMapi
= NULL
;
123 m_data
->m_lpfnMAPILogon
= NULL
;
124 m_data
->m_lpfnMAPILogoff
= NULL
;
125 m_data
->m_lpfnMAPISendMail
= NULL
;
126 m_data
->m_lpfnMAPIResolveName
= NULL
;
127 m_data
->m_lpfnMAPIFreeBuffer
= NULL
;
131 bool wxMapiSession::Logon(const wxString
& sProfileName
, const wxString
& sPassword
, wxWindow
* pParentWnd
)
133 wxASSERT(MapiInstalled()); //MAPI must be installed
134 wxASSERT(m_data
->m_lpfnMAPILogon
); //Function pointer must be valid
136 //Initialise the function return value
137 bool bSuccess
= FALSE
;
139 //Just in case we are already logged in
142 //Setup the ascii versions of the profile name and password
143 int nProfileLength
= sProfileName
.Length();
145 LPSTR pszProfileName
= NULL
;
146 LPSTR pszPassword
= NULL
;
147 wxCharBuffer
cbProfile(1),cbPassword(1);
151 pszProfileName
= (LPSTR
) sProfileName
.c_str();
152 pszPassword
= (LPSTR
) sPassword
.c_str();
154 cbProfile
= sProfileName
.mb_str();
155 cbPassword
= sPassword
.mb_str();
156 pszProfileName
= cbProfile
.data();
157 pszPassword
= cbPassword
.data();
161 //Setup the flags & UIParam parameters used in the MapiLogon call
164 if (nProfileLength
== 0)
166 //No profile name given, then we must interactively request a profile name
169 nUIParam
= (ULONG
) (HWND
) pParentWnd
->GetHWND();
170 flags
|= MAPI_LOGON_UI
;
174 //No window given, just use the main window of the app as the parent window
175 if (wxTheApp
->GetTopWindow())
177 nUIParam
= (ULONG
) (HWND
) wxTheApp
->GetTopWindow()->GetHWND();
178 flags
|= MAPI_LOGON_UI
;
183 //First try to acquire a new MAPI session using the supplied settings using the MAPILogon functio
184 ULONG nError
= m_data
->m_lpfnMAPILogon(nUIParam
, pszProfileName
, pszPassword
, flags
| MAPI_NEW_SESSION
, 0, &m_data
->m_hSession
);
185 if (nError
!= SUCCESS_SUCCESS
&& nError
!= MAPI_E_USER_ABORT
)
187 //Failed to create a create mapi session, try to acquire a shared mapi session
188 wxLogDebug(_T("Failed to logon to MAPI using a new session, trying to acquire a shared one\n"));
189 nError
= m_data
->m_lpfnMAPILogon(nUIParam
, NULL
, NULL
, 0, 0, &m_data
->m_hSession
);
190 if (nError
== SUCCESS_SUCCESS
)
192 m_data
->m_nLastError
= SUCCESS_SUCCESS
;
197 wxLogDebug(_T("Failed to logon to MAPI using a shared session, Error:%ld\n"), nError
);
198 m_data
->m_nLastError
= nError
;
201 else if (nError
== SUCCESS_SUCCESS
)
203 m_data
->m_nLastError
= SUCCESS_SUCCESS
;
210 bool wxMapiSession::LoggedOn() const
212 return (m_data
->m_hSession
!= 0);
215 bool wxMapiSession::MapiInstalled() const
217 return (m_data
->m_hMapi
!= NULL
);
220 bool wxMapiSession::Logoff()
222 wxASSERT(MapiInstalled()); //MAPI must be installed
223 wxASSERT(m_data
->m_lpfnMAPILogoff
); //Function pointer must be valid
225 //Initialise the function return value
226 bool bSuccess
= FALSE
;
228 if (m_data
->m_hSession
)
230 //Call the MAPILogoff function
231 ULONG nError
= m_data
->m_lpfnMAPILogoff(m_data
->m_hSession
, 0, 0, 0);
232 if (nError
!= SUCCESS_SUCCESS
)
234 wxLogDebug(_T("Failed in call to MapiLogoff, Error:%ld"), nError
);
235 m_data
->m_nLastError
= nError
;
240 m_data
->m_nLastError
= SUCCESS_SUCCESS
;
243 m_data
->m_hSession
= 0;
249 bool wxMapiSession::Resolve(const wxString
& sName
, void* lppRecip1
)
251 lpMapiRecipDesc
* lppRecip
= (lpMapiRecipDesc
*) lppRecip1
;
253 wxASSERT(MapiInstalled()); //MAPI must be installed
254 wxASSERT(m_data
->m_lpfnMAPIResolveName
); //Function pointer must be valid
255 wxASSERT(LoggedOn()); //Must be logged on to MAPI
256 wxASSERT(m_data
->m_hSession
); //MAPI session handle must be valid
258 //Call the MAPIResolveName function
260 LPSTR lpszAsciiName
= (LPSTR
) sName
.c_str();
262 wxCharBuffer
cbName(1);
263 cbName
= sName
.mb_str();
264 LPSTR lpszAsciiName
= cbName
.data();
266 ULONG nError
= m_data
->m_lpfnMAPIResolveName(m_data
->m_hSession
, 0, lpszAsciiName
, 0, 0, lppRecip
);
267 if (nError
!= SUCCESS_SUCCESS
)
269 wxLogDebug(_T("Failed to resolve the name: %s, Error:%ld\n"),
270 sName
.c_str(), nError
);
271 m_data
->m_nLastError
= nError
;
274 return (nError
== SUCCESS_SUCCESS
);
277 bool wxMapiSession::Send(wxMailMessage
& message
)
279 wxASSERT(MapiInstalled()); //MAPI must be installed
280 wxASSERT(m_data
->m_lpfnMAPISendMail
); //Function pointer must be valid
281 wxASSERT(m_data
->m_lpfnMAPIFreeBuffer
); //Function pointer must be valid
282 wxASSERT(LoggedOn()); //Must be logged on to MAPI
283 wxASSERT(m_data
->m_hSession
); //MAPI session handle must be valid
285 //Initialise the function return value
286 bool bSuccess
= FALSE
;
288 //Create the MapiMessage structure to match the message parameter send into us
289 MapiMessage mapiMessage
;
290 ZeroMemory(&mapiMessage
, sizeof(mapiMessage
));
292 mapiMessage
.lpszSubject
= (LPSTR
) message
.m_subject
.c_str();
293 mapiMessage
.lpszNoteText
= (LPSTR
) message
.m_body
.c_str();
295 wxCharBuffer
cbSubject(1),cbBody(1),cbOriginator(1);
296 cbSubject
= message
.m_subject
.mb_str();
297 cbBody
= message
.m_body
.mb_str();
298 mapiMessage
.lpszSubject
= cbSubject
.data();
299 mapiMessage
.lpszNoteText
= cbBody
.data();
301 mapiMessage
.nRecipCount
= message
.m_to
.GetCount() + message
.m_cc
.GetCount() + message
.m_bcc
.GetCount();
302 wxASSERT(mapiMessage
.nRecipCount
); //Must have at least 1 recipient!
304 //Allocate the recipients array
305 mapiMessage
.lpRecips
= new MapiRecipDesc
[mapiMessage
.nRecipCount
];
307 // If we have a 'From' field, use it
308 if (!message
.m_from
.IsEmpty())
310 mapiMessage
.lpOriginator
= new MapiRecipDesc
;
311 ZeroMemory(mapiMessage
.lpOriginator
, sizeof(MapiRecipDesc
));
313 mapiMessage
.lpOriginator
->ulRecipClass
= MAPI_ORIG
;
314 // TODO Do we have to call Resolve?
316 mapiMessage
.lpOriginator
->lpszName
= (LPSTR
) message
.m_from
.c_str();
318 cbOriginator
= message
.m_from
.mb_str();
319 mapiMessage
.lpOriginator
->lpszName
= cbOriginator
.data();
323 //Setup the "To" recipients
325 int nToSize
= message
.m_to
.GetCount();
327 for (i
=0; i
<nToSize
; i
++)
329 MapiRecipDesc
& recip
= mapiMessage
.lpRecips
[nRecipIndex
];
330 ZeroMemory(&recip
, sizeof(MapiRecipDesc
));
331 recip
.ulRecipClass
= MAPI_TO
;
332 wxString
& sName
= message
.m_to
[i
];
334 //Try to resolve the name
335 lpMapiRecipDesc lpTempRecip
;
336 if (Resolve(sName
, (void*) &lpTempRecip
))
338 //Resolve worked, put the resolved name back into the sName
339 sName
= wxString(lpTempRecip
->lpszName
,*wxConvCurrent
);
341 //Don't forget to free up the memory MAPI allocated for us
342 m_data
->m_lpfnMAPIFreeBuffer(lpTempRecip
);
345 recip
.lpszName
= (LPSTR
) sName
.c_str();
347 recip
.lpszName
= sName
.mb_str().release();
353 //Setup the "CC" recipients
354 int nCCSize
= message
.m_cc
.GetCount();
355 for (i
=0; i
<nCCSize
; i
++)
357 MapiRecipDesc
& recip
= mapiMessage
.lpRecips
[nRecipIndex
];
358 ZeroMemory(&recip
, sizeof(MapiRecipDesc
));
359 recip
.ulRecipClass
= MAPI_CC
;
360 wxString
& sName
= message
.m_cc
[i
];
362 //Try to resolve the name
363 lpMapiRecipDesc lpTempRecip
;
364 if (Resolve(sName
, (void*) &lpTempRecip
))
366 //Resolve worked, put the resolved name back into the sName
367 sName
= wxString(lpTempRecip
->lpszName
,*wxConvCurrent
);
369 //Don't forget to free up the memory MAPI allocated for us
370 m_data
->m_lpfnMAPIFreeBuffer(lpTempRecip
);
373 recip
.lpszName
= (LPSTR
) sName
.c_str();
375 recip
.lpszName
= sName
.mb_str().release();
381 //Setup the "BCC" recipients
382 int nBCCSize
= message
.m_bcc
.GetCount();
383 for (i
=0; i
<nBCCSize
; i
++)
385 MapiRecipDesc
& recip
= mapiMessage
.lpRecips
[nRecipIndex
];
386 ZeroMemory(&recip
, sizeof(MapiRecipDesc
));
387 recip
.ulRecipClass
= MAPI_BCC
;
388 wxString
& sName
= message
.m_bcc
[i
];
390 //Try to resolve the name
391 lpMapiRecipDesc lpTempRecip
;
392 if (Resolve(sName
, (void*) &lpTempRecip
))
394 //Resolve worked, put the resolved name back into the sName
395 sName
= wxString(lpTempRecip
->lpszName
,wxConvCurrent
);
397 //Don't forget to free up the memory MAPI allocated for us
398 m_data
->m_lpfnMAPIFreeBuffer(lpTempRecip
);
401 recip
.lpszName
= (LPSTR
) sName
.c_str();
403 recip
.lpszName
= sName
.mb_str().release();
409 //Setup the attachments
410 int nAttachmentSize
= message
.m_attachments
.GetCount();
411 int nTitleSize
= message
.m_attachmentTitles
.GetCount();
414 wxASSERT(nTitleSize
== nAttachmentSize
); //If you are going to set the attachment titles then you must set
415 //the attachment title for each attachment
419 mapiMessage
.nFileCount
= nAttachmentSize
;
420 mapiMessage
.lpFiles
= new MapiFileDesc
[nAttachmentSize
];
421 for (i
=0; i
<nAttachmentSize
; i
++)
423 MapiFileDesc
& file
= mapiMessage
.lpFiles
[i
];
424 ZeroMemory(&file
, sizeof(MapiFileDesc
));
425 file
.nPosition
= 0xFFFFFFFF;
426 wxString
& sFilename
= message
.m_attachments
[i
];
429 file
.lpszPathName
= (LPSTR
) sFilename
.c_str();
431 file
.lpszPathName
= sFilename
.mb_str().release();
433 //file.lpszFileName = file.lpszPathName;
434 file
.lpszFileName
= NULL
;
436 if (nTitleSize
&& !message
.m_attachmentTitles
[i
].IsEmpty())
438 wxString
& sTitle
= message
.m_attachmentTitles
[i
];
440 file
.lpszFileName
= (LPSTR
) sTitle
.c_str();
442 file
.lpszFileName
= sTitle
.mb_str().release();
448 //Do the actual send using MAPISendMail
449 ULONG nError
= m_data
->m_lpfnMAPISendMail(m_data
->m_hSession
, 0, &mapiMessage
, MAPI_DIALOG
, 0);
450 if (nError
== SUCCESS_SUCCESS
)
453 m_data
->m_nLastError
= SUCCESS_SUCCESS
;
457 wxLogDebug(_T("Failed to send mail message, Error:%ld\n"), nError
);
458 m_data
->m_nLastError
= nError
;
461 //Tidy up the Attachements
465 for (i
= 0;i
< nAttachmentSize
;i
++)
467 free(mapiMessage
.lpFiles
[i
].lpszPathName
);
468 free(mapiMessage
.lpFiles
[i
].lpszFileName
);
471 delete [] mapiMessage
.lpFiles
;
474 //Free up the Recipients and Originator memory
476 for (i
= 0;i
< nRecipIndex
;i
++)
477 free(mapiMessage
.lpRecips
[i
].lpszName
);
479 delete [] mapiMessage
.lpRecips
;
481 delete mapiMessage
.lpOriginator
;
486 long wxMapiSession::GetLastError() const
488 return m_data
->m_nLastError
;