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;
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 *) &folder);
156 /* Create a new message */
157 if (IMAPIFolder_CreateMessage(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, 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(folder);
358 IMsgStore_Release(msg_store);
361 IMAPISession_Logoff(session, 0, 0, 0);
362 IMAPISession_Release(session);
369 /**************************************************************************
370 * MAPISendMail (MAPI32.211)
375 * session [I] Handle to a MAPI session.
376 * uiparam [I] Parent window handle.
377 * message [I] Pointer to a MAPIMessage structure.
379 * reserved [I] Reserved, pass 0.
382 * Success: SUCCESS_SUCCESS
383 * Failure: MAPI_E_FAILURE
386 * The fallback procedure is a temporary hack.
388 ULONG WINAPI MAPISendMail( LHANDLE session, ULONG_PTR uiparam,
389 lpMapiMessage message, FLAGS flags, ULONG reserved )
391 ULONG ret = MAPI_E_FAILURE;
392 unsigned int i, to_count = 0, cc_count = 0, bcc_count = 0;
393 unsigned int to_size = 0, cc_size = 0, bcc_size = 0, subj_size, body_size;
395 char *to = NULL, *cc = NULL, *bcc = NULL;
396 const char *address, *subject, *body;
397 static const char format[] =
398 "mailto:\"%s\"?subject=\"%s\"&cc=\"%s\"&bcc=\"%s\"&body=\"%s\"";
399 char *mailto = NULL, *escape = NULL;
400 char empty_string[] = "";
404 TRACE( "(0x%08x 0x%08lx %p 0x%08x 0x%08x)\n", session, uiparam,
405 message, flags, reserved );
407 /* Check to see if we have a Simple MAPI provider loaded */
408 if (mapiFunctions.MAPISendMail)
409 return mapiFunctions.MAPISendMail(session, uiparam, message, flags, reserved);
411 /* Check if we have an Extended MAPI provider - if so, use our wrapper */
412 if (MAPIInitialize(NULL) == S_OK)
413 return sendmail_extended_mapi(session, uiparam, message, flags, reserved);
415 /* Fall back on our own implementation */
416 if (!message) return MAPI_E_FAILURE;
418 for (i = 0; i < message->nRecipCount; i++)
420 if (!message->lpRecips)
422 WARN("No recipients found\n");
423 return MAPI_E_FAILURE;
426 address = message->lpRecips[i].lpszAddress;
429 switch (message->lpRecips[i].ulRecipClass)
432 TRACE( "From: %s\n", debugstr_a(address) );
435 TRACE( "To: %s\n", debugstr_a(address) );
436 to_size += lstrlenA( address ) + 1;
439 TRACE( "Cc: %s\n", debugstr_a(address) );
440 cc_size += lstrlenA( address ) + 1;
443 TRACE( "Bcc: %s\n", debugstr_a(address) );
444 bcc_size += lstrlenA( address ) + 1;
447 TRACE( "Unknown recipient class: %d\n",
448 message->lpRecips[i].ulRecipClass );
452 FIXME("Name resolution and entry identifiers not supported\n");
454 if (message->nFileCount) FIXME("Ignoring attachments\n");
456 subject = message->lpszSubject ? message->lpszSubject : "";
457 body = message->lpszNoteText ? message->lpszNoteText : "";
459 TRACE( "Subject: %s\n", debugstr_a(subject) );
460 TRACE( "Body: %s\n", debugstr_a(body) );
462 subj_size = lstrlenA( subject );
463 body_size = lstrlenA( body );
465 ret = MAPI_E_INSUFFICIENT_MEMORY;
468 to = HeapAlloc( GetProcessHeap(), 0, to_size );
474 cc = HeapAlloc( GetProcessHeap(), 0, cc_size );
480 bcc = HeapAlloc( GetProcessHeap(), 0, bcc_size );
485 if (message->lpOriginator)
486 TRACE( "From: %s\n", debugstr_a(message->lpOriginator->lpszAddress) );
488 for (i = 0; i < message->nRecipCount; i++)
490 address = message->lpRecips[i].lpszAddress;
493 switch (message->lpRecips[i].ulRecipClass)
496 if (to_count) lstrcatA( to, "," );
497 lstrcatA( to, address );
501 if (cc_count) lstrcatA( cc, "," );
502 lstrcatA( cc, address );
506 if (bcc_count) lstrcatA( bcc, "," );
507 lstrcatA( bcc, address );
513 ret = MAPI_E_FAILURE;
514 size = sizeof(format) + to_size + cc_size + bcc_size + subj_size + body_size;
516 mailto = HeapAlloc( GetProcessHeap(), 0, size );
517 if (!mailto) goto exit;
519 sprintf( mailto, format, to ? to : "", subject, cc ? cc : "", bcc ? bcc : "", body );
522 res = UrlEscapeA( mailto, empty_string, &size, URL_ESCAPE_SPACES_ONLY );
523 if (res != E_POINTER) goto exit;
525 escape = HeapAlloc( GetProcessHeap(), 0, size );
526 if (!escape) goto exit;
528 res = UrlEscapeA( mailto, escape, &size, URL_ESCAPE_SPACES_ONLY );
529 if (res != S_OK) goto exit;
531 if ((UINT_PTR)ShellExecuteA( NULL, "open", escape, NULL, NULL, 0 ) > 32)
532 ret = SUCCESS_SUCCESS;
535 HeapFree( GetProcessHeap(), 0, to );
536 HeapFree( GetProcessHeap(), 0, cc );
537 HeapFree( GetProcessHeap(), 0, bcc );
538 HeapFree( GetProcessHeap(), 0, mailto );
539 HeapFree( GetProcessHeap(), 0, escape );
544 ULONG WINAPI MAPISendDocuments(ULONG_PTR uiparam, LPSTR delim, LPSTR paths,
545 LPSTR filenames, ULONG reserved)
547 if (mapiFunctions.MAPISendDocuments)
548 return mapiFunctions.MAPISendDocuments(uiparam, delim, paths, filenames, reserved);
550 return MAPI_E_NOT_SUPPORTED;