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