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
23 #include "wine/port.h"
42 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(mapi);
47 #define READ_BUF_SIZE 4096
50 Internal function to send a message via Extended MAPI. Wrapper around the Simple
51 MAPI function MAPISendMail.
53 static ULONG sendmail_extended_mapi(LHANDLE mapi_session, ULONG_PTR uiparam, lpMapiMessage message,
54 FLAGS flags, ULONG reserved)
56 ULONG tags[] = {1, PR_IPM_DRAFTS_ENTRYID};
57 ULONG retval = MAPI_E_FAILURE;
58 IMAPISession *session = NULL;
59 IMAPITable* msg_table;
60 LPSRowSet rows = NULL;
62 IMAPIFolder* folder = NULL, *draft_folder = NULL;
71 TRACE("Using Extended MAPI wrapper for MAPISendMail\n");
73 /* Attempt to log on via Extended MAPI */
75 ret = MAPILogonEx(0, NULL, NULL, MAPI_EXTENDED | MAPI_USE_DEFAULT | MAPI_NEW_SESSION, &session);
76 TRACE("MAPILogonEx: %x\n", ret);
80 retval = MAPI_E_LOGIN_FAILURE;
84 /* Open the default message store */
86 if (IMAPISession_GetMsgStoresTable(session, 0, &msg_table) == S_OK)
88 /* We want the default store */
89 SizedSPropTagArray(2, columns) = {2, {PR_ENTRYID, PR_DEFAULT_STORE}};
91 /* Set the columns we want */
92 if (IMAPITable_SetColumns(msg_table, (LPSPropTagArray) &columns, 0) == S_OK)
96 if (IMAPITable_QueryRows(msg_table, 1, 0, &rows) != S_OK)
101 else if (rows->cRows != 1)
108 /* If it's not the default store, try the next row */
109 if (!rows->aRow[0].lpProps[1].Value.b)
120 IMAPITable_Release(msg_table);
123 /* Did we manage to get the right store? */
127 /* Open the message store */
128 IMAPISession_OpenMsgStore(session, 0, rows->aRow[0].lpProps[0].Value.bin.cb,
129 (ENTRYID *) rows->aRow[0].lpProps[0].Value.bin.lpb, NULL,
130 MDB_NO_DIALOG | MAPI_BEST_ACCESS, &msg_store);
132 /* We don't need this any more */
135 /* First open the inbox, from which the drafts folder can be opened */
136 if (IMsgStore_GetReceiveFolder(msg_store, NULL, 0, &entry_len, &entry_id, NULL) == S_OK)
138 IMsgStore_OpenEntry(msg_store, entry_len, entry_id, NULL, 0, &obj_type, (LPUNKNOWN*) &folder);
139 MAPIFreeBuffer(entry_id);
142 /* Open the drafts folder, or failing that, try asking the message store for the outbox */
143 if ((folder == NULL) || ((ret = IMAPIFolder_GetProps(folder, (LPSPropTagArray) tags, 0, &values, &props)) != S_OK))
145 TRACE("Unable to open Drafts folder; opening Outbox instead\n");
146 tags[1] = PR_IPM_OUTBOX_ENTRYID;
147 ret = IMsgStore_GetProps(msg_store, (LPSPropTagArray) tags, 0, &values, &props);
153 IMsgStore_OpenEntry(msg_store, props[0].Value.bin.cb, (LPENTRYID) props[0].Value.bin.lpb,
154 NULL, MAPI_MODIFY, &obj_type, (LPUNKNOWN *) &draft_folder);
156 /* Create a new message */
157 if (IMAPIFolder_CreateMessage(draft_folder, NULL, 0, &msg) == S_OK)
162 /* Define message properties */
163 p.ulPropTag = PR_MESSAGE_FLAGS;
164 p.Value.l = MSGFLAG_FROMME | MSGFLAG_UNSENT;
166 IMessage_SetProps(msg, 1, &p, NULL);
168 p.ulPropTag = PR_SENTMAIL_ENTRYID;
169 p.Value.bin.cb = props[0].Value.bin.cb;
170 p.Value.bin.lpb = props[0].Value.bin.lpb;
171 IMessage_SetProps(msg, 1,&p, NULL);
173 /* Set message subject */
174 if (message->lpszSubject)
176 p.ulPropTag = PR_SUBJECT_A;
177 p.Value.lpszA = message->lpszSubject;
178 IMessage_SetProps(msg, 1, &p, NULL);
181 /* Set message body */
182 if (message->lpszNoteText)
184 LPSTREAM stream = NULL;
186 if (IMessage_OpenProperty(msg, PR_BODY_A, &IID_IStream, 0,
187 MAPI_MODIFY | MAPI_CREATE, (LPUNKNOWN*) &stream) == S_OK)
189 IStream_Write(stream, message->lpszNoteText, strlen(message->lpszNoteText)+1, NULL);
190 IStream_Release(stream);
194 /* Add message attachments */
195 if (message->nFileCount > 0)
197 ULONG num_attach = 0;
200 for (i = 0; i < message->nFileCount; i++)
202 IAttach* attachment = NULL;
207 if (!message->lpFiles[i].lpszPathName)
210 /* Open the attachment for reading */
211 file = CreateFileA(message->lpFiles[i].lpszPathName, GENERIC_READ, FILE_SHARE_READ,
212 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
214 if (file == INVALID_HANDLE_VALUE)
217 /* Check if a display filename has been given; if not, get one ourselves from path name */
218 filename = message->lpFiles[i].lpszFileName;
222 filename = message->lpFiles[i].lpszPathName;
224 for (j = strlen(message->lpFiles[i].lpszPathName)-1; j >= 0; j--)
226 if (message->lpFiles[i].lpszPathName[i] == '\\' ||
227 message->lpFiles[i].lpszPathName[i] == '/')
229 filename = &message->lpFiles[i].lpszPathName[i+1];
235 TRACE("Attachment %d path: '%s'; filename: '%s'\n", i, debugstr_a(message->lpFiles[i].lpszPathName),
236 debugstr_a(filename));
238 /* Create the attachment */
239 if (IMessage_CreateAttach(msg, NULL, 0, &num_attach, &attachment) != S_OK)
241 TRACE("Unable to create attachment\n");
246 /* Set the attachment properties */
247 ZeroMemory(prop, sizeof(prop));
249 prop[0].ulPropTag = PR_ATTACH_METHOD;
250 prop[0].Value.ul = ATTACH_BY_VALUE;
251 prop[1].ulPropTag = PR_ATTACH_LONG_FILENAME_A;
252 prop[1].Value.lpszA = (LPSTR) filename;
253 prop[2].ulPropTag = PR_ATTACH_FILENAME_A;
254 prop[2].Value.lpszA = (LPSTR) filename;
255 prop[3].ulPropTag = PR_RENDERING_POSITION;
256 prop[3].Value.l = -1;
258 if (IAttach_SetProps(attachment, 4, prop, NULL) == S_OK)
260 LPSTREAM stream = NULL;
262 if (IAttach_OpenProperty(attachment, PR_ATTACH_DATA_BIN, &IID_IStream, 0,
263 MAPI_MODIFY | MAPI_CREATE, (LPUNKNOWN*) &stream) == S_OK)
265 BYTE data[READ_BUF_SIZE];
266 DWORD size = 0, read, written;
268 while (ReadFile(file, data, READ_BUF_SIZE, &read, NULL) && (read != 0))
270 IStream_Write(stream, data, read, &written);
274 TRACE("%d bytes read, %d bytes written of attachment\n", read, written);
276 IStream_Commit(stream, STGC_DEFAULT);
277 IStream_Release(stream);
279 prop[0].ulPropTag = PR_ATTACH_SIZE;
280 prop[0].Value.ul = size;
281 IAttach_SetProps(attachment, 1, prop, NULL);
283 IAttach_SaveChanges(attachment, KEEP_OPEN_READONLY);
289 IAttach_Release(attachment);
293 IMessage_SaveChanges(msg, KEEP_OPEN_READWRITE);
295 /* Prepare the message form */
297 if (IMAPISession_PrepareForm(session, NULL, msg, &token) == S_OK)
299 ULONG access = 0, status = 0, flags = 0, pc = 0;
300 ULONG pT[2] = {1, PR_MSG_STATUS};
302 /* Retrieve message status, flags, access rights and class */
304 if (IMessage_GetProps(msg, (LPSPropTagArray) pT, 0, &pc, &props) == S_OK)
306 status = props->Value.ul;
307 MAPIFreeBuffer(props);
310 pT[1] = PR_MESSAGE_FLAGS;
312 if (IMessage_GetProps(msg, (LPSPropTagArray) pT, 0, &pc, &props) == S_OK)
314 flags = props->Value.ul;
315 MAPIFreeBuffer(props);
320 if (IMessage_GetProps(msg, (LPSPropTagArray) pT, 0, &pc, &props) == S_OK)
322 access = props->Value.ul;
323 MAPIFreeBuffer(props);
326 pT[1] = PR_MESSAGE_CLASS_A;
328 if (IMessage_GetProps(msg, (LPSPropTagArray) pT, 0, &pc, &props) == S_OK)
330 /* Show the message form (edit window) */
332 ret = IMAPISession_ShowForm(session, 0, msg_store, draft_folder, NULL,
333 token, NULL, 0, status, flags, access,
339 retval = SUCCESS_SUCCESS;
342 case MAPI_E_USER_CANCEL:
343 retval = MAPI_E_USER_ABORT;
347 TRACE("ShowForm failure: %x\n", ret);
353 IMessage_Release(msg);
356 /* Free up the resources we've used */
357 IMAPIFolder_Release(draft_folder);
358 if (folder) IMAPIFolder_Release(folder);
359 IMsgStore_Release(msg_store);
362 IMAPISession_Logoff(session, 0, 0, 0);
363 IMAPISession_Release(session);
370 /**************************************************************************
371 * MAPISendMail (MAPI32.211)
376 * session [I] Handle to a MAPI session.
377 * uiparam [I] Parent window handle.
378 * message [I] Pointer to a MAPIMessage structure.
380 * reserved [I] Reserved, pass 0.
383 * Success: SUCCESS_SUCCESS
384 * Failure: MAPI_E_FAILURE
387 * The fallback procedure is a temporary hack.
389 ULONG WINAPI MAPISendMail( LHANDLE session, ULONG_PTR uiparam,
390 lpMapiMessage message, FLAGS flags, ULONG reserved )
392 ULONG ret = MAPI_E_FAILURE;
393 unsigned int i, to_count = 0, cc_count = 0, bcc_count = 0;
394 unsigned int to_size = 0, cc_size = 0, bcc_size = 0, subj_size, body_size;
396 char *to = NULL, *cc = NULL, *bcc = NULL;
397 const char *address, *subject, *body;
398 static const char format[] =
399 "mailto:\"%s\"?subject=\"%s\"&cc=\"%s\"&bcc=\"%s\"&body=\"%s\"";
400 char *mailto = NULL, *escape = NULL;
401 char empty_string[] = "";
405 TRACE( "(0x%08x 0x%08lx %p 0x%08x 0x%08x)\n", session, uiparam,
406 message, flags, reserved );
408 /* Check to see if we have a Simple MAPI provider loaded */
409 if (mapiFunctions.MAPISendMail)
410 return mapiFunctions.MAPISendMail(session, uiparam, message, flags, reserved);
412 /* Check if we have an Extended MAPI provider - if so, use our wrapper */
413 if (MAPIInitialize(NULL) == S_OK)
414 return sendmail_extended_mapi(session, uiparam, message, flags, reserved);
416 /* Fall back on our own implementation */
417 if (!message) return MAPI_E_FAILURE;
419 for (i = 0; i < message->nRecipCount; i++)
421 if (!message->lpRecips)
423 WARN("No recipients found\n");
424 return MAPI_E_FAILURE;
427 address = message->lpRecips[i].lpszAddress;
430 switch (message->lpRecips[i].ulRecipClass)
433 TRACE( "From: %s\n", debugstr_a(address) );
436 TRACE( "To: %s\n", debugstr_a(address) );
437 to_size += lstrlenA( address ) + 1;
440 TRACE( "Cc: %s\n", debugstr_a(address) );
441 cc_size += lstrlenA( address ) + 1;
444 TRACE( "Bcc: %s\n", debugstr_a(address) );
445 bcc_size += lstrlenA( address ) + 1;
448 TRACE( "Unknown recipient class: %d\n",
449 message->lpRecips[i].ulRecipClass );
453 FIXME("Name resolution and entry identifiers not supported\n");
455 if (message->nFileCount) FIXME("Ignoring attachments\n");
457 subject = message->lpszSubject ? message->lpszSubject : "";
458 body = message->lpszNoteText ? message->lpszNoteText : "";
460 TRACE( "Subject: %s\n", debugstr_a(subject) );
461 TRACE( "Body: %s\n", debugstr_a(body) );
463 subj_size = lstrlenA( subject );
464 body_size = lstrlenA( body );
466 ret = MAPI_E_INSUFFICIENT_MEMORY;
469 to = HeapAlloc( GetProcessHeap(), 0, to_size );
475 cc = HeapAlloc( GetProcessHeap(), 0, cc_size );
481 bcc = HeapAlloc( GetProcessHeap(), 0, bcc_size );
486 if (message->lpOriginator)
487 TRACE( "From: %s\n", debugstr_a(message->lpOriginator->lpszAddress) );
489 for (i = 0; i < message->nRecipCount; i++)
491 address = message->lpRecips[i].lpszAddress;
494 switch (message->lpRecips[i].ulRecipClass)
497 if (to_count) lstrcatA( to, "," );
498 lstrcatA( to, address );
502 if (cc_count) lstrcatA( cc, "," );
503 lstrcatA( cc, address );
507 if (bcc_count) lstrcatA( bcc, "," );
508 lstrcatA( bcc, address );
514 ret = MAPI_E_FAILURE;
515 size = sizeof(format) + to_size + cc_size + bcc_size + subj_size + body_size;
517 mailto = HeapAlloc( GetProcessHeap(), 0, size );
518 if (!mailto) goto exit;
520 sprintf( mailto, format, to ? to : "", subject, cc ? cc : "", bcc ? bcc : "", body );
523 res = UrlEscapeA( mailto, empty_string, &size, URL_ESCAPE_SPACES_ONLY );
524 if (res != E_POINTER) goto exit;
526 escape = HeapAlloc( GetProcessHeap(), 0, size );
527 if (!escape) goto exit;
529 res = UrlEscapeA( mailto, escape, &size, URL_ESCAPE_SPACES_ONLY );
530 if (res != S_OK) goto exit;
532 if ((UINT_PTR)ShellExecuteA( NULL, "open", escape, NULL, NULL, 0 ) > 32)
533 ret = SUCCESS_SUCCESS;
536 HeapFree( GetProcessHeap(), 0, to );
537 HeapFree( GetProcessHeap(), 0, cc );
538 HeapFree( GetProcessHeap(), 0, bcc );
539 HeapFree( GetProcessHeap(), 0, mailto );
540 HeapFree( GetProcessHeap(), 0, escape );
545 ULONG WINAPI MAPISendDocuments(ULONG_PTR uiparam, LPSTR delim, LPSTR paths,
546 LPSTR filenames, ULONG reserved)
548 if (mapiFunctions.MAPISendDocuments)
549 return mapiFunctions.MAPISendDocuments(uiparam, delim, paths, filenames, reserved);
551 return MAPI_E_NOT_SUPPORTED;