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
41 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(mapi
);
47 #define READ_BUF_SIZE 4096
49 #define STORE_UNICODE_OK 0x00040000
51 static LPSTR
convert_from_unicode(LPCWSTR wstr
)
59 len
= WideCharToMultiByte(CP_ACP
, 0, wstr
, -1, NULL
, 0, NULL
, NULL
);
60 str
= HeapAlloc(GetProcessHeap(), 0, len
);
61 WideCharToMultiByte(CP_ACP
, 0, wstr
, -1, str
, len
, NULL
, NULL
);
66 static LPWSTR
convert_to_unicode(LPSTR str
)
74 len
= MultiByteToWideChar(CP_ACP
, 0, str
, -1, NULL
, 0);
75 wstr
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
76 MultiByteToWideChar(CP_ACP
, 0, str
, -1, wstr
, len
);
82 Internal function to send a message via Extended MAPI. Wrapper around the Simple
83 MAPI function MAPISendMail.
85 static ULONG
sendmail_extended_mapi(LHANDLE mapi_session
, ULONG_PTR uiparam
, lpMapiMessageW message
,
88 ULONG tags
[] = {1, 0};
89 char *subjectA
= NULL
, *bodyA
= NULL
;
90 ULONG retval
= MAPI_E_FAILURE
;
91 IMAPISession
*session
= NULL
;
92 BOOL unicode_aware
= FALSE
;
93 IMAPITable
* msg_table
;
94 LPSRowSet rows
= NULL
;
96 IMAPIFolder
* folder
= NULL
, *draft_folder
= NULL
;
105 TRACE("Using Extended MAPI wrapper for MAPISendMail\n");
107 /* Attempt to log on via Extended MAPI */
109 ret
= MAPILogonEx(0, NULL
, NULL
, MAPI_EXTENDED
| MAPI_USE_DEFAULT
| MAPI_NEW_SESSION
, &session
);
110 TRACE("MAPILogonEx: %lx\n", ret
);
114 retval
= MAPI_E_LOGIN_FAILURE
;
118 /* Open the default message store */
120 if (IMAPISession_GetMsgStoresTable(session
, 0, &msg_table
) == S_OK
)
122 /* We want the default store */
123 SizedSPropTagArray(2, columns
) = {2, {PR_ENTRYID
, PR_DEFAULT_STORE
}};
125 /* Set the columns we want */
126 if (IMAPITable_SetColumns(msg_table
, (LPSPropTagArray
) &columns
, 0) == S_OK
)
130 if (IMAPITable_QueryRows(msg_table
, 1, 0, &rows
) != S_OK
)
132 MAPIFreeBuffer(rows
);
135 else if (rows
->cRows
!= 1)
142 /* If it's not the default store, try the next row */
143 if (!rows
->aRow
[0].lpProps
[1].Value
.b
)
154 IMAPITable_Release(msg_table
);
157 /* Did we manage to get the right store? */
161 /* Open the message store */
162 IMAPISession_OpenMsgStore(session
, 0, rows
->aRow
[0].lpProps
[0].Value
.bin
.cb
,
163 (ENTRYID
*) rows
->aRow
[0].lpProps
[0].Value
.bin
.lpb
, NULL
,
164 MDB_NO_DIALOG
| MAPI_BEST_ACCESS
, &msg_store
);
166 /* We don't need this any more */
169 /* Check if the message store supports Unicode */
170 tags
[1] = PR_STORE_SUPPORT_MASK
;
171 ret
= IMsgStore_GetProps(msg_store
, (LPSPropTagArray
) tags
, 0, &values
, &props
);
173 if ((ret
== S_OK
) && (props
[0].Value
.l
& STORE_UNICODE_OK
))
174 unicode_aware
= TRUE
;
177 /* Don't convert to ANSI */
178 if (flags
& MAPI_FORCE_UNICODE
)
180 WARN("No Unicode-capable mail client, and MAPI_FORCE_UNICODE is specified. MAPISendMail failed.\n");
181 retval
= MAPI_E_UNICODE_NOT_SUPPORTED
;
182 IMsgStore_Release(msg_store
);
187 /* First open the inbox, from which the drafts folder can be opened */
188 if (IMsgStore_GetReceiveFolder(msg_store
, NULL
, 0, &entry_len
, &entry_id
, NULL
) == S_OK
)
190 IMsgStore_OpenEntry(msg_store
, entry_len
, entry_id
, NULL
, 0, &obj_type
, (LPUNKNOWN
*) &folder
);
191 MAPIFreeBuffer(entry_id
);
194 tags
[1] = PR_IPM_DRAFTS_ENTRYID
;
196 /* Open the drafts folder, or failing that, try asking the message store for the outbox */
197 if ((folder
== NULL
) || ((ret
= IMAPIFolder_GetProps(folder
, (LPSPropTagArray
) tags
, 0, &values
, &props
)) != S_OK
))
199 TRACE("Unable to open Drafts folder; opening Outbox instead\n");
200 tags
[1] = PR_IPM_OUTBOX_ENTRYID
;
201 ret
= IMsgStore_GetProps(msg_store
, (LPSPropTagArray
) tags
, 0, &values
, &props
);
207 IMsgStore_OpenEntry(msg_store
, props
[0].Value
.bin
.cb
, (LPENTRYID
) props
[0].Value
.bin
.lpb
,
208 NULL
, MAPI_MODIFY
, &obj_type
, (LPUNKNOWN
*) &draft_folder
);
210 /* Create a new message */
211 if (IMAPIFolder_CreateMessage(draft_folder
, NULL
, 0, &msg
) == S_OK
)
216 /* Define message properties */
217 p
.ulPropTag
= PR_MESSAGE_FLAGS
;
218 p
.Value
.l
= MSGFLAG_FROMME
| MSGFLAG_UNSENT
;
220 IMessage_SetProps(msg
, 1, &p
, NULL
);
222 p
.ulPropTag
= PR_SENTMAIL_ENTRYID
;
223 p
.Value
.bin
.cb
= props
[0].Value
.bin
.cb
;
224 p
.Value
.bin
.lpb
= props
[0].Value
.bin
.lpb
;
225 IMessage_SetProps(msg
, 1,&p
, NULL
);
227 /* Set message subject */
228 if (message
->lpszSubject
)
232 p
.ulPropTag
= PR_SUBJECT_W
;
233 p
.Value
.lpszW
= message
->lpszSubject
;
237 subjectA
= convert_from_unicode(message
->lpszSubject
);
239 p
.ulPropTag
= PR_SUBJECT_A
;
240 p
.Value
.lpszA
= subjectA
;
243 IMessage_SetProps(msg
, 1, &p
, NULL
);
246 /* Set message body */
247 if (message
->lpszNoteText
)
249 LPSTREAM stream
= NULL
;
251 if (IMessage_OpenProperty(msg
, unicode_aware
? PR_BODY_W
: PR_BODY_A
, &IID_IStream
, 0,
252 MAPI_MODIFY
| MAPI_CREATE
, (LPUNKNOWN
*) &stream
) == S_OK
)
255 IStream_Write(stream
, message
->lpszNoteText
, (lstrlenW(message
->lpszNoteText
)+1) * sizeof(WCHAR
), NULL
);
258 bodyA
= convert_from_unicode(message
->lpszNoteText
);
259 IStream_Write(stream
, bodyA
, strlen(bodyA
)+1, NULL
);
262 IStream_Release(stream
);
266 /* Add message attachments */
267 if (message
->nFileCount
> 0)
269 ULONG num_attach
= 0;
272 for (i
= 0; i
< message
->nFileCount
; i
++)
274 IAttach
* attachment
= NULL
;
275 char *filenameA
= NULL
;
280 if (!message
->lpFiles
[i
].lpszPathName
)
283 /* Open the attachment for reading */
284 file
= CreateFileW(message
->lpFiles
[i
].lpszPathName
, GENERIC_READ
, FILE_SHARE_READ
,
285 NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
287 if (file
== INVALID_HANDLE_VALUE
)
290 /* Check if a display filename has been given; if not, get one ourselves from path name */
291 filename
= message
->lpFiles
[i
].lpszFileName
;
297 filename
= message
->lpFiles
[i
].lpszPathName
;
299 for (j
= lstrlenW(message
->lpFiles
[i
].lpszPathName
)-1; j
>= 0; j
--)
301 if (message
->lpFiles
[i
].lpszPathName
[i
] == '\\' ||
302 message
->lpFiles
[i
].lpszPathName
[i
] == '/')
304 filename
= &message
->lpFiles
[i
].lpszPathName
[i
+1];
310 TRACE("Attachment %u path: '%s'; filename: '%s'\n", i
, debugstr_w(message
->lpFiles
[i
].lpszPathName
),
311 debugstr_w(filename
));
313 /* Create the attachment */
314 if (IMessage_CreateAttach(msg
, NULL
, 0, &num_attach
, &attachment
) != S_OK
)
316 TRACE("Unable to create attachment\n");
321 /* Set the attachment properties */
322 ZeroMemory(prop
, sizeof(prop
));
324 prop
[0].ulPropTag
= PR_ATTACH_METHOD
;
325 prop
[0].Value
.ul
= ATTACH_BY_VALUE
;
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
;
336 filenameA
= convert_from_unicode(filename
);
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
;
345 prop
[3].ulPropTag
= PR_RENDERING_POSITION
;
346 prop
[3].Value
.l
= -1;
348 if (IAttach_SetProps(attachment
, 4, prop
, NULL
) == S_OK
)
350 LPSTREAM stream
= NULL
;
352 if (IAttach_OpenProperty(attachment
, PR_ATTACH_DATA_BIN
, &IID_IStream
, 0,
353 MAPI_MODIFY
| MAPI_CREATE
, (LPUNKNOWN
*) &stream
) == S_OK
)
355 BYTE data
[READ_BUF_SIZE
];
356 DWORD size
= 0, read
, written
;
358 while (ReadFile(file
, data
, READ_BUF_SIZE
, &read
, NULL
) && (read
!= 0))
360 IStream_Write(stream
, data
, read
, &written
);
364 TRACE("%ld bytes written of attachment\n", size
);
366 IStream_Commit(stream
, STGC_DEFAULT
);
367 IStream_Release(stream
);
369 prop
[0].ulPropTag
= PR_ATTACH_SIZE
;
370 prop
[0].Value
.ul
= size
;
371 IAttach_SetProps(attachment
, 1, prop
, NULL
);
373 IAttach_SaveChanges(attachment
, KEEP_OPEN_READONLY
);
379 IAttach_Release(attachment
);
381 HeapFree(GetProcessHeap(), 0, filenameA
);
385 IMessage_SaveChanges(msg
, KEEP_OPEN_READWRITE
);
387 /* Prepare the message form */
389 if (IMAPISession_PrepareForm(session
, NULL
, msg
, &token
) == S_OK
)
391 ULONG access
= 0, status
= 0, message_flags
= 0, pc
= 0;
392 ULONG pT
[2] = {1, PR_MSG_STATUS
};
394 /* Retrieve message status, flags, access rights and class */
396 if (IMessage_GetProps(msg
, (LPSPropTagArray
) pT
, 0, &pc
, &props
) == S_OK
)
398 status
= props
->Value
.ul
;
399 MAPIFreeBuffer(props
);
402 pT
[1] = PR_MESSAGE_FLAGS
;
404 if (IMessage_GetProps(msg
, (LPSPropTagArray
) pT
, 0, &pc
, &props
) == S_OK
)
406 message_flags
= props
->Value
.ul
;
407 MAPIFreeBuffer(props
);
412 if (IMessage_GetProps(msg
, (LPSPropTagArray
) pT
, 0, &pc
, &props
) == S_OK
)
414 access
= props
->Value
.ul
;
415 MAPIFreeBuffer(props
);
418 pT
[1] = PR_MESSAGE_CLASS_A
;
420 if (IMessage_GetProps(msg
, (LPSPropTagArray
) pT
, 0, &pc
, &props
) == S_OK
)
422 /* Show the message form (edit window) */
424 ret
= IMAPISession_ShowForm(session
, 0, msg_store
, draft_folder
, NULL
,
425 token
, NULL
, 0, status
, message_flags
, access
,
431 retval
= SUCCESS_SUCCESS
;
434 case MAPI_E_USER_CANCEL
:
435 retval
= MAPI_E_USER_ABORT
;
439 TRACE("ShowForm failure: %lx\n", ret
);
445 IMessage_Release(msg
);
448 /* Free up the resources we've used */
449 IMAPIFolder_Release(draft_folder
);
450 if (folder
) IMAPIFolder_Release(folder
);
451 IMsgStore_Release(msg_store
);
453 HeapFree(GetProcessHeap(), 0, subjectA
);
454 HeapFree(GetProcessHeap(), 0, bodyA
);
457 IMAPISession_Logoff(session
, 0, 0, 0);
458 IMAPISession_Release(session
);
465 /**************************************************************************
466 * MAPISendMail (MAPI32.211)
471 * session [I] Handle to a MAPI session.
472 * uiparam [I] Parent window handle.
473 * message [I] Pointer to a MAPIMessage structure.
475 * reserved [I] Reserved, pass 0.
478 * Success: SUCCESS_SUCCESS
479 * Failure: MAPI_E_FAILURE
482 ULONG WINAPI
MAPISendMail( LHANDLE session
, ULONG_PTR uiparam
,
483 lpMapiMessage message
, FLAGS flags
, ULONG reserved
)
485 WCHAR msg_title
[READ_BUF_SIZE
], error_msg
[READ_BUF_SIZE
];
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
);
491 /* Check if we have an Extended MAPI provider - if so, use our wrapper */
492 if (MAPIInitialize(NULL
) == S_OK
)
494 MapiMessageW messageW
;
497 ZeroMemory(&messageW
, sizeof(MapiMessageW
));
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
;
504 if (message
->nFileCount
&& message
->lpFiles
)
506 lpMapiFileDescW filesW
;
509 filesW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(MapiFileDescW
) * message
->nFileCount
);
511 for (i
= 0; i
< message
->nFileCount
; i
++)
513 filesW
[i
].lpszPathName
= convert_to_unicode(message
->lpFiles
[i
].lpszPathName
);
514 filesW
[i
].lpszFileName
= convert_to_unicode(message
->lpFiles
[i
].lpszFileName
);
517 messageW
.lpFiles
= filesW
;
520 ret
= sendmail_extended_mapi(session
, uiparam
, &messageW
, flags
);
522 /* Now free everything we allocated */
523 if (message
->nFileCount
&& message
->lpFiles
)
527 for (i
= 0; i
< message
->nFileCount
; i
++)
529 HeapFree(GetProcessHeap(), 0, messageW
.lpFiles
[i
].lpszPathName
);
530 HeapFree(GetProcessHeap(), 0, messageW
.lpFiles
[i
].lpszFileName
);
533 HeapFree(GetProcessHeap(), 0, messageW
.lpFiles
);
536 HeapFree(GetProcessHeap(), 0, messageW
.lpszSubject
);
537 HeapFree(GetProcessHeap(), 0, messageW
.lpszNoteText
);
542 /* Display an error message since we apparently have no mail clients */
543 LoadStringW(hInstMAPI32
, IDS_NO_MAPI_CLIENT
, error_msg
, ARRAY_SIZE(error_msg
));
544 LoadStringW(hInstMAPI32
, IDS_SEND_MAIL
, msg_title
, ARRAY_SIZE(msg_title
));
546 MessageBoxW((HWND
) uiparam
, error_msg
, msg_title
, MB_ICONEXCLAMATION
);
548 return MAPI_E_NOT_SUPPORTED
;
551 static lpMapiRecipDesc
convert_recipient_from_unicode(lpMapiRecipDescW recipW
, lpMapiRecipDesc dest
)
561 ret
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(MapiRecipDesc
));
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
;
572 /**************************************************************************
573 * MAPISendMailW (MAPI32.256)
578 * session [I] Handle to a MAPI session.
579 * uiparam [I] Parent window handle.
580 * message [I] Pointer to a MAPIMessageW structure.
582 * reserved [I] Reserved, pass 0.
585 * Success: SUCCESS_SUCCESS
586 * Failure: MAPI_E_FAILURE
589 ULONG WINAPI
MAPISendMailW(LHANDLE session
, ULONG_PTR uiparam
,
590 lpMapiMessageW message
, FLAGS flags
, ULONG reserved
)
592 WCHAR msg_title
[READ_BUF_SIZE
], error_msg
[READ_BUF_SIZE
];
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
);
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
);
602 if (mapiFunctions
.MAPISendMail
)
604 MapiMessage messageA
;
607 if (flags
& MAPI_FORCE_UNICODE
)
608 return MAPI_E_UNICODE_NOT_SUPPORTED
;
610 /* Convert to ANSI and send to MAPISendMail */
611 ZeroMemory(&messageA
, sizeof(MapiMessage
));
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
;
623 if (message
->nRecipCount
&& message
->lpRecips
)
625 lpMapiRecipDesc recipsA
;
628 recipsA
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(MapiRecipDesc
) * message
->nRecipCount
);
630 for (i
= 0; i
< message
->nRecipCount
; i
++)
632 convert_recipient_from_unicode(&message
->lpRecips
[i
], &recipsA
[i
]);
635 messageA
.lpRecips
= recipsA
;
638 if (message
->nFileCount
&& message
->lpFiles
)
640 lpMapiFileDesc filesA
;
643 filesA
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(MapiFileDesc
) * message
->nFileCount
);
645 for (i
= 0; i
< message
->nFileCount
; i
++)
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
;
654 messageA
.lpFiles
= filesA
;
657 ret
= mapiFunctions
.MAPISendMail(session
, uiparam
, &messageA
, flags
, reserved
);
659 /* Now free everything we allocated */
660 if (message
->lpOriginator
)
662 HeapFree(GetProcessHeap(), 0, messageA
.lpOriginator
->lpszName
);
663 HeapFree(GetProcessHeap(), 0, messageA
.lpOriginator
->lpszAddress
);
664 HeapFree(GetProcessHeap(), 0, messageA
.lpOriginator
);
667 if (message
->nRecipCount
&& message
->lpRecips
)
671 for (i
= 0; i
< message
->nRecipCount
; i
++)
673 HeapFree(GetProcessHeap(), 0, messageA
.lpRecips
[i
].lpszName
);
674 HeapFree(GetProcessHeap(), 0, messageA
.lpRecips
[i
].lpszAddress
);
677 HeapFree(GetProcessHeap(), 0, messageA
.lpRecips
);
680 if (message
->nFileCount
&& message
->lpFiles
)
684 for (i
= 0; i
< message
->nFileCount
; i
++)
686 HeapFree(GetProcessHeap(), 0, messageA
.lpFiles
[i
].lpszPathName
);
687 HeapFree(GetProcessHeap(), 0, messageA
.lpFiles
[i
].lpszFileName
);
690 HeapFree(GetProcessHeap(), 0, messageA
.lpFiles
);
693 HeapFree(GetProcessHeap(), 0, messageA
.lpszSubject
);
694 HeapFree(GetProcessHeap(), 0, messageA
.lpszNoteText
);
695 HeapFree(GetProcessHeap(), 0, messageA
.lpszDateReceived
);
696 HeapFree(GetProcessHeap(), 0, messageA
.lpszConversationID
);
701 /* Display an error message since we apparently have no mail clients */
702 LoadStringW(hInstMAPI32
, IDS_NO_MAPI_CLIENT
, error_msg
, ARRAY_SIZE(error_msg
));
703 LoadStringW(hInstMAPI32
, IDS_SEND_MAIL
, msg_title
, ARRAY_SIZE(msg_title
));
705 MessageBoxW((HWND
) uiparam
, error_msg
, msg_title
, MB_ICONEXCLAMATION
);
707 return MAPI_E_NOT_SUPPORTED
;
710 ULONG WINAPI
MAPISendDocuments(ULONG_PTR uiparam
, LPSTR delim
, LPSTR paths
,
711 LPSTR filenames
, ULONG reserved
)
713 if (mapiFunctions
.MAPISendDocuments
)
714 return mapiFunctions
.MAPISendDocuments(uiparam
, delim
, paths
, filenames
, reserved
);
716 return MAPI_E_NOT_SUPPORTED
;