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"
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();
144 int nPasswordLength
= sPassword
.Length();
146 LPSTR pszProfileName
= NULL
;
147 LPSTR pszPassword
= NULL
;
150 // pszProfileName = T2A((LPTSTR) (LPCTSTR) sProfileName);
151 // pszPassword = T2A((LPTSTR) (LPCTSTR) sPassword);
152 pszProfileName
= (LPSTR
) sProfileName
.c_str();
153 pszPassword
= (LPSTR
) sPassword
.c_str();
156 //Setup the flags & UIParam parameters used in the MapiLogon call
159 if (nProfileLength
== 0)
161 //No profile name given, then we must interactively request a profile name
164 nUIParam
= (ULONG
) (HWND
) pParentWnd
->GetHWND();
165 flags
|= MAPI_LOGON_UI
;
169 //No window given, just use the main window of the app as the parent window
170 if (wxTheApp
->GetTopWindow())
172 nUIParam
= (ULONG
) (HWND
) wxTheApp
->GetTopWindow()->GetHWND();
173 flags
|= MAPI_LOGON_UI
;
178 //First try to acquire a new MAPI session using the supplied settings using the MAPILogon functio
179 ULONG nError
= m_data
->m_lpfnMAPILogon(nUIParam
, pszProfileName
, pszPassword
, flags
| MAPI_NEW_SESSION
, 0, &m_data
->m_hSession
);
180 if (nError
!= SUCCESS_SUCCESS
&& nError
!= MAPI_E_USER_ABORT
)
182 //Failed to create a create mapi session, try to acquire a shared mapi session
183 wxLogDebug(_T("Failed to logon to MAPI using a new session, trying to acquire a shared one\n"));
184 nError
= m_data
->m_lpfnMAPILogon(nUIParam
, NULL
, NULL
, 0, 0, &m_data
->m_hSession
);
185 if (nError
== SUCCESS_SUCCESS
)
187 m_data
->m_nLastError
= SUCCESS_SUCCESS
;
192 wxLogDebug(_T("Failed to logon to MAPI using a shared session, Error:%d\n"), nError
);
193 m_data
->m_nLastError
= nError
;
196 else if (nError
== SUCCESS_SUCCESS
)
198 m_data
->m_nLastError
= SUCCESS_SUCCESS
;
205 bool wxMapiSession::LoggedOn() const
207 return (m_data
->m_hSession
!= 0);
210 bool wxMapiSession::MapiInstalled() const
212 return (m_data
->m_hMapi
!= NULL
);
215 bool wxMapiSession::Logoff()
217 wxASSERT(MapiInstalled()); //MAPI must be installed
218 wxASSERT(m_data
->m_lpfnMAPILogoff
); //Function pointer must be valid
220 //Initialise the function return value
221 bool bSuccess
= FALSE
;
223 if (m_data
->m_hSession
)
225 //Call the MAPILogoff function
226 ULONG nError
= m_data
->m_lpfnMAPILogoff(m_data
->m_hSession
, 0, 0, 0);
227 if (nError
!= SUCCESS_SUCCESS
)
229 wxLogDebug(_T("Failed in call to MapiLogoff, Error:%d"), nError
);
230 m_data
->m_nLastError
= nError
;
235 m_data
->m_nLastError
= SUCCESS_SUCCESS
;
238 m_data
->m_hSession
= 0;
244 bool wxMapiSession::Resolve(const wxString
& sName
, void* lppRecip1
)
246 lpMapiRecipDesc
* lppRecip
= (lpMapiRecipDesc
*) lppRecip1
;
248 wxASSERT(MapiInstalled()); //MAPI must be installed
249 wxASSERT(m_data
->m_lpfnMAPIResolveName
); //Function pointer must be valid
250 wxASSERT(LoggedOn()); //Must be logged on to MAPI
251 wxASSERT(m_data
->m_hSession
); //MAPI session handle must be valid
253 //Call the MAPIResolveName function
254 // LPSTR lpszAsciiName = T2A((LPTSTR) (LPCTSTR) sName);
255 LPSTR lpszAsciiName
= (LPSTR
) sName
.c_str();
256 ULONG nError
= m_data
->m_lpfnMAPIResolveName(m_data
->m_hSession
, 0, lpszAsciiName
, 0, 0, lppRecip
);
257 if (nError
!= SUCCESS_SUCCESS
)
259 wxLogDebug(_T("Failed to resolve the name: %s, Error:%d\n"), sName
, nError
);
260 m_data
->m_nLastError
= nError
;
263 return (nError
== SUCCESS_SUCCESS
);
266 bool wxMapiSession::Send(wxMailMessage
& message
)
268 wxASSERT(MapiInstalled()); //MAPI must be installed
269 wxASSERT(m_data
->m_lpfnMAPISendMail
); //Function pointer must be valid
270 wxASSERT(m_data
->m_lpfnMAPIFreeBuffer
); //Function pointer must be valid
271 wxASSERT(LoggedOn()); //Must be logged on to MAPI
272 wxASSERT(m_data
->m_hSession
); //MAPI session handle must be valid
274 //Initialise the function return value
275 bool bSuccess
= FALSE
;
277 //Create the MapiMessage structure to match the message parameter send into us
278 MapiMessage mapiMessage
;
279 ZeroMemory(&mapiMessage
, sizeof(mapiMessage
));
280 mapiMessage
.lpszSubject
= (LPSTR
) message
.m_subject
.c_str();
281 mapiMessage
.lpszNoteText
= (LPSTR
) message
.m_body
.c_str();
282 // mapiMessage.lpszSubject = T2A((LPTSTR) (LPCTSTR) message.m_subject);
283 // mapiMessage.lpszNoteText = T2A((LPTSTR) (LPCTSTR) message.m_body);
284 mapiMessage
.nRecipCount
= message
.m_to
.GetCount() + message
.m_cc
.GetCount() + message
.m_bcc
.GetCount();
285 wxASSERT(mapiMessage
.nRecipCount
); //Must have at least 1 recipient!
287 //Allocate the recipients array
288 mapiMessage
.lpRecips
= new MapiRecipDesc
[mapiMessage
.nRecipCount
];
290 //Setup the "To" recipients
292 int nToSize
= message
.m_to
.GetCount();
293 for (int i
=0; i
<nToSize
; i
++)
295 MapiRecipDesc
& recip
= mapiMessage
.lpRecips
[nRecipIndex
];
296 ZeroMemory(&recip
, sizeof(MapiRecipDesc
));
297 recip
.ulRecipClass
= MAPI_TO
;
298 wxString
& sName
= message
.m_to
[i
];
300 //Try to resolve the name
301 lpMapiRecipDesc lpTempRecip
;
302 if (Resolve(sName
, (void*) &lpTempRecip
))
304 //Resolve worked, put the resolved name back into the sName
305 sName
= lpTempRecip
->lpszName
;
307 //Don't forget to free up the memory MAPI allocated for us
308 m_data
->m_lpfnMAPIFreeBuffer(lpTempRecip
);
310 //recip.lpszName = T2A((LPTSTR) (LPCTSTR) sName);
311 recip
.lpszName
= (LPSTR
) sName
.c_str();
316 //Setup the "CC" recipients
317 int nCCSize
= message
.m_cc
.GetCount();
318 for (i
=0; i
<nCCSize
; i
++)
320 MapiRecipDesc
& recip
= mapiMessage
.lpRecips
[nRecipIndex
];
321 ZeroMemory(&recip
, sizeof(MapiRecipDesc
));
322 recip
.ulRecipClass
= MAPI_CC
;
323 wxString
& sName
= message
.m_cc
[i
];
325 //Try to resolve the name
326 lpMapiRecipDesc lpTempRecip
;
327 if (Resolve(sName
, (void*) &lpTempRecip
))
329 //Resolve worked, put the resolved name back into the sName
330 sName
= lpTempRecip
->lpszName
;
332 //Don't forget to free up the memory MAPI allocated for us
333 m_data
->m_lpfnMAPIFreeBuffer(lpTempRecip
);
335 //recip.lpszName = T2A((LPTSTR) (LPCTSTR) sName);
336 recip
.lpszName
= (LPSTR
) sName
.c_str();
341 //Setup the "BCC" recipients
342 int nBCCSize
= message
.m_bcc
.GetCount();
343 for (i
=0; i
<nBCCSize
; i
++)
345 MapiRecipDesc
& recip
= mapiMessage
.lpRecips
[nRecipIndex
];
346 ZeroMemory(&recip
, sizeof(MapiRecipDesc
));
347 recip
.ulRecipClass
= MAPI_BCC
;
348 wxString
& sName
= message
.m_bcc
[i
];
350 //Try to resolve the name
351 lpMapiRecipDesc lpTempRecip
;
352 if (Resolve(sName
, (void*) &lpTempRecip
))
354 //Resolve worked, put the resolved name back into the sName
355 sName
= lpTempRecip
->lpszName
;
357 //Don't forget to free up the memory MAPI allocated for us
358 m_data
->m_lpfnMAPIFreeBuffer(lpTempRecip
);
360 //recip.lpszName = T2A((LPTSTR) (LPCTSTR) sName);
361 recip
.lpszName
= (LPSTR
) sName
.c_str();
366 //Setup the attachments
367 int nAttachmentSize
= message
.m_attachments
.GetCount();
368 int nTitleSize
= message
.m_attachmentTitles
.GetCount();
371 wxASSERT(nTitleSize
== nAttachmentSize
); //If you are going to set the attachment titles then you must set
372 //the attachment title for each attachment
376 mapiMessage
.nFileCount
= nAttachmentSize
;
377 mapiMessage
.lpFiles
= new MapiFileDesc
[nAttachmentSize
];
378 for (i
=0; i
<nAttachmentSize
; i
++)
380 MapiFileDesc
& file
= mapiMessage
.lpFiles
[i
];
381 ZeroMemory(&file
, sizeof(MapiFileDesc
));
382 file
.nPosition
= 0xFFFFFFFF;
383 wxString
& sFilename
= message
.m_attachments
[i
];
384 //file.lpszPathName = T2A((LPTSTR) (LPCTSTR) sFilename);
386 file
.lpszPathName
= (LPSTR
) sFilename
.c_str();
387 //file.lpszFileName = file.lpszPathName;
388 file
.lpszFileName
= NULL
;
390 if (nTitleSize
&& !message
.m_attachmentTitles
[i
].IsEmpty())
392 wxString
& sTitle
= message
.m_attachmentTitles
[i
];
393 //file.lpszFileName = T2A((LPTSTR) (LPCTSTR) sTitle);
394 file
.lpszFileName
= (LPSTR
) sTitle
.c_str();
399 //Do the actual send using MAPISendMail
400 ULONG nError
= m_data
->m_lpfnMAPISendMail(m_data
->m_hSession
, 0, &mapiMessage
, MAPI_DIALOG
, 0);
401 if (nError
== SUCCESS_SUCCESS
)
404 m_data
->m_nLastError
= SUCCESS_SUCCESS
;
408 wxLogDebug(_T("Failed to send mail message, Error:%d\n"), nError
);
409 m_data
->m_nLastError
= nError
;
412 //Tidy up the Attachements
414 delete [] mapiMessage
.lpFiles
;
416 //Free up the Recipients memory
417 delete [] mapiMessage
.lpRecips
;
422 long wxMapiSession::GetLastError() const
424 return m_data
->m_nLastError
;