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"
43 #include "wine/debug.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
)
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
);
68 static LPWSTR
convert_to_unicode(LPSTR str
)
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
);
84 Internal function to send a message via Extended MAPI. Wrapper around the Simple
85 MAPI function MAPISendMail.
87 static ULONG
sendmail_extended_mapi(LHANDLE mapi_session
, ULONG_PTR uiparam
, lpMapiMessageW message
,
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
;
98 IMAPIFolder
* folder
= NULL
, *draft_folder
= NULL
;
107 TRACE("Using Extended MAPI wrapper for MAPISendMail\n");
109 /* Attempt to log on via Extended MAPI */
111 ret
= MAPILogonEx(0, NULL
, NULL
, MAPI_EXTENDED
| MAPI_USE_DEFAULT
| MAPI_NEW_SESSION
, &session
);
112 TRACE("MAPILogonEx: %x\n", ret
);
116 retval
= MAPI_E_LOGIN_FAILURE
;
120 /* Open the default message store */
122 if (IMAPISession_GetMsgStoresTable(session
, 0, &msg_table
) == S_OK
)
124 /* We want the default store */
125 SizedSPropTagArray(2, columns
) = {2, {PR_ENTRYID
, PR_DEFAULT_STORE
}};
127 /* Set the columns we want */
128 if (IMAPITable_SetColumns(msg_table
, (LPSPropTagArray
) &columns
, 0) == S_OK
)
132 if (IMAPITable_QueryRows(msg_table
, 1, 0, &rows
) != S_OK
)
134 MAPIFreeBuffer(rows
);
137 else if (rows
->cRows
!= 1)
144 /* If it's not the default store, try the next row */
145 if (!rows
->aRow
[0].lpProps
[1].Value
.b
)
156 IMAPITable_Release(msg_table
);
159 /* Did we manage to get the right store? */
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
);
168 /* We don't need this any more */
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
);
175 if ((ret
== S_OK
) && (props
[0].Value
.l
& STORE_UNICODE_OK
))
176 unicode_aware
= TRUE
;
179 /* Don't convert to ANSI */
180 if (flags
& MAPI_FORCE_UNICODE
)
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
);
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
)
192 IMsgStore_OpenEntry(msg_store
, entry_len
, entry_id
, NULL
, 0, &obj_type
, (LPUNKNOWN
*) &folder
);
193 MAPIFreeBuffer(entry_id
);
196 tags
[1] = PR_IPM_DRAFTS_ENTRYID
;
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
))
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
);
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
);
212 /* Create a new message */
213 if (IMAPIFolder_CreateMessage(draft_folder
, NULL
, 0, &msg
) == S_OK
)
218 /* Define message properties */
219 p
.ulPropTag
= PR_MESSAGE_FLAGS
;
220 p
.Value
.l
= MSGFLAG_FROMME
| MSGFLAG_UNSENT
;
222 IMessage_SetProps(msg
, 1, &p
, NULL
);
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
);
229 /* Set message subject */
230 if (message
->lpszSubject
)
234 p
.ulPropTag
= PR_SUBJECT_W
;
235 p
.Value
.lpszW
= message
->lpszSubject
;
239 subjectA
= convert_from_unicode(message
->lpszSubject
);
241 p
.ulPropTag
= PR_SUBJECT_A
;
242 p
.Value
.lpszA
= subjectA
;
245 IMessage_SetProps(msg
, 1, &p
, NULL
);
248 /* Set message body */
249 if (message
->lpszNoteText
)
251 LPSTREAM stream
= NULL
;
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
)
257 IStream_Write(stream
, message
->lpszNoteText
, (lstrlenW(message
->lpszNoteText
)+1) * sizeof(WCHAR
), NULL
);
260 bodyA
= convert_from_unicode(message
->lpszNoteText
);
261 IStream_Write(stream
, bodyA
, strlen(bodyA
)+1, NULL
);
264 IStream_Release(stream
);
268 /* Add message attachments */
269 if (message
->nFileCount
> 0)
271 ULONG num_attach
= 0;
274 for (i
= 0; i
< message
->nFileCount
; i
++)
276 IAttach
* attachment
= NULL
;
277 char *filenameA
= NULL
;
282 if (!message
->lpFiles
[i
].lpszPathName
)
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
);
289 if (file
== INVALID_HANDLE_VALUE
)
292 /* Check if a display filename has been given; if not, get one ourselves from path name */
293 filename
= message
->lpFiles
[i
].lpszFileName
;
299 filename
= message
->lpFiles
[i
].lpszPathName
;
301 for (j
= lstrlenW(message
->lpFiles
[i
].lpszPathName
)-1; j
>= 0; j
--)
303 if (message
->lpFiles
[i
].lpszPathName
[i
] == '\\' ||
304 message
->lpFiles
[i
].lpszPathName
[i
] == '/')
306 filename
= &message
->lpFiles
[i
].lpszPathName
[i
+1];
312 TRACE("Attachment %u path: '%s'; filename: '%s'\n", i
, debugstr_w(message
->lpFiles
[i
].lpszPathName
),
313 debugstr_w(filename
));
315 /* Create the attachment */
316 if (IMessage_CreateAttach(msg
, NULL
, 0, &num_attach
, &attachment
) != S_OK
)
318 TRACE("Unable to create attachment\n");
323 /* Set the attachment properties */
324 ZeroMemory(prop
, sizeof(prop
));
326 prop
[0].ulPropTag
= PR_ATTACH_METHOD
;
327 prop
[0].Value
.ul
= ATTACH_BY_VALUE
;
331 prop
[1].ulPropTag
= PR_ATTACH_LONG_FILENAME_W
;
332 prop
[1].Value
.lpszW
= (LPWSTR
) filename
;
333 prop
[2].ulPropTag
= PR_ATTACH_FILENAME_W
;
334 prop
[2].Value
.lpszW
= (LPWSTR
) filename
;
338 filenameA
= convert_from_unicode(filename
);
340 prop
[1].ulPropTag
= PR_ATTACH_LONG_FILENAME_A
;
341 prop
[1].Value
.lpszA
= (LPSTR
) filenameA
;
342 prop
[2].ulPropTag
= PR_ATTACH_FILENAME_A
;
343 prop
[2].Value
.lpszA
= (LPSTR
) filenameA
;
347 prop
[3].ulPropTag
= PR_RENDERING_POSITION
;
348 prop
[3].Value
.l
= -1;
350 if (IAttach_SetProps(attachment
, 4, prop
, NULL
) == S_OK
)
352 LPSTREAM stream
= NULL
;
354 if (IAttach_OpenProperty(attachment
, PR_ATTACH_DATA_BIN
, &IID_IStream
, 0,
355 MAPI_MODIFY
| MAPI_CREATE
, (LPUNKNOWN
*) &stream
) == S_OK
)
357 BYTE data
[READ_BUF_SIZE
];
358 DWORD size
= 0, read
, written
;
360 while (ReadFile(file
, data
, READ_BUF_SIZE
, &read
, NULL
) && (read
!= 0))
362 IStream_Write(stream
, data
, read
, &written
);
366 TRACE("%d bytes written of attachment\n", size
);
368 IStream_Commit(stream
, STGC_DEFAULT
);
369 IStream_Release(stream
);
371 prop
[0].ulPropTag
= PR_ATTACH_SIZE
;
372 prop
[0].Value
.ul
= size
;
373 IAttach_SetProps(attachment
, 1, prop
, NULL
);
375 IAttach_SaveChanges(attachment
, KEEP_OPEN_READONLY
);
381 IAttach_Release(attachment
);
383 HeapFree(GetProcessHeap(), 0, filenameA
);
387 IMessage_SaveChanges(msg
, KEEP_OPEN_READWRITE
);
389 /* Prepare the message form */
391 if (IMAPISession_PrepareForm(session
, NULL
, msg
, &token
) == S_OK
)
393 ULONG access
= 0, status
= 0, message_flags
= 0, pc
= 0;
394 ULONG pT
[2] = {1, PR_MSG_STATUS
};
396 /* Retrieve message status, flags, access rights and class */
398 if (IMessage_GetProps(msg
, (LPSPropTagArray
) pT
, 0, &pc
, &props
) == S_OK
)
400 status
= props
->Value
.ul
;
401 MAPIFreeBuffer(props
);
404 pT
[1] = PR_MESSAGE_FLAGS
;
406 if (IMessage_GetProps(msg
, (LPSPropTagArray
) pT
, 0, &pc
, &props
) == S_OK
)
408 message_flags
= props
->Value
.ul
;
409 MAPIFreeBuffer(props
);
414 if (IMessage_GetProps(msg
, (LPSPropTagArray
) pT
, 0, &pc
, &props
) == S_OK
)
416 access
= props
->Value
.ul
;
417 MAPIFreeBuffer(props
);
420 pT
[1] = PR_MESSAGE_CLASS_A
;
422 if (IMessage_GetProps(msg
, (LPSPropTagArray
) pT
, 0, &pc
, &props
) == S_OK
)
424 /* Show the message form (edit window) */
426 ret
= IMAPISession_ShowForm(session
, 0, msg_store
, draft_folder
, NULL
,
427 token
, NULL
, 0, status
, message_flags
, access
,
433 retval
= SUCCESS_SUCCESS
;
436 case MAPI_E_USER_CANCEL
:
437 retval
= MAPI_E_USER_ABORT
;
441 TRACE("ShowForm failure: %x\n", ret
);
447 IMessage_Release(msg
);
450 /* Free up the resources we've used */
451 IMAPIFolder_Release(draft_folder
);
452 if (folder
) IMAPIFolder_Release(folder
);
453 IMsgStore_Release(msg_store
);
455 HeapFree(GetProcessHeap(), 0, subjectA
);
456 HeapFree(GetProcessHeap(), 0, bodyA
);
459 IMAPISession_Logoff(session
, 0, 0, 0);
460 IMAPISession_Release(session
);
467 /**************************************************************************
468 * MAPISendMail (MAPI32.211)
473 * session [I] Handle to a MAPI session.
474 * uiparam [I] Parent window handle.
475 * message [I] Pointer to a MAPIMessage structure.
477 * reserved [I] Reserved, pass 0.
480 * Success: SUCCESS_SUCCESS
481 * Failure: MAPI_E_FAILURE
484 ULONG WINAPI
MAPISendMail( LHANDLE session
, ULONG_PTR uiparam
,
485 lpMapiMessage message
, FLAGS flags
, ULONG reserved
)
487 WCHAR msg_title
[READ_BUF_SIZE
], error_msg
[READ_BUF_SIZE
];
489 /* Check to see if we have a Simple MAPI provider loaded */
490 if (mapiFunctions
.MAPISendMail
)
491 return mapiFunctions
.MAPISendMail(session
, uiparam
, message
, flags
, reserved
);
493 /* Check if we have an Extended MAPI provider - if so, use our wrapper */
494 if (MAPIInitialize(NULL
) == S_OK
)
496 MapiMessageW messageW
;
499 ZeroMemory(&messageW
, sizeof(MapiMessageW
));
501 /* Convert the entries we need to Unicode */
502 messageW
.lpszSubject
= convert_to_unicode(message
->lpszSubject
);
503 messageW
.lpszNoteText
= convert_to_unicode(message
->lpszNoteText
);
504 messageW
.nFileCount
= message
->nFileCount
;
506 if (message
->nFileCount
&& message
->lpFiles
)
508 lpMapiFileDescW filesW
;
511 filesW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(MapiFileDescW
) * message
->nFileCount
);
513 for (i
= 0; i
< message
->nFileCount
; i
++)
515 filesW
[i
].lpszPathName
= convert_to_unicode(message
->lpFiles
[i
].lpszPathName
);
516 filesW
[i
].lpszFileName
= convert_to_unicode(message
->lpFiles
[i
].lpszFileName
);
519 messageW
.lpFiles
= filesW
;
522 ret
= sendmail_extended_mapi(session
, uiparam
, &messageW
, flags
);
524 /* Now free everything we allocated */
525 if (message
->nFileCount
&& message
->lpFiles
)
529 for (i
= 0; i
< message
->nFileCount
; i
++)
531 HeapFree(GetProcessHeap(), 0, messageW
.lpFiles
[i
].lpszPathName
);
532 HeapFree(GetProcessHeap(), 0, messageW
.lpFiles
[i
].lpszFileName
);
535 HeapFree(GetProcessHeap(), 0, messageW
.lpFiles
);
538 HeapFree(GetProcessHeap(), 0, messageW
.lpszSubject
);
539 HeapFree(GetProcessHeap(), 0, messageW
.lpszNoteText
);
544 /* Display an error message since we apparently have no mail clients */
545 LoadStringW(hInstMAPI32
, IDS_NO_MAPI_CLIENT
, error_msg
, sizeof(error_msg
) / sizeof(WCHAR
));
546 LoadStringW(hInstMAPI32
, IDS_SEND_MAIL
, msg_title
, sizeof(msg_title
) / sizeof(WCHAR
));
548 MessageBoxW((HWND
) uiparam
, error_msg
, msg_title
, MB_ICONEXCLAMATION
);
550 return MAPI_E_NOT_SUPPORTED
;
553 static lpMapiRecipDesc
convert_recipient_from_unicode(lpMapiRecipDescW recipW
, lpMapiRecipDesc dest
)
563 ret
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(MapiRecipDesc
));
565 ret
->ulRecipClass
= recipW
->ulRecipClass
;
566 ret
->lpszName
= convert_from_unicode(recipW
->lpszName
);
567 ret
->lpszAddress
= convert_from_unicode(recipW
->lpszAddress
);
568 ret
->ulEIDSize
= recipW
->ulEIDSize
;
569 ret
->lpEntryID
= recipW
->lpEntryID
;
574 /**************************************************************************
575 * MAPISendMailW (MAPI32.256)
580 * session [I] Handle to a MAPI session.
581 * uiparam [I] Parent window handle.
582 * message [I] Pointer to a MAPIMessageW structure.
584 * reserved [I] Reserved, pass 0.
587 * Success: SUCCESS_SUCCESS
588 * Failure: MAPI_E_FAILURE
591 ULONG WINAPI
MAPISendMailW(LHANDLE session
, ULONG_PTR uiparam
,
592 lpMapiMessageW message
, FLAGS flags
, ULONG reserved
)
594 WCHAR msg_title
[READ_BUF_SIZE
], error_msg
[READ_BUF_SIZE
];
596 /* Check to see if we have a Simple MAPI provider loaded */
597 if (mapiFunctions
.MAPISendMailW
)
598 return mapiFunctions
.MAPISendMailW(session
, uiparam
, message
, flags
, reserved
);
600 /* Check if we have an Extended MAPI provider - if so, use our wrapper */
601 if (MAPIInitialize(NULL
) == S_OK
)
602 return sendmail_extended_mapi(session
, uiparam
, message
, flags
);
604 if (mapiFunctions
.MAPISendMail
)
606 MapiMessage messageA
;
609 if (flags
& MAPI_FORCE_UNICODE
)
610 return MAPI_E_UNICODE_NOT_SUPPORTED
;
612 /* Convert to ANSI and send to MAPISendMail */
613 ZeroMemory(&messageA
, sizeof(MapiMessage
));
615 messageA
.lpszSubject
= convert_from_unicode(message
->lpszSubject
);
616 messageA
.lpszNoteText
= convert_from_unicode(message
->lpszNoteText
);
617 messageA
.lpszMessageType
= convert_from_unicode(message
->lpszMessageType
);
618 messageA
.lpszDateReceived
= convert_from_unicode(message
->lpszDateReceived
);
619 messageA
.lpszConversationID
= convert_from_unicode(message
->lpszConversationID
);
620 messageA
.flFlags
= message
->flFlags
;
621 messageA
.lpOriginator
= convert_recipient_from_unicode(message
->lpOriginator
, NULL
);
622 messageA
.nRecipCount
= message
->nRecipCount
;
623 messageA
.nFileCount
= message
->nFileCount
;
625 if (message
->nRecipCount
&& message
->lpRecips
)
627 lpMapiRecipDesc recipsA
;
630 recipsA
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(MapiRecipDesc
) * message
->nRecipCount
);
632 for (i
= 0; i
< message
->nRecipCount
; i
++)
634 convert_recipient_from_unicode(&message
->lpRecips
[i
], &recipsA
[i
]);
637 messageA
.lpRecips
= recipsA
;
640 if (message
->nFileCount
&& message
->lpFiles
)
642 lpMapiFileDesc filesA
;
645 filesA
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(MapiFileDesc
) * message
->nFileCount
);
647 for (i
= 0; i
< message
->nFileCount
; i
++)
649 filesA
[i
].flFlags
= message
->lpFiles
[i
].flFlags
;
650 filesA
[i
].nPosition
= message
->lpFiles
[i
].nPosition
;
651 filesA
[i
].lpszPathName
= convert_from_unicode(message
->lpFiles
[i
].lpszPathName
);
652 filesA
[i
].lpszFileName
= convert_from_unicode(message
->lpFiles
[i
].lpszFileName
);
653 filesA
[i
].lpFileType
= message
->lpFiles
[i
].lpFileType
;
656 messageA
.lpFiles
= filesA
;
659 ret
= mapiFunctions
.MAPISendMail(session
, uiparam
, &messageA
, flags
, reserved
);
661 /* Now free everything we allocated */
662 if (message
->lpOriginator
)
664 HeapFree(GetProcessHeap(), 0, messageA
.lpOriginator
->lpszName
);
665 HeapFree(GetProcessHeap(), 0, messageA
.lpOriginator
->lpszAddress
);
666 HeapFree(GetProcessHeap(), 0, messageA
.lpOriginator
);
669 if (message
->nRecipCount
&& message
->lpRecips
)
673 for (i
= 0; i
< message
->nRecipCount
; i
++)
675 HeapFree(GetProcessHeap(), 0, messageA
.lpRecips
[i
].lpszName
);
676 HeapFree(GetProcessHeap(), 0, messageA
.lpRecips
[i
].lpszAddress
);
679 HeapFree(GetProcessHeap(), 0, messageA
.lpRecips
);
682 if (message
->nFileCount
&& message
->lpFiles
)
686 for (i
= 0; i
< message
->nFileCount
; i
++)
688 HeapFree(GetProcessHeap(), 0, messageA
.lpFiles
[i
].lpszPathName
);
689 HeapFree(GetProcessHeap(), 0, messageA
.lpFiles
[i
].lpszFileName
);
692 HeapFree(GetProcessHeap(), 0, messageA
.lpFiles
);
695 HeapFree(GetProcessHeap(), 0, messageA
.lpszSubject
);
696 HeapFree(GetProcessHeap(), 0, messageA
.lpszNoteText
);
697 HeapFree(GetProcessHeap(), 0, messageA
.lpszDateReceived
);
698 HeapFree(GetProcessHeap(), 0, messageA
.lpszConversationID
);
703 /* Display an error message since we apparently have no mail clients */
704 LoadStringW(hInstMAPI32
, IDS_NO_MAPI_CLIENT
, error_msg
, sizeof(error_msg
) / sizeof(WCHAR
));
705 LoadStringW(hInstMAPI32
, IDS_SEND_MAIL
, msg_title
, sizeof(msg_title
) / sizeof(WCHAR
));
707 MessageBoxW((HWND
) uiparam
, error_msg
, msg_title
, MB_ICONEXCLAMATION
);
709 return MAPI_E_NOT_SUPPORTED
;
712 ULONG WINAPI
MAPISendDocuments(ULONG_PTR uiparam
, LPSTR delim
, LPSTR paths
,
713 LPSTR filenames
, ULONG reserved
)
715 if (mapiFunctions
.MAPISendDocuments
)
716 return mapiFunctions
.MAPISendDocuments(uiparam
, delim
, paths
, filenames
, reserved
);
718 return MAPI_E_NOT_SUPPORTED
;