xref: /reactos/dll/win32/mapi32/sendmail.c (revision 9d3a9694)
1c2c66affSColin Finck /*
2c2c66affSColin Finck  * MAPISendMail implementation
3c2c66affSColin Finck  *
4c2c66affSColin Finck  * Copyright 2005 Hans Leidekker
5c2c66affSColin Finck  * Copyright 2009 Owen Rudge for CodeWeavers
6c2c66affSColin Finck  *
7c2c66affSColin Finck  * This library is free software; you can redistribute it and/or
8c2c66affSColin Finck  * modify it under the terms of the GNU Lesser General Public
9c2c66affSColin Finck  * License as published by the Free Software Foundation; either
10c2c66affSColin Finck  * version 2.1 of the License, or (at your option) any later version.
11c2c66affSColin Finck  *
12c2c66affSColin Finck  * This library is distributed in the hope that it will be useful,
13c2c66affSColin Finck  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14c2c66affSColin Finck  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15c2c66affSColin Finck  * Lesser General Public License for more details.
16c2c66affSColin Finck  *
17c2c66affSColin Finck  * You should have received a copy of the GNU Lesser General Public
18c2c66affSColin Finck  * License along with this library; if not, write to the Free Software
19c2c66affSColin Finck  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20c2c66affSColin Finck  */
21c2c66affSColin Finck 
22c2c66affSColin Finck 
234dd96d42SAmine Khaldi #include <stdio.h>
244dd96d42SAmine Khaldi #include <stdarg.h>
25c2c66affSColin Finck 
264dd96d42SAmine Khaldi #define COBJMACROS
274dd96d42SAmine Khaldi 
284dd96d42SAmine Khaldi #include "windef.h"
294dd96d42SAmine Khaldi #include "winbase.h"
304dd96d42SAmine Khaldi #include "winerror.h"
314dd96d42SAmine Khaldi #include "winuser.h"
324dd96d42SAmine Khaldi #include "objbase.h"
334dd96d42SAmine Khaldi #include "objidl.h"
344dd96d42SAmine Khaldi #include "mapi.h"
354dd96d42SAmine Khaldi #include "mapix.h"
364dd96d42SAmine Khaldi #include "mapiutil.h"
374dd96d42SAmine Khaldi #include "mapidefs.h"
384dd96d42SAmine Khaldi #include "winreg.h"
394dd96d42SAmine Khaldi #include "shellapi.h"
404dd96d42SAmine Khaldi #include "shlwapi.h"
414dd96d42SAmine Khaldi #include "wine/debug.h"
424dd96d42SAmine Khaldi #include "util.h"
43c2c66affSColin Finck #include "res.h"
44c2c66affSColin Finck 
454dd96d42SAmine Khaldi WINE_DEFAULT_DEBUG_CHANNEL(mapi);
464dd96d42SAmine Khaldi 
47c2c66affSColin Finck #define READ_BUF_SIZE    4096
48c2c66affSColin Finck 
49c2c66affSColin Finck #define STORE_UNICODE_OK  0x00040000
50c2c66affSColin Finck 
convert_from_unicode(LPCWSTR wstr)51c2c66affSColin Finck static LPSTR convert_from_unicode(LPCWSTR wstr)
52c2c66affSColin Finck {
53c2c66affSColin Finck     LPSTR str;
54c2c66affSColin Finck     DWORD len;
55c2c66affSColin Finck 
56c2c66affSColin Finck     if (!wstr)
57c2c66affSColin Finck         return NULL;
58c2c66affSColin Finck 
59c2c66affSColin Finck     len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
60c2c66affSColin Finck     str = HeapAlloc(GetProcessHeap(), 0, len);
61c2c66affSColin Finck     WideCharToMultiByte(CP_ACP, 0, wstr, -1, str, len, NULL, NULL);
62c2c66affSColin Finck 
63c2c66affSColin Finck     return str;
64c2c66affSColin Finck }
65c2c66affSColin Finck 
convert_to_unicode(LPSTR str)66c2c66affSColin Finck static LPWSTR convert_to_unicode(LPSTR str)
67c2c66affSColin Finck {
68c2c66affSColin Finck     LPWSTR wstr;
69c2c66affSColin Finck     DWORD len;
70c2c66affSColin Finck 
71c2c66affSColin Finck     if (!str)
72c2c66affSColin Finck         return NULL;
73c2c66affSColin Finck 
74c2c66affSColin Finck     len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
75c2c66affSColin Finck     wstr = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
76c2c66affSColin Finck     MultiByteToWideChar(CP_ACP, 0, str, -1, wstr, len);
77c2c66affSColin Finck 
78c2c66affSColin Finck     return wstr;
79c2c66affSColin Finck }
80c2c66affSColin Finck 
81c2c66affSColin Finck /*
82c2c66affSColin Finck    Internal function to send a message via Extended MAPI. Wrapper around the Simple
83c2c66affSColin Finck    MAPI function MAPISendMail.
84c2c66affSColin Finck */
sendmail_extended_mapi(LHANDLE mapi_session,ULONG_PTR uiparam,lpMapiMessageW message,FLAGS flags)85c2c66affSColin Finck static ULONG sendmail_extended_mapi(LHANDLE mapi_session, ULONG_PTR uiparam, lpMapiMessageW message,
86c2c66affSColin Finck     FLAGS flags)
87c2c66affSColin Finck {
88c2c66affSColin Finck     ULONG tags[] = {1, 0};
89c2c66affSColin Finck     char *subjectA = NULL, *bodyA = NULL;
90c2c66affSColin Finck     ULONG retval = MAPI_E_FAILURE;
91c2c66affSColin Finck     IMAPISession *session = NULL;
92c2c66affSColin Finck     BOOL unicode_aware = FALSE;
93c2c66affSColin Finck     IMAPITable* msg_table;
94c2c66affSColin Finck     LPSRowSet rows = NULL;
95c2c66affSColin Finck     IMsgStore* msg_store;
96c2c66affSColin Finck     IMAPIFolder* folder = NULL, *draft_folder = NULL;
97c2c66affSColin Finck     LPENTRYID entry_id;
98c2c66affSColin Finck     LPSPropValue props;
99c2c66affSColin Finck     ULONG entry_len;
100c2c66affSColin Finck     DWORD obj_type;
101c2c66affSColin Finck     IMessage* msg;
102c2c66affSColin Finck     ULONG values;
103c2c66affSColin Finck     HRESULT ret;
104c2c66affSColin Finck 
105c2c66affSColin Finck     TRACE("Using Extended MAPI wrapper for MAPISendMail\n");
106c2c66affSColin Finck 
107c2c66affSColin Finck     /* Attempt to log on via Extended MAPI */
108c2c66affSColin Finck 
109c2c66affSColin Finck     ret = MAPILogonEx(0, NULL, NULL, MAPI_EXTENDED | MAPI_USE_DEFAULT | MAPI_NEW_SESSION, &session);
110c2c66affSColin Finck     TRACE("MAPILogonEx: %x\n", ret);
111c2c66affSColin Finck 
112c2c66affSColin Finck     if (ret != S_OK)
113c2c66affSColin Finck     {
114c2c66affSColin Finck         retval = MAPI_E_LOGIN_FAILURE;
115c2c66affSColin Finck         goto cleanup;
116c2c66affSColin Finck     }
117c2c66affSColin Finck 
118c2c66affSColin Finck     /* Open the default message store */
119c2c66affSColin Finck 
120c2c66affSColin Finck     if (IMAPISession_GetMsgStoresTable(session, 0, &msg_table) == S_OK)
121c2c66affSColin Finck     {
122c2c66affSColin Finck         /* We want the default store */
123c2c66affSColin Finck         SizedSPropTagArray(2, columns) = {2, {PR_ENTRYID, PR_DEFAULT_STORE}};
124c2c66affSColin Finck 
125c2c66affSColin Finck         /* Set the columns we want */
126c2c66affSColin Finck         if (IMAPITable_SetColumns(msg_table, (LPSPropTagArray) &columns, 0) == S_OK)
127c2c66affSColin Finck         {
128c2c66affSColin Finck             while (1)
129c2c66affSColin Finck             {
130c2c66affSColin Finck                 if (IMAPITable_QueryRows(msg_table, 1, 0, &rows) != S_OK)
131c2c66affSColin Finck                 {
132c2c66affSColin Finck                     MAPIFreeBuffer(rows);
133c2c66affSColin Finck                     rows = NULL;
134c2c66affSColin Finck                 }
135c2c66affSColin Finck                 else if (rows->cRows != 1)
136c2c66affSColin Finck                 {
137c2c66affSColin Finck                     FreeProws(rows);
138c2c66affSColin Finck                     rows = NULL;
139c2c66affSColin Finck                 }
140c2c66affSColin Finck                 else
141c2c66affSColin Finck                 {
142c2c66affSColin Finck                     /* If it's not the default store, try the next row */
143c2c66affSColin Finck                     if (!rows->aRow[0].lpProps[1].Value.b)
144c2c66affSColin Finck                     {
145c2c66affSColin Finck                         FreeProws(rows);
146c2c66affSColin Finck                         continue;
147c2c66affSColin Finck                     }
148c2c66affSColin Finck                 }
149c2c66affSColin Finck 
150c2c66affSColin Finck                 break;
151c2c66affSColin Finck             }
152c2c66affSColin Finck         }
153c2c66affSColin Finck 
154c2c66affSColin Finck         IMAPITable_Release(msg_table);
155c2c66affSColin Finck     }
156c2c66affSColin Finck 
157c2c66affSColin Finck     /* Did we manage to get the right store? */
158c2c66affSColin Finck     if (!rows)
159c2c66affSColin Finck         goto logoff;
160c2c66affSColin Finck 
161c2c66affSColin Finck     /* Open the message store */
162c2c66affSColin Finck     IMAPISession_OpenMsgStore(session, 0, rows->aRow[0].lpProps[0].Value.bin.cb,
163c2c66affSColin Finck                               (ENTRYID *) rows->aRow[0].lpProps[0].Value.bin.lpb, NULL,
164c2c66affSColin Finck                               MDB_NO_DIALOG | MAPI_BEST_ACCESS, &msg_store);
165c2c66affSColin Finck 
166c2c66affSColin Finck     /* We don't need this any more */
167c2c66affSColin Finck     FreeProws(rows);
168c2c66affSColin Finck 
169c2c66affSColin Finck     /* Check if the message store supports Unicode */
170c2c66affSColin Finck     tags[1] = PR_STORE_SUPPORT_MASK;
171c2c66affSColin Finck     ret = IMsgStore_GetProps(msg_store, (LPSPropTagArray) tags, 0, &values, &props);
172c2c66affSColin Finck 
173c2c66affSColin Finck     if ((ret == S_OK) && (props[0].Value.l & STORE_UNICODE_OK))
174c2c66affSColin Finck         unicode_aware = TRUE;
175c2c66affSColin Finck     else
176c2c66affSColin Finck     {
177c2c66affSColin Finck         /* Don't convert to ANSI */
178c2c66affSColin Finck         if (flags & MAPI_FORCE_UNICODE)
179c2c66affSColin Finck         {
180c2c66affSColin Finck             WARN("No Unicode-capable mail client, and MAPI_FORCE_UNICODE is specified. MAPISendMail failed.\n");
181c2c66affSColin Finck             retval = MAPI_E_UNICODE_NOT_SUPPORTED;
182c2c66affSColin Finck             IMsgStore_Release(msg_store);
183c2c66affSColin Finck             goto logoff;
184c2c66affSColin Finck         }
185c2c66affSColin Finck     }
186c2c66affSColin Finck 
187c2c66affSColin Finck     /* First open the inbox, from which the drafts folder can be opened */
188c2c66affSColin Finck     if (IMsgStore_GetReceiveFolder(msg_store, NULL, 0, &entry_len, &entry_id, NULL) == S_OK)
189c2c66affSColin Finck     {
190c2c66affSColin Finck         IMsgStore_OpenEntry(msg_store, entry_len, entry_id, NULL, 0, &obj_type, (LPUNKNOWN*) &folder);
191c2c66affSColin Finck         MAPIFreeBuffer(entry_id);
192c2c66affSColin Finck     }
193c2c66affSColin Finck 
194c2c66affSColin Finck     tags[1] = PR_IPM_DRAFTS_ENTRYID;
195c2c66affSColin Finck 
196c2c66affSColin Finck     /* Open the drafts folder, or failing that, try asking the message store for the outbox */
197c2c66affSColin Finck     if ((folder == NULL) || ((ret = IMAPIFolder_GetProps(folder, (LPSPropTagArray) tags, 0, &values, &props)) != S_OK))
198c2c66affSColin Finck     {
199c2c66affSColin Finck         TRACE("Unable to open Drafts folder; opening Outbox instead\n");
200c2c66affSColin Finck         tags[1] = PR_IPM_OUTBOX_ENTRYID;
201c2c66affSColin Finck         ret = IMsgStore_GetProps(msg_store, (LPSPropTagArray) tags, 0, &values, &props);
202c2c66affSColin Finck     }
203c2c66affSColin Finck 
204c2c66affSColin Finck     if (ret != S_OK)
205c2c66affSColin Finck         goto logoff;
206c2c66affSColin Finck 
207c2c66affSColin Finck     IMsgStore_OpenEntry(msg_store, props[0].Value.bin.cb, (LPENTRYID) props[0].Value.bin.lpb,
208c2c66affSColin Finck         NULL, MAPI_MODIFY, &obj_type, (LPUNKNOWN *) &draft_folder);
209c2c66affSColin Finck 
210c2c66affSColin Finck     /* Create a new message */
211c2c66affSColin Finck     if (IMAPIFolder_CreateMessage(draft_folder, NULL, 0, &msg) == S_OK)
212c2c66affSColin Finck     {
213c2c66affSColin Finck         ULONG token;
214c2c66affSColin Finck         SPropValue p;
215c2c66affSColin Finck 
216c2c66affSColin Finck         /* Define message properties */
217c2c66affSColin Finck         p.ulPropTag = PR_MESSAGE_FLAGS;
218c2c66affSColin Finck         p.Value.l = MSGFLAG_FROMME | MSGFLAG_UNSENT;
219c2c66affSColin Finck 
220c2c66affSColin Finck         IMessage_SetProps(msg, 1, &p, NULL);
221c2c66affSColin Finck 
222c2c66affSColin Finck         p.ulPropTag = PR_SENTMAIL_ENTRYID;
223c2c66affSColin Finck         p.Value.bin.cb = props[0].Value.bin.cb;
224c2c66affSColin Finck         p.Value.bin.lpb = props[0].Value.bin.lpb;
225c2c66affSColin Finck         IMessage_SetProps(msg, 1,&p, NULL);
226c2c66affSColin Finck 
227c2c66affSColin Finck         /* Set message subject */
228c2c66affSColin Finck         if (message->lpszSubject)
229c2c66affSColin Finck         {
230c2c66affSColin Finck             if (unicode_aware)
231c2c66affSColin Finck             {
232c2c66affSColin Finck                 p.ulPropTag = PR_SUBJECT_W;
233c2c66affSColin Finck                 p.Value.lpszW = message->lpszSubject;
234c2c66affSColin Finck             }
235c2c66affSColin Finck             else
236c2c66affSColin Finck             {
237c2c66affSColin Finck                 subjectA = convert_from_unicode(message->lpszSubject);
238c2c66affSColin Finck 
239c2c66affSColin Finck                 p.ulPropTag = PR_SUBJECT_A;
240c2c66affSColin Finck                 p.Value.lpszA = subjectA;
241c2c66affSColin Finck             }
242c2c66affSColin Finck 
243c2c66affSColin Finck             IMessage_SetProps(msg, 1, &p, NULL);
244c2c66affSColin Finck         }
245c2c66affSColin Finck 
246c2c66affSColin Finck         /* Set message body */
247c2c66affSColin Finck         if (message->lpszNoteText)
248c2c66affSColin Finck         {
249c2c66affSColin Finck             LPSTREAM stream = NULL;
250c2c66affSColin Finck 
251c2c66affSColin Finck             if (IMessage_OpenProperty(msg, unicode_aware ? PR_BODY_W : PR_BODY_A, &IID_IStream, 0,
252c2c66affSColin Finck                 MAPI_MODIFY | MAPI_CREATE, (LPUNKNOWN*) &stream) == S_OK)
253c2c66affSColin Finck             {
254c2c66affSColin Finck                 if (unicode_aware)
255c2c66affSColin Finck                     IStream_Write(stream, message->lpszNoteText, (lstrlenW(message->lpszNoteText)+1) * sizeof(WCHAR), NULL);
256c2c66affSColin Finck                 else
257c2c66affSColin Finck                 {
258c2c66affSColin Finck                     bodyA = convert_from_unicode(message->lpszNoteText);
259c2c66affSColin Finck                     IStream_Write(stream, bodyA, strlen(bodyA)+1, NULL);
260c2c66affSColin Finck                 }
261c2c66affSColin Finck 
262c2c66affSColin Finck                 IStream_Release(stream);
263c2c66affSColin Finck             }
264c2c66affSColin Finck         }
265c2c66affSColin Finck 
266c2c66affSColin Finck         /* Add message attachments */
267c2c66affSColin Finck         if (message->nFileCount > 0)
268c2c66affSColin Finck         {
269c2c66affSColin Finck             ULONG num_attach = 0;
270c2c66affSColin Finck             unsigned int i;
271c2c66affSColin Finck 
272c2c66affSColin Finck             for (i = 0; i < message->nFileCount; i++)
273c2c66affSColin Finck             {
274c2c66affSColin Finck                 IAttach* attachment = NULL;
275c2c66affSColin Finck                 char *filenameA = NULL;
276c2c66affSColin Finck                 SPropValue prop[4];
277c2c66affSColin Finck                 LPCWSTR filename;
278c2c66affSColin Finck                 HANDLE file;
279c2c66affSColin Finck 
280c2c66affSColin Finck                 if (!message->lpFiles[i].lpszPathName)
281c2c66affSColin Finck                     continue;
282c2c66affSColin Finck 
283c2c66affSColin Finck                 /* Open the attachment for reading */
284c2c66affSColin Finck                 file = CreateFileW(message->lpFiles[i].lpszPathName, GENERIC_READ, FILE_SHARE_READ,
285c2c66affSColin Finck                     NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
286c2c66affSColin Finck 
287c2c66affSColin Finck                 if (file == INVALID_HANDLE_VALUE)
288c2c66affSColin Finck                     continue;
289c2c66affSColin Finck 
290c2c66affSColin Finck                 /* Check if a display filename has been given; if not, get one ourselves from path name */
291c2c66affSColin Finck                 filename = message->lpFiles[i].lpszFileName;
292c2c66affSColin Finck 
293c2c66affSColin Finck                 if (!filename)
294c2c66affSColin Finck                 {
295c2c66affSColin Finck                     int j;
296c2c66affSColin Finck 
297c2c66affSColin Finck                     filename = message->lpFiles[i].lpszPathName;
298c2c66affSColin Finck 
299c2c66affSColin Finck                     for (j = lstrlenW(message->lpFiles[i].lpszPathName)-1; j >= 0; j--)
300c2c66affSColin Finck                     {
301c2c66affSColin Finck                         if (message->lpFiles[i].lpszPathName[i] == '\\' ||
302c2c66affSColin Finck                             message->lpFiles[i].lpszPathName[i] == '/')
303c2c66affSColin Finck                         {
304c2c66affSColin Finck                             filename = &message->lpFiles[i].lpszPathName[i+1];
305c2c66affSColin Finck                             break;
306c2c66affSColin Finck                         }
307c2c66affSColin Finck                     }
308c2c66affSColin Finck                 }
309c2c66affSColin Finck 
310c2c66affSColin Finck                 TRACE("Attachment %u path: '%s'; filename: '%s'\n", i, debugstr_w(message->lpFiles[i].lpszPathName),
311c2c66affSColin Finck                     debugstr_w(filename));
312c2c66affSColin Finck 
313c2c66affSColin Finck                 /* Create the attachment */
314c2c66affSColin Finck                 if (IMessage_CreateAttach(msg, NULL, 0, &num_attach, &attachment) != S_OK)
315c2c66affSColin Finck                 {
316c2c66affSColin Finck                     TRACE("Unable to create attachment\n");
317c2c66affSColin Finck                     CloseHandle(file);
318c2c66affSColin Finck                     continue;
319c2c66affSColin Finck                 }
320c2c66affSColin Finck 
321c2c66affSColin Finck                 /* Set the attachment properties */
322c2c66affSColin Finck                 ZeroMemory(prop, sizeof(prop));
323c2c66affSColin Finck 
324c2c66affSColin Finck                 prop[0].ulPropTag = PR_ATTACH_METHOD;
325c2c66affSColin Finck                 prop[0].Value.ul = ATTACH_BY_VALUE;
326c2c66affSColin Finck 
327c2c66affSColin Finck                 if (unicode_aware)
328c2c66affSColin Finck                 {
329c2c66affSColin Finck                     prop[1].ulPropTag = PR_ATTACH_LONG_FILENAME_W;
330c2c66affSColin Finck                     prop[1].Value.lpszW = (LPWSTR) filename;
331c2c66affSColin Finck                     prop[2].ulPropTag = PR_ATTACH_FILENAME_W;
332c2c66affSColin Finck                     prop[2].Value.lpszW = (LPWSTR) filename;
333c2c66affSColin Finck                 }
334c2c66affSColin Finck                 else
335c2c66affSColin Finck                 {
336c2c66affSColin Finck                     filenameA = convert_from_unicode(filename);
337c2c66affSColin Finck 
338c2c66affSColin Finck                     prop[1].ulPropTag = PR_ATTACH_LONG_FILENAME_A;
339c2c66affSColin Finck                     prop[1].Value.lpszA = (LPSTR) filenameA;
340c2c66affSColin Finck                     prop[2].ulPropTag = PR_ATTACH_FILENAME_A;
341c2c66affSColin Finck                     prop[2].Value.lpszA = (LPSTR) filenameA;
342c2c66affSColin Finck 
343c2c66affSColin Finck                 }
344c2c66affSColin Finck 
345c2c66affSColin Finck                 prop[3].ulPropTag = PR_RENDERING_POSITION;
346c2c66affSColin Finck                 prop[3].Value.l = -1;
347c2c66affSColin Finck 
348c2c66affSColin Finck                 if (IAttach_SetProps(attachment, 4, prop, NULL) == S_OK)
349c2c66affSColin Finck                 {
350c2c66affSColin Finck                     LPSTREAM stream = NULL;
351c2c66affSColin Finck 
352c2c66affSColin Finck                     if (IAttach_OpenProperty(attachment, PR_ATTACH_DATA_BIN, &IID_IStream, 0,
353c2c66affSColin Finck                         MAPI_MODIFY | MAPI_CREATE, (LPUNKNOWN*) &stream) == S_OK)
354c2c66affSColin Finck                     {
355c2c66affSColin Finck                         BYTE data[READ_BUF_SIZE];
356c2c66affSColin Finck                         DWORD size = 0, read, written;
357c2c66affSColin Finck 
358c2c66affSColin Finck                         while (ReadFile(file, data, READ_BUF_SIZE, &read, NULL) && (read != 0))
359c2c66affSColin Finck                         {
360c2c66affSColin Finck                             IStream_Write(stream, data, read, &written);
361c2c66affSColin Finck                             size += read;
362c2c66affSColin Finck                         }
363c2c66affSColin Finck 
364c2c66affSColin Finck                         TRACE("%d bytes written of attachment\n", size);
365c2c66affSColin Finck 
366c2c66affSColin Finck                         IStream_Commit(stream, STGC_DEFAULT);
367c2c66affSColin Finck                         IStream_Release(stream);
368c2c66affSColin Finck 
369c2c66affSColin Finck                         prop[0].ulPropTag = PR_ATTACH_SIZE;
370c2c66affSColin Finck                         prop[0].Value.ul = size;
371c2c66affSColin Finck                         IAttach_SetProps(attachment, 1, prop, NULL);
372c2c66affSColin Finck 
373c2c66affSColin Finck                         IAttach_SaveChanges(attachment, KEEP_OPEN_READONLY);
374c2c66affSColin Finck                         num_attach++;
375c2c66affSColin Finck                     }
376c2c66affSColin Finck                 }
377c2c66affSColin Finck 
378c2c66affSColin Finck                 CloseHandle(file);
379c2c66affSColin Finck                 IAttach_Release(attachment);
380c2c66affSColin Finck 
381c2c66affSColin Finck                 HeapFree(GetProcessHeap(), 0, filenameA);
382c2c66affSColin Finck             }
383c2c66affSColin Finck         }
384c2c66affSColin Finck 
385c2c66affSColin Finck         IMessage_SaveChanges(msg, KEEP_OPEN_READWRITE);
386c2c66affSColin Finck 
387c2c66affSColin Finck         /* Prepare the message form */
388c2c66affSColin Finck 
389c2c66affSColin Finck         if (IMAPISession_PrepareForm(session, NULL, msg, &token) == S_OK)
390c2c66affSColin Finck         {
391c2c66affSColin Finck             ULONG access = 0, status = 0, message_flags = 0, pc = 0;
392c2c66affSColin Finck             ULONG pT[2] = {1, PR_MSG_STATUS};
393c2c66affSColin Finck 
394c2c66affSColin Finck             /* Retrieve message status, flags, access rights and class */
395c2c66affSColin Finck 
396c2c66affSColin Finck             if (IMessage_GetProps(msg, (LPSPropTagArray) pT, 0, &pc, &props) == S_OK)
397c2c66affSColin Finck             {
398c2c66affSColin Finck                 status = props->Value.ul;
399c2c66affSColin Finck                 MAPIFreeBuffer(props);
400c2c66affSColin Finck             }
401c2c66affSColin Finck 
402c2c66affSColin Finck             pT[1] = PR_MESSAGE_FLAGS;
403c2c66affSColin Finck 
404c2c66affSColin Finck             if (IMessage_GetProps(msg, (LPSPropTagArray) pT, 0, &pc, &props) == S_OK)
405c2c66affSColin Finck             {
406c2c66affSColin Finck                 message_flags = props->Value.ul;
407c2c66affSColin Finck                 MAPIFreeBuffer(props);
408c2c66affSColin Finck             }
409c2c66affSColin Finck 
410c2c66affSColin Finck             pT[1] = PR_ACCESS;
411c2c66affSColin Finck 
412c2c66affSColin Finck             if (IMessage_GetProps(msg, (LPSPropTagArray) pT, 0, &pc, &props) == S_OK)
413c2c66affSColin Finck             {
414c2c66affSColin Finck                 access = props->Value.ul;
415c2c66affSColin Finck                 MAPIFreeBuffer(props);
416c2c66affSColin Finck             }
417c2c66affSColin Finck 
418c2c66affSColin Finck             pT[1] = PR_MESSAGE_CLASS_A;
419c2c66affSColin Finck 
420c2c66affSColin Finck             if (IMessage_GetProps(msg, (LPSPropTagArray) pT, 0, &pc, &props) == S_OK)
421c2c66affSColin Finck             {
422c2c66affSColin Finck                 /* Show the message form (edit window) */
423c2c66affSColin Finck 
424c2c66affSColin Finck                 ret = IMAPISession_ShowForm(session, 0, msg_store, draft_folder, NULL,
425c2c66affSColin Finck                                             token, NULL, 0, status, message_flags, access,
426c2c66affSColin Finck                                             props->Value.lpszA);
427c2c66affSColin Finck 
428c2c66affSColin Finck                 switch (ret)
429c2c66affSColin Finck                 {
430c2c66affSColin Finck                     case S_OK:
431c2c66affSColin Finck                         retval = SUCCESS_SUCCESS;
432c2c66affSColin Finck                         break;
433c2c66affSColin Finck 
434c2c66affSColin Finck                     case MAPI_E_USER_CANCEL:
435c2c66affSColin Finck                         retval = MAPI_E_USER_ABORT;
436c2c66affSColin Finck                         break;
437c2c66affSColin Finck 
438c2c66affSColin Finck                     default:
439c2c66affSColin Finck                         TRACE("ShowForm failure: %x\n", ret);
440c2c66affSColin Finck                         break;
441c2c66affSColin Finck                 }
442c2c66affSColin Finck             }
443c2c66affSColin Finck         }
444c2c66affSColin Finck 
445c2c66affSColin Finck         IMessage_Release(msg);
446c2c66affSColin Finck     }
447c2c66affSColin Finck 
448c2c66affSColin Finck     /* Free up the resources we've used */
449c2c66affSColin Finck     IMAPIFolder_Release(draft_folder);
450c2c66affSColin Finck     if (folder) IMAPIFolder_Release(folder);
451c2c66affSColin Finck     IMsgStore_Release(msg_store);
452c2c66affSColin Finck 
453c2c66affSColin Finck     HeapFree(GetProcessHeap(), 0, subjectA);
454c2c66affSColin Finck     HeapFree(GetProcessHeap(), 0, bodyA);
455c2c66affSColin Finck 
456c2c66affSColin Finck logoff: ;
457c2c66affSColin Finck     IMAPISession_Logoff(session, 0, 0, 0);
458c2c66affSColin Finck     IMAPISession_Release(session);
459c2c66affSColin Finck 
460c2c66affSColin Finck cleanup: ;
461c2c66affSColin Finck     MAPIUninitialize();
462c2c66affSColin Finck     return retval;
463c2c66affSColin Finck }
464c2c66affSColin Finck 
465c2c66affSColin Finck /**************************************************************************
466c2c66affSColin Finck  *  MAPISendMail	(MAPI32.211)
467c2c66affSColin Finck  *
468c2c66affSColin Finck  * Send a mail.
469c2c66affSColin Finck  *
470c2c66affSColin Finck  * PARAMS
471c2c66affSColin Finck  *  session  [I] Handle to a MAPI session.
472c2c66affSColin Finck  *  uiparam  [I] Parent window handle.
473c2c66affSColin Finck  *  message  [I] Pointer to a MAPIMessage structure.
474c2c66affSColin Finck  *  flags    [I] Flags.
475c2c66affSColin Finck  *  reserved [I] Reserved, pass 0.
476c2c66affSColin Finck  *
477c2c66affSColin Finck  * RETURNS
478c2c66affSColin Finck  *  Success: SUCCESS_SUCCESS
479c2c66affSColin Finck  *  Failure: MAPI_E_FAILURE
480c2c66affSColin Finck  *
481c2c66affSColin Finck  */
MAPISendMail(LHANDLE session,ULONG_PTR uiparam,lpMapiMessage message,FLAGS flags,ULONG reserved)482c2c66affSColin Finck ULONG WINAPI MAPISendMail( LHANDLE session, ULONG_PTR uiparam,
483c2c66affSColin Finck     lpMapiMessage message, FLAGS flags, ULONG reserved )
484c2c66affSColin Finck {
485c2c66affSColin Finck     WCHAR msg_title[READ_BUF_SIZE], error_msg[READ_BUF_SIZE];
486c2c66affSColin Finck 
487c2c66affSColin Finck     /* Check to see if we have a Simple MAPI provider loaded */
488c2c66affSColin Finck     if (mapiFunctions.MAPISendMail)
489c2c66affSColin Finck         return mapiFunctions.MAPISendMail(session, uiparam, message, flags, reserved);
490c2c66affSColin Finck 
491c2c66affSColin Finck     /* Check if we have an Extended MAPI provider - if so, use our wrapper */
492c2c66affSColin Finck     if (MAPIInitialize(NULL) == S_OK)
493c2c66affSColin Finck     {
494c2c66affSColin Finck         MapiMessageW messageW;
495c2c66affSColin Finck         ULONG ret;
496c2c66affSColin Finck 
497c2c66affSColin Finck         ZeroMemory(&messageW, sizeof(MapiMessageW));
498c2c66affSColin Finck 
499c2c66affSColin Finck         /* Convert the entries we need to Unicode */
500c2c66affSColin Finck         messageW.lpszSubject = convert_to_unicode(message->lpszSubject);
501c2c66affSColin Finck         messageW.lpszNoteText = convert_to_unicode(message->lpszNoteText);
502c2c66affSColin Finck         messageW.nFileCount = message->nFileCount;
503c2c66affSColin Finck 
504c2c66affSColin Finck         if (message->nFileCount && message->lpFiles)
505c2c66affSColin Finck         {
506c2c66affSColin Finck             lpMapiFileDescW filesW;
507c2c66affSColin Finck             unsigned int i;
508c2c66affSColin Finck 
509c2c66affSColin Finck             filesW = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(MapiFileDescW) * message->nFileCount);
510c2c66affSColin Finck 
511c2c66affSColin Finck             for (i = 0; i < message->nFileCount; i++)
512c2c66affSColin Finck             {
513c2c66affSColin Finck                 filesW[i].lpszPathName = convert_to_unicode(message->lpFiles[i].lpszPathName);
514c2c66affSColin Finck                 filesW[i].lpszFileName = convert_to_unicode(message->lpFiles[i].lpszFileName);
515c2c66affSColin Finck             }
516c2c66affSColin Finck 
517c2c66affSColin Finck             messageW.lpFiles = filesW;
518c2c66affSColin Finck         }
519c2c66affSColin Finck 
520c2c66affSColin Finck         ret = sendmail_extended_mapi(session, uiparam, &messageW, flags);
521c2c66affSColin Finck 
522c2c66affSColin Finck         /* Now free everything we allocated */
523c2c66affSColin Finck         if (message->nFileCount && message->lpFiles)
524c2c66affSColin Finck         {
525c2c66affSColin Finck             unsigned int i;
526c2c66affSColin Finck 
527c2c66affSColin Finck             for (i = 0; i < message->nFileCount; i++)
528c2c66affSColin Finck             {
529c2c66affSColin Finck                 HeapFree(GetProcessHeap(), 0, messageW.lpFiles[i].lpszPathName);
530c2c66affSColin Finck                 HeapFree(GetProcessHeap(), 0, messageW.lpFiles[i].lpszFileName);
531c2c66affSColin Finck             }
532c2c66affSColin Finck 
533c2c66affSColin Finck             HeapFree(GetProcessHeap(), 0, messageW.lpFiles);
534c2c66affSColin Finck         }
535c2c66affSColin Finck 
536c2c66affSColin Finck         HeapFree(GetProcessHeap(), 0, messageW.lpszSubject);
537c2c66affSColin Finck         HeapFree(GetProcessHeap(), 0, messageW.lpszNoteText);
538c2c66affSColin Finck 
539c2c66affSColin Finck         return ret;
540c2c66affSColin Finck     }
541c2c66affSColin Finck 
542c2c66affSColin Finck     /* Display an error message since we apparently have no mail clients */
543*dd78a4bfSAmine Khaldi     LoadStringW(hInstMAPI32, IDS_NO_MAPI_CLIENT, error_msg, ARRAY_SIZE(error_msg));
544*dd78a4bfSAmine Khaldi     LoadStringW(hInstMAPI32, IDS_SEND_MAIL, msg_title, ARRAY_SIZE(msg_title));
545c2c66affSColin Finck 
546c2c66affSColin Finck     MessageBoxW((HWND) uiparam, error_msg, msg_title, MB_ICONEXCLAMATION);
547c2c66affSColin Finck 
548c2c66affSColin Finck     return MAPI_E_NOT_SUPPORTED;
549c2c66affSColin Finck }
550c2c66affSColin Finck 
convert_recipient_from_unicode(lpMapiRecipDescW recipW,lpMapiRecipDesc dest)551c2c66affSColin Finck static lpMapiRecipDesc convert_recipient_from_unicode(lpMapiRecipDescW recipW, lpMapiRecipDesc dest)
552c2c66affSColin Finck {
553c2c66affSColin Finck     lpMapiRecipDesc ret;
554c2c66affSColin Finck 
555c2c66affSColin Finck     if (!recipW)
556c2c66affSColin Finck         return NULL;
557c2c66affSColin Finck 
558c2c66affSColin Finck     if (dest)
559c2c66affSColin Finck         ret = dest;
560c2c66affSColin Finck     else
561c2c66affSColin Finck         ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(MapiRecipDesc));
562c2c66affSColin Finck 
563c2c66affSColin Finck     ret->ulRecipClass = recipW->ulRecipClass;
564c2c66affSColin Finck     ret->lpszName = convert_from_unicode(recipW->lpszName);
565c2c66affSColin Finck     ret->lpszAddress = convert_from_unicode(recipW->lpszAddress);
566c2c66affSColin Finck     ret->ulEIDSize = recipW->ulEIDSize;
567c2c66affSColin Finck     ret->lpEntryID = recipW->lpEntryID;
568c2c66affSColin Finck 
569c2c66affSColin Finck     return ret;
570c2c66affSColin Finck }
571c2c66affSColin Finck 
572c2c66affSColin Finck /**************************************************************************
573c2c66affSColin Finck  *  MAPISendMailW	(MAPI32.256)
574c2c66affSColin Finck  *
575c2c66affSColin Finck  * Send a mail.
576c2c66affSColin Finck  *
577c2c66affSColin Finck  * PARAMS
578c2c66affSColin Finck  *  session  [I] Handle to a MAPI session.
579c2c66affSColin Finck  *  uiparam  [I] Parent window handle.
580c2c66affSColin Finck  *  message  [I] Pointer to a MAPIMessageW structure.
581c2c66affSColin Finck  *  flags    [I] Flags.
582c2c66affSColin Finck  *  reserved [I] Reserved, pass 0.
583c2c66affSColin Finck  *
584c2c66affSColin Finck  * RETURNS
585c2c66affSColin Finck  *  Success: SUCCESS_SUCCESS
586c2c66affSColin Finck  *  Failure: MAPI_E_FAILURE
587c2c66affSColin Finck  *
588c2c66affSColin Finck  */
MAPISendMailW(LHANDLE session,ULONG_PTR uiparam,lpMapiMessageW message,FLAGS flags,ULONG reserved)589c2c66affSColin Finck ULONG WINAPI MAPISendMailW(LHANDLE session, ULONG_PTR uiparam,
590c2c66affSColin Finck     lpMapiMessageW message, FLAGS flags, ULONG reserved)
591c2c66affSColin Finck {
592c2c66affSColin Finck     WCHAR msg_title[READ_BUF_SIZE], error_msg[READ_BUF_SIZE];
593c2c66affSColin Finck 
594c2c66affSColin Finck     /* Check to see if we have a Simple MAPI provider loaded */
595c2c66affSColin Finck     if (mapiFunctions.MAPISendMailW)
596c2c66affSColin Finck         return mapiFunctions.MAPISendMailW(session, uiparam, message, flags, reserved);
597c2c66affSColin Finck 
598c2c66affSColin Finck     /* Check if we have an Extended MAPI provider - if so, use our wrapper */
599c2c66affSColin Finck     if (MAPIInitialize(NULL) == S_OK)
600c2c66affSColin Finck         return sendmail_extended_mapi(session, uiparam, message, flags);
601c2c66affSColin Finck 
602c2c66affSColin Finck     if (mapiFunctions.MAPISendMail)
603c2c66affSColin Finck     {
604c2c66affSColin Finck         MapiMessage messageA;
605c2c66affSColin Finck         ULONG ret;
606c2c66affSColin Finck 
607c2c66affSColin Finck         if (flags & MAPI_FORCE_UNICODE)
608c2c66affSColin Finck             return MAPI_E_UNICODE_NOT_SUPPORTED;
609c2c66affSColin Finck 
610c2c66affSColin Finck         /* Convert to ANSI and send to MAPISendMail */
611c2c66affSColin Finck         ZeroMemory(&messageA, sizeof(MapiMessage));
612c2c66affSColin Finck 
613c2c66affSColin Finck         messageA.lpszSubject = convert_from_unicode(message->lpszSubject);
614c2c66affSColin Finck         messageA.lpszNoteText = convert_from_unicode(message->lpszNoteText);
615c2c66affSColin Finck         messageA.lpszMessageType = convert_from_unicode(message->lpszMessageType);
616c2c66affSColin Finck         messageA.lpszDateReceived = convert_from_unicode(message->lpszDateReceived);
617c2c66affSColin Finck         messageA.lpszConversationID = convert_from_unicode(message->lpszConversationID);
618c2c66affSColin Finck         messageA.flFlags = message->flFlags;
619c2c66affSColin Finck         messageA.lpOriginator = convert_recipient_from_unicode(message->lpOriginator, NULL);
620c2c66affSColin Finck         messageA.nRecipCount = message->nRecipCount;
621c2c66affSColin Finck         messageA.nFileCount = message->nFileCount;
622c2c66affSColin Finck 
623c2c66affSColin Finck         if (message->nRecipCount && message->lpRecips)
624c2c66affSColin Finck         {
625c2c66affSColin Finck             lpMapiRecipDesc recipsA;
626c2c66affSColin Finck             unsigned int i;
627c2c66affSColin Finck 
628c2c66affSColin Finck             recipsA = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(MapiRecipDesc) * message->nRecipCount);
629c2c66affSColin Finck 
630c2c66affSColin Finck             for (i = 0; i < message->nRecipCount; i++)
631c2c66affSColin Finck             {
632c2c66affSColin Finck                 convert_recipient_from_unicode(&message->lpRecips[i], &recipsA[i]);
633c2c66affSColin Finck             }
634c2c66affSColin Finck 
635c2c66affSColin Finck             messageA.lpRecips = recipsA;
636c2c66affSColin Finck         }
637c2c66affSColin Finck 
638c2c66affSColin Finck         if (message->nFileCount && message->lpFiles)
639c2c66affSColin Finck         {
640c2c66affSColin Finck             lpMapiFileDesc filesA;
641c2c66affSColin Finck             unsigned int i;
642c2c66affSColin Finck 
643c2c66affSColin Finck             filesA = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(MapiFileDesc) * message->nFileCount);
644c2c66affSColin Finck 
645c2c66affSColin Finck             for (i = 0; i < message->nFileCount; i++)
646c2c66affSColin Finck             {
647c2c66affSColin Finck                 filesA[i].flFlags = message->lpFiles[i].flFlags;
648c2c66affSColin Finck                 filesA[i].nPosition = message->lpFiles[i].nPosition;
649c2c66affSColin Finck                 filesA[i].lpszPathName = convert_from_unicode(message->lpFiles[i].lpszPathName);
650c2c66affSColin Finck                 filesA[i].lpszFileName = convert_from_unicode(message->lpFiles[i].lpszFileName);
651c2c66affSColin Finck                 filesA[i].lpFileType = message->lpFiles[i].lpFileType;
652c2c66affSColin Finck             }
653c2c66affSColin Finck 
654c2c66affSColin Finck             messageA.lpFiles = filesA;
655c2c66affSColin Finck         }
656c2c66affSColin Finck 
657c2c66affSColin Finck         ret = mapiFunctions.MAPISendMail(session, uiparam, &messageA, flags, reserved);
658c2c66affSColin Finck 
659c2c66affSColin Finck         /* Now free everything we allocated */
660c2c66affSColin Finck         if (message->lpOriginator)
661c2c66affSColin Finck         {
662c2c66affSColin Finck             HeapFree(GetProcessHeap(), 0, messageA.lpOriginator->lpszName);
663c2c66affSColin Finck             HeapFree(GetProcessHeap(), 0, messageA.lpOriginator->lpszAddress);
664c2c66affSColin Finck             HeapFree(GetProcessHeap(), 0, messageA.lpOriginator);
665c2c66affSColin Finck         }
666c2c66affSColin Finck 
667c2c66affSColin Finck         if (message->nRecipCount && message->lpRecips)
668c2c66affSColin Finck         {
669c2c66affSColin Finck             unsigned int i;
670c2c66affSColin Finck 
671c2c66affSColin Finck             for (i = 0; i < message->nRecipCount; i++)
672c2c66affSColin Finck             {
673c2c66affSColin Finck                 HeapFree(GetProcessHeap(), 0, messageA.lpRecips[i].lpszName);
674c2c66affSColin Finck                 HeapFree(GetProcessHeap(), 0, messageA.lpRecips[i].lpszAddress);
675c2c66affSColin Finck             }
676c2c66affSColin Finck 
677c2c66affSColin Finck             HeapFree(GetProcessHeap(), 0, messageA.lpRecips);
678c2c66affSColin Finck         }
679c2c66affSColin Finck 
680c2c66affSColin Finck         if (message->nFileCount && message->lpFiles)
681c2c66affSColin Finck         {
682c2c66affSColin Finck             unsigned int i;
683c2c66affSColin Finck 
684c2c66affSColin Finck             for (i = 0; i < message->nFileCount; i++)
685c2c66affSColin Finck             {
686c2c66affSColin Finck                 HeapFree(GetProcessHeap(), 0, messageA.lpFiles[i].lpszPathName);
687c2c66affSColin Finck                 HeapFree(GetProcessHeap(), 0, messageA.lpFiles[i].lpszFileName);
688c2c66affSColin Finck             }
689c2c66affSColin Finck 
690c2c66affSColin Finck             HeapFree(GetProcessHeap(), 0, messageA.lpFiles);
691c2c66affSColin Finck         }
692c2c66affSColin Finck 
693c2c66affSColin Finck         HeapFree(GetProcessHeap(), 0, messageA.lpszSubject);
694c2c66affSColin Finck         HeapFree(GetProcessHeap(), 0, messageA.lpszNoteText);
695c2c66affSColin Finck         HeapFree(GetProcessHeap(), 0, messageA.lpszDateReceived);
696c2c66affSColin Finck         HeapFree(GetProcessHeap(), 0, messageA.lpszConversationID);
697c2c66affSColin Finck 
698c2c66affSColin Finck         return ret;
699c2c66affSColin Finck     }
700c2c66affSColin Finck 
701c2c66affSColin Finck     /* Display an error message since we apparently have no mail clients */
702*dd78a4bfSAmine Khaldi     LoadStringW(hInstMAPI32, IDS_NO_MAPI_CLIENT, error_msg, ARRAY_SIZE(error_msg));
703*dd78a4bfSAmine Khaldi     LoadStringW(hInstMAPI32, IDS_SEND_MAIL, msg_title, ARRAY_SIZE(msg_title));
704c2c66affSColin Finck 
705c2c66affSColin Finck     MessageBoxW((HWND) uiparam, error_msg, msg_title, MB_ICONEXCLAMATION);
706c2c66affSColin Finck 
707c2c66affSColin Finck     return MAPI_E_NOT_SUPPORTED;
708c2c66affSColin Finck }
709c2c66affSColin Finck 
MAPISendDocuments(ULONG_PTR uiparam,LPSTR delim,LPSTR paths,LPSTR filenames,ULONG reserved)710c2c66affSColin Finck ULONG WINAPI MAPISendDocuments(ULONG_PTR uiparam, LPSTR delim, LPSTR paths,
711c2c66affSColin Finck     LPSTR filenames, ULONG reserved)
712c2c66affSColin Finck {
713c2c66affSColin Finck     if (mapiFunctions.MAPISendDocuments)
714c2c66affSColin Finck         return mapiFunctions.MAPISendDocuments(uiparam, delim, paths, filenames, reserved);
715c2c66affSColin Finck 
716c2c66affSColin Finck     return MAPI_E_NOT_SUPPORTED;
717c2c66affSColin Finck }
718