mapi32: Convert sendmail_extended_mapi to Unicode.
[wine/multimedia.git] / dlls / mapi32 / sendmail.c
blob8242e1179209b1788fbdc75afd8d85eb05e36422
1 /*
2 * MAPISendMail implementation
4 * Copyright 2005 Hans Leidekker
5 * Copyright 2009 Owen Rudge for CodeWeavers
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.
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.
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
22 #include "config.h"
23 #include "wine/port.h"
25 #include <stdio.h>
26 #include <stdarg.h>
28 #define COBJMACROS
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"
47 WINE_DEFAULT_DEBUG_CHANNEL(mapi);
49 #define READ_BUF_SIZE 4096
51 #define STORE_UNICODE_OK 0x00040000
53 static LPSTR convert_from_unicode(LPCWSTR wstr)
55 LPSTR str;
56 DWORD len;
58 if (!wstr)
59 return NULL;
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);
65 return str;
69 Internal function to send a message via Extended MAPI. Wrapper around the Simple
70 MAPI function MAPISendMail.
72 static ULONG sendmail_extended_mapi(LHANDLE mapi_session, ULONG_PTR uiparam, lpMapiMessageW message,
73 FLAGS flags)
75 ULONG tags[] = {1, 0};
76 char *subjectA = NULL, *bodyA = NULL;
77 ULONG retval = MAPI_E_FAILURE;
78 IMAPISession *session = NULL;
79 BOOL unicode_aware = FALSE;
80 IMAPITable* msg_table;
81 LPSRowSet rows = NULL;
82 IMsgStore* msg_store;
83 IMAPIFolder* folder = NULL, *draft_folder = NULL;
84 LPENTRYID entry_id;
85 LPSPropValue props;
86 ULONG entry_len;
87 DWORD obj_type;
88 IMessage* msg;
89 ULONG values;
90 HRESULT ret;
92 TRACE("Using Extended MAPI wrapper for MAPISendMail\n");
94 /* Attempt to log on via Extended MAPI */
96 ret = MAPILogonEx(0, NULL, NULL, MAPI_EXTENDED | MAPI_USE_DEFAULT | MAPI_NEW_SESSION, &session);
97 TRACE("MAPILogonEx: %x\n", ret);
99 if (ret != S_OK)
101 retval = MAPI_E_LOGIN_FAILURE;
102 goto cleanup;
105 /* Open the default message store */
107 if (IMAPISession_GetMsgStoresTable(session, 0, &msg_table) == S_OK)
109 /* We want the default store */
110 SizedSPropTagArray(2, columns) = {2, {PR_ENTRYID, PR_DEFAULT_STORE}};
112 /* Set the columns we want */
113 if (IMAPITable_SetColumns(msg_table, (LPSPropTagArray) &columns, 0) == S_OK)
115 while (1)
117 if (IMAPITable_QueryRows(msg_table, 1, 0, &rows) != S_OK)
119 MAPIFreeBuffer(rows);
120 rows = NULL;
122 else if (rows->cRows != 1)
124 FreeProws(rows);
125 rows = NULL;
127 else
129 /* If it's not the default store, try the next row */
130 if (!rows->aRow[0].lpProps[1].Value.b)
132 FreeProws(rows);
133 continue;
137 break;
141 IMAPITable_Release(msg_table);
144 /* Did we manage to get the right store? */
145 if (!rows)
146 goto logoff;
148 /* Open the message store */
149 IMAPISession_OpenMsgStore(session, 0, rows->aRow[0].lpProps[0].Value.bin.cb,
150 (ENTRYID *) rows->aRow[0].lpProps[0].Value.bin.lpb, NULL,
151 MDB_NO_DIALOG | MAPI_BEST_ACCESS, &msg_store);
153 /* We don't need this any more */
154 FreeProws(rows);
156 /* Check if the message store supports Unicode */
157 tags[1] = PR_STORE_SUPPORT_MASK;
158 ret = IMsgStore_GetProps(msg_store, (LPSPropTagArray) tags, 0, &values, &props);
160 if ((ret == S_OK) && (props[0].Value.l & STORE_UNICODE_OK))
161 unicode_aware = TRUE;
162 else
164 /* Don't convert to ANSI */
165 if (flags & MAPI_FORCE_UNICODE)
167 WARN("No Unicode-capable mail client, and MAPI_FORCE_UNICODE is specified. MAPISendMail failed.\n");
168 retval = MAPI_E_UNICODE_NOT_SUPPORTED;
169 IMsgStore_Release(msg_store);
170 goto logoff;
174 /* First open the inbox, from which the drafts folder can be opened */
175 if (IMsgStore_GetReceiveFolder(msg_store, NULL, 0, &entry_len, &entry_id, NULL) == S_OK)
177 IMsgStore_OpenEntry(msg_store, entry_len, entry_id, NULL, 0, &obj_type, (LPUNKNOWN*) &folder);
178 MAPIFreeBuffer(entry_id);
181 tags[1] = PR_IPM_DRAFTS_ENTRYID;
183 /* Open the drafts folder, or failing that, try asking the message store for the outbox */
184 if ((folder == NULL) || ((ret = IMAPIFolder_GetProps(folder, (LPSPropTagArray) tags, 0, &values, &props)) != S_OK))
186 TRACE("Unable to open Drafts folder; opening Outbox instead\n");
187 tags[1] = PR_IPM_OUTBOX_ENTRYID;
188 ret = IMsgStore_GetProps(msg_store, (LPSPropTagArray) tags, 0, &values, &props);
191 if (ret != S_OK)
192 goto logoff;
194 IMsgStore_OpenEntry(msg_store, props[0].Value.bin.cb, (LPENTRYID) props[0].Value.bin.lpb,
195 NULL, MAPI_MODIFY, &obj_type, (LPUNKNOWN *) &draft_folder);
197 /* Create a new message */
198 if (IMAPIFolder_CreateMessage(draft_folder, NULL, 0, &msg) == S_OK)
200 ULONG token;
201 SPropValue p;
203 /* Define message properties */
204 p.ulPropTag = PR_MESSAGE_FLAGS;
205 p.Value.l = MSGFLAG_FROMME | MSGFLAG_UNSENT;
207 IMessage_SetProps(msg, 1, &p, NULL);
209 p.ulPropTag = PR_SENTMAIL_ENTRYID;
210 p.Value.bin.cb = props[0].Value.bin.cb;
211 p.Value.bin.lpb = props[0].Value.bin.lpb;
212 IMessage_SetProps(msg, 1,&p, NULL);
214 /* Set message subject */
215 if (message->lpszSubject)
217 if (unicode_aware)
219 p.ulPropTag = PR_SUBJECT_W;
220 p.Value.lpszW = message->lpszSubject;
222 else
224 subjectA = convert_from_unicode(message->lpszSubject);
226 p.ulPropTag = PR_SUBJECT_A;
227 p.Value.lpszA = subjectA;
230 IMessage_SetProps(msg, 1, &p, NULL);
233 /* Set message body */
234 if (message->lpszNoteText)
236 LPSTREAM stream = NULL;
238 if (IMessage_OpenProperty(msg, unicode_aware ? PR_BODY_W : PR_BODY_A, &IID_IStream, 0,
239 MAPI_MODIFY | MAPI_CREATE, (LPUNKNOWN*) &stream) == S_OK)
241 if (unicode_aware)
242 IStream_Write(stream, message->lpszNoteText, (lstrlenW(message->lpszNoteText)+1) * sizeof(WCHAR), NULL);
243 else
245 bodyA = convert_from_unicode(message->lpszNoteText);
246 IStream_Write(stream, bodyA, strlen(bodyA)+1, NULL);
249 IStream_Release(stream);
253 /* Add message attachments */
254 if (message->nFileCount > 0)
256 ULONG num_attach = 0;
257 int i, j;
259 for (i = 0; i < message->nFileCount; i++)
261 IAttach* attachment = NULL;
262 char *filenameA = NULL;
263 SPropValue prop[4];
264 LPCWSTR filename;
265 HANDLE file;
267 if (!message->lpFiles[i].lpszPathName)
268 continue;
270 /* Open the attachment for reading */
271 file = CreateFileW(message->lpFiles[i].lpszPathName, GENERIC_READ, FILE_SHARE_READ,
272 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
274 if (file == INVALID_HANDLE_VALUE)
275 continue;
277 /* Check if a display filename has been given; if not, get one ourselves from path name */
278 filename = message->lpFiles[i].lpszFileName;
280 if (!filename)
282 filename = message->lpFiles[i].lpszPathName;
284 for (j = lstrlenW(message->lpFiles[i].lpszPathName)-1; j >= 0; j--)
286 if (message->lpFiles[i].lpszPathName[i] == '\\' ||
287 message->lpFiles[i].lpszPathName[i] == '/')
289 filename = &message->lpFiles[i].lpszPathName[i+1];
290 break;
295 TRACE("Attachment %d path: '%s'; filename: '%s'\n", i, debugstr_w(message->lpFiles[i].lpszPathName),
296 debugstr_w(filename));
298 /* Create the attachment */
299 if (IMessage_CreateAttach(msg, NULL, 0, &num_attach, &attachment) != S_OK)
301 TRACE("Unable to create attachment\n");
302 CloseHandle(file);
303 continue;
306 /* Set the attachment properties */
307 ZeroMemory(prop, sizeof(prop));
309 prop[0].ulPropTag = PR_ATTACH_METHOD;
310 prop[0].Value.ul = ATTACH_BY_VALUE;
312 if (unicode_aware)
314 prop[1].ulPropTag = PR_ATTACH_LONG_FILENAME_W;
315 prop[1].Value.lpszW = (LPWSTR) filename;
316 prop[2].ulPropTag = PR_ATTACH_FILENAME_W;
317 prop[2].Value.lpszW = (LPWSTR) filename;
319 else
321 filenameA = convert_from_unicode(filename);
323 prop[1].ulPropTag = PR_ATTACH_LONG_FILENAME_A;
324 prop[1].Value.lpszA = (LPSTR) filenameA;
325 prop[2].ulPropTag = PR_ATTACH_FILENAME_A;
326 prop[2].Value.lpszA = (LPSTR) filenameA;
330 prop[3].ulPropTag = PR_RENDERING_POSITION;
331 prop[3].Value.l = -1;
333 if (IAttach_SetProps(attachment, 4, prop, NULL) == S_OK)
335 LPSTREAM stream = NULL;
337 if (IAttach_OpenProperty(attachment, PR_ATTACH_DATA_BIN, &IID_IStream, 0,
338 MAPI_MODIFY | MAPI_CREATE, (LPUNKNOWN*) &stream) == S_OK)
340 BYTE data[READ_BUF_SIZE];
341 DWORD size = 0, read, written;
343 while (ReadFile(file, data, READ_BUF_SIZE, &read, NULL) && (read != 0))
345 IStream_Write(stream, data, read, &written);
346 size += read;
349 TRACE("%d bytes written of attachment\n", size);
351 IStream_Commit(stream, STGC_DEFAULT);
352 IStream_Release(stream);
354 prop[0].ulPropTag = PR_ATTACH_SIZE;
355 prop[0].Value.ul = size;
356 IAttach_SetProps(attachment, 1, prop, NULL);
358 IAttach_SaveChanges(attachment, KEEP_OPEN_READONLY);
359 num_attach++;
363 CloseHandle(file);
364 IAttach_Release(attachment);
366 HeapFree(GetProcessHeap(), 0, filenameA);
370 IMessage_SaveChanges(msg, KEEP_OPEN_READWRITE);
372 /* Prepare the message form */
374 if (IMAPISession_PrepareForm(session, NULL, msg, &token) == S_OK)
376 ULONG access = 0, status = 0, message_flags = 0, pc = 0;
377 ULONG pT[2] = {1, PR_MSG_STATUS};
379 /* Retrieve message status, flags, access rights and class */
381 if (IMessage_GetProps(msg, (LPSPropTagArray) pT, 0, &pc, &props) == S_OK)
383 status = props->Value.ul;
384 MAPIFreeBuffer(props);
387 pT[1] = PR_MESSAGE_FLAGS;
389 if (IMessage_GetProps(msg, (LPSPropTagArray) pT, 0, &pc, &props) == S_OK)
391 message_flags = props->Value.ul;
392 MAPIFreeBuffer(props);
395 pT[1] = PR_ACCESS;
397 if (IMessage_GetProps(msg, (LPSPropTagArray) pT, 0, &pc, &props) == S_OK)
399 access = props->Value.ul;
400 MAPIFreeBuffer(props);
403 pT[1] = PR_MESSAGE_CLASS_A;
405 if (IMessage_GetProps(msg, (LPSPropTagArray) pT, 0, &pc, &props) == S_OK)
407 /* Show the message form (edit window) */
409 ret = IMAPISession_ShowForm(session, 0, msg_store, draft_folder, NULL,
410 token, NULL, 0, status, message_flags, access,
411 props->Value.lpszA);
413 switch (ret)
415 case S_OK:
416 retval = SUCCESS_SUCCESS;
417 break;
419 case MAPI_E_USER_CANCEL:
420 retval = MAPI_E_USER_ABORT;
421 break;
423 default:
424 TRACE("ShowForm failure: %x\n", ret);
425 break;
430 IMessage_Release(msg);
433 /* Free up the resources we've used */
434 IMAPIFolder_Release(draft_folder);
435 if (folder) IMAPIFolder_Release(folder);
436 IMsgStore_Release(msg_store);
438 HeapFree(GetProcessHeap(), 0, subjectA);
439 HeapFree(GetProcessHeap(), 0, bodyA);
441 logoff: ;
442 IMAPISession_Logoff(session, 0, 0, 0);
443 IMAPISession_Release(session);
445 cleanup: ;
446 MAPIUninitialize();
447 return retval;
450 /**************************************************************************
451 * MAPISendMail (MAPI32.211)
453 * Send a mail.
455 * PARAMS
456 * session [I] Handle to a MAPI session.
457 * uiparam [I] Parent window handle.
458 * message [I] Pointer to a MAPIMessage structure.
459 * flags [I] Flags.
460 * reserved [I] Reserved, pass 0.
462 * RETURNS
463 * Success: SUCCESS_SUCCESS
464 * Failure: MAPI_E_FAILURE
467 ULONG WINAPI MAPISendMail( LHANDLE session, ULONG_PTR uiparam,
468 lpMapiMessage message, FLAGS flags, ULONG reserved )
470 WCHAR msg_title[READ_BUF_SIZE], error_msg[READ_BUF_SIZE];
472 /* Check to see if we have a Simple MAPI provider loaded */
473 if (mapiFunctions.MAPISendMail)
474 return mapiFunctions.MAPISendMail(session, uiparam, message, flags, reserved);
476 /* Check if we have an Extended MAPI provider - if so, use our wrapper */
477 #if 0
478 if (MAPIInitialize(NULL) == S_OK)
479 return sendmail_extended_mapi(session, uiparam, message, flags);
480 #endif
482 /* Display an error message since we apparently have no mail clients */
483 LoadStringW(hInstMAPI32, IDS_NO_MAPI_CLIENT, error_msg, sizeof(error_msg) / sizeof(WCHAR));
484 LoadStringW(hInstMAPI32, IDS_SEND_MAIL, msg_title, sizeof(msg_title) / sizeof(WCHAR));
486 MessageBoxW((HWND) uiparam, error_msg, msg_title, MB_ICONEXCLAMATION);
488 return MAPI_E_NOT_SUPPORTED;
491 /**************************************************************************
492 * MAPISendMailW (MAPI32.256)
494 * Send a mail.
496 * PARAMS
497 * session [I] Handle to a MAPI session.
498 * uiparam [I] Parent window handle.
499 * message [I] Pointer to a MAPIMessageW structure.
500 * flags [I] Flags.
501 * reserved [I] Reserved, pass 0.
503 * RETURNS
504 * Success: SUCCESS_SUCCESS
505 * Failure: MAPI_E_FAILURE
508 ULONG WINAPI MAPISendMailW(LHANDLE session, ULONG_PTR uiparam,
509 lpMapiMessageW message, FLAGS flags, ULONG reserved)
511 WCHAR msg_title[READ_BUF_SIZE], error_msg[READ_BUF_SIZE];
513 /* Check to see if we have a Simple MAPI provider loaded */
514 if (mapiFunctions.MAPISendMailW)
515 return mapiFunctions.MAPISendMailW(session, uiparam, message, flags, reserved);
517 /* Check if we have an Extended MAPI provider - if so, use our wrapper */
518 if (MAPIInitialize(NULL) == S_OK)
519 return sendmail_extended_mapi(session, uiparam, message, flags);
521 /* Display an error message since we apparently have no mail clients */
522 LoadStringW(hInstMAPI32, IDS_NO_MAPI_CLIENT, error_msg, sizeof(error_msg) / sizeof(WCHAR));
523 LoadStringW(hInstMAPI32, IDS_SEND_MAIL, msg_title, sizeof(msg_title) / sizeof(WCHAR));
525 MessageBoxW((HWND) uiparam, error_msg, msg_title, MB_ICONEXCLAMATION);
527 return MAPI_E_NOT_SUPPORTED;
530 ULONG WINAPI MAPISendDocuments(ULONG_PTR uiparam, LPSTR delim, LPSTR paths,
531 LPSTR filenames, ULONG reserved)
533 if (mapiFunctions.MAPISendDocuments)
534 return mapiFunctions.MAPISendDocuments(uiparam, delim, paths, filenames, reserved);
536 return MAPI_E_NOT_SUPPORTED;