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
52 Internal function to send a message via Extended MAPI. Wrapper around the Simple
53 MAPI function MAPISendMail.
55 static ULONG
sendmail_extended_mapi(LHANDLE mapi_session
, ULONG_PTR uiparam
, lpMapiMessage message
,
56 FLAGS flags
, ULONG reserved
)
58 ULONG tags
[] = {1, PR_IPM_DRAFTS_ENTRYID
};
59 ULONG retval
= MAPI_E_FAILURE
;
60 IMAPISession
*session
= NULL
;
61 IMAPITable
* msg_table
;
62 LPSRowSet rows
= NULL
;
64 IMAPIFolder
* folder
= NULL
, *draft_folder
= NULL
;
73 TRACE("Using Extended MAPI wrapper for MAPISendMail\n");
75 /* Attempt to log on via Extended MAPI */
77 ret
= MAPILogonEx(0, NULL
, NULL
, MAPI_EXTENDED
| MAPI_USE_DEFAULT
| MAPI_NEW_SESSION
, &session
);
78 TRACE("MAPILogonEx: %x\n", ret
);
82 retval
= MAPI_E_LOGIN_FAILURE
;
86 /* Open the default message store */
88 if (IMAPISession_GetMsgStoresTable(session
, 0, &msg_table
) == S_OK
)
90 /* We want the default store */
91 SizedSPropTagArray(2, columns
) = {2, {PR_ENTRYID
, PR_DEFAULT_STORE
}};
93 /* Set the columns we want */
94 if (IMAPITable_SetColumns(msg_table
, (LPSPropTagArray
) &columns
, 0) == S_OK
)
98 if (IMAPITable_QueryRows(msg_table
, 1, 0, &rows
) != S_OK
)
100 MAPIFreeBuffer(rows
);
103 else if (rows
->cRows
!= 1)
110 /* If it's not the default store, try the next row */
111 if (!rows
->aRow
[0].lpProps
[1].Value
.b
)
122 IMAPITable_Release(msg_table
);
125 /* Did we manage to get the right store? */
129 /* Open the message store */
130 IMAPISession_OpenMsgStore(session
, 0, rows
->aRow
[0].lpProps
[0].Value
.bin
.cb
,
131 (ENTRYID
*) rows
->aRow
[0].lpProps
[0].Value
.bin
.lpb
, NULL
,
132 MDB_NO_DIALOG
| MAPI_BEST_ACCESS
, &msg_store
);
134 /* We don't need this any more */
137 /* First open the inbox, from which the drafts folder can be opened */
138 if (IMsgStore_GetReceiveFolder(msg_store
, NULL
, 0, &entry_len
, &entry_id
, NULL
) == S_OK
)
140 IMsgStore_OpenEntry(msg_store
, entry_len
, entry_id
, NULL
, 0, &obj_type
, (LPUNKNOWN
*) &folder
);
141 MAPIFreeBuffer(entry_id
);
144 /* Open the drafts folder, or failing that, try asking the message store for the outbox */
145 if ((folder
== NULL
) || ((ret
= IMAPIFolder_GetProps(folder
, (LPSPropTagArray
) tags
, 0, &values
, &props
)) != S_OK
))
147 TRACE("Unable to open Drafts folder; opening Outbox instead\n");
148 tags
[1] = PR_IPM_OUTBOX_ENTRYID
;
149 ret
= IMsgStore_GetProps(msg_store
, (LPSPropTagArray
) tags
, 0, &values
, &props
);
155 IMsgStore_OpenEntry(msg_store
, props
[0].Value
.bin
.cb
, (LPENTRYID
) props
[0].Value
.bin
.lpb
,
156 NULL
, MAPI_MODIFY
, &obj_type
, (LPUNKNOWN
*) &draft_folder
);
158 /* Create a new message */
159 if (IMAPIFolder_CreateMessage(draft_folder
, NULL
, 0, &msg
) == S_OK
)
164 /* Define message properties */
165 p
.ulPropTag
= PR_MESSAGE_FLAGS
;
166 p
.Value
.l
= MSGFLAG_FROMME
| MSGFLAG_UNSENT
;
168 IMessage_SetProps(msg
, 1, &p
, NULL
);
170 p
.ulPropTag
= PR_SENTMAIL_ENTRYID
;
171 p
.Value
.bin
.cb
= props
[0].Value
.bin
.cb
;
172 p
.Value
.bin
.lpb
= props
[0].Value
.bin
.lpb
;
173 IMessage_SetProps(msg
, 1,&p
, NULL
);
175 /* Set message subject */
176 if (message
->lpszSubject
)
178 p
.ulPropTag
= PR_SUBJECT_A
;
179 p
.Value
.lpszA
= message
->lpszSubject
;
180 IMessage_SetProps(msg
, 1, &p
, NULL
);
183 /* Set message body */
184 if (message
->lpszNoteText
)
186 LPSTREAM stream
= NULL
;
188 if (IMessage_OpenProperty(msg
, PR_BODY_A
, &IID_IStream
, 0,
189 MAPI_MODIFY
| MAPI_CREATE
, (LPUNKNOWN
*) &stream
) == S_OK
)
191 IStream_Write(stream
, message
->lpszNoteText
, strlen(message
->lpszNoteText
)+1, NULL
);
192 IStream_Release(stream
);
196 /* Add message attachments */
197 if (message
->nFileCount
> 0)
199 ULONG num_attach
= 0;
202 for (i
= 0; i
< message
->nFileCount
; i
++)
204 IAttach
* attachment
= NULL
;
209 if (!message
->lpFiles
[i
].lpszPathName
)
212 /* Open the attachment for reading */
213 file
= CreateFileA(message
->lpFiles
[i
].lpszPathName
, GENERIC_READ
, FILE_SHARE_READ
,
214 NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
216 if (file
== INVALID_HANDLE_VALUE
)
219 /* Check if a display filename has been given; if not, get one ourselves from path name */
220 filename
= message
->lpFiles
[i
].lpszFileName
;
224 filename
= message
->lpFiles
[i
].lpszPathName
;
226 for (j
= strlen(message
->lpFiles
[i
].lpszPathName
)-1; j
>= 0; j
--)
228 if (message
->lpFiles
[i
].lpszPathName
[i
] == '\\' ||
229 message
->lpFiles
[i
].lpszPathName
[i
] == '/')
231 filename
= &message
->lpFiles
[i
].lpszPathName
[i
+1];
237 TRACE("Attachment %d path: '%s'; filename: '%s'\n", i
, debugstr_a(message
->lpFiles
[i
].lpszPathName
),
238 debugstr_a(filename
));
240 /* Create the attachment */
241 if (IMessage_CreateAttach(msg
, NULL
, 0, &num_attach
, &attachment
) != S_OK
)
243 TRACE("Unable to create attachment\n");
248 /* Set the attachment properties */
249 ZeroMemory(prop
, sizeof(prop
));
251 prop
[0].ulPropTag
= PR_ATTACH_METHOD
;
252 prop
[0].Value
.ul
= ATTACH_BY_VALUE
;
253 prop
[1].ulPropTag
= PR_ATTACH_LONG_FILENAME_A
;
254 prop
[1].Value
.lpszA
= (LPSTR
) filename
;
255 prop
[2].ulPropTag
= PR_ATTACH_FILENAME_A
;
256 prop
[2].Value
.lpszA
= (LPSTR
) filename
;
257 prop
[3].ulPropTag
= PR_RENDERING_POSITION
;
258 prop
[3].Value
.l
= -1;
260 if (IAttach_SetProps(attachment
, 4, prop
, NULL
) == S_OK
)
262 LPSTREAM stream
= NULL
;
264 if (IAttach_OpenProperty(attachment
, PR_ATTACH_DATA_BIN
, &IID_IStream
, 0,
265 MAPI_MODIFY
| MAPI_CREATE
, (LPUNKNOWN
*) &stream
) == S_OK
)
267 BYTE data
[READ_BUF_SIZE
];
268 DWORD size
= 0, read
, written
;
270 while (ReadFile(file
, data
, READ_BUF_SIZE
, &read
, NULL
) && (read
!= 0))
272 IStream_Write(stream
, data
, read
, &written
);
276 TRACE("%d bytes read, %d bytes written of attachment\n", read
, written
);
278 IStream_Commit(stream
, STGC_DEFAULT
);
279 IStream_Release(stream
);
281 prop
[0].ulPropTag
= PR_ATTACH_SIZE
;
282 prop
[0].Value
.ul
= size
;
283 IAttach_SetProps(attachment
, 1, prop
, NULL
);
285 IAttach_SaveChanges(attachment
, KEEP_OPEN_READONLY
);
291 IAttach_Release(attachment
);
295 IMessage_SaveChanges(msg
, KEEP_OPEN_READWRITE
);
297 /* Prepare the message form */
299 if (IMAPISession_PrepareForm(session
, NULL
, msg
, &token
) == S_OK
)
301 ULONG access
= 0, status
= 0, flags
= 0, pc
= 0;
302 ULONG pT
[2] = {1, PR_MSG_STATUS
};
304 /* Retrieve message status, flags, access rights and class */
306 if (IMessage_GetProps(msg
, (LPSPropTagArray
) pT
, 0, &pc
, &props
) == S_OK
)
308 status
= props
->Value
.ul
;
309 MAPIFreeBuffer(props
);
312 pT
[1] = PR_MESSAGE_FLAGS
;
314 if (IMessage_GetProps(msg
, (LPSPropTagArray
) pT
, 0, &pc
, &props
) == S_OK
)
316 flags
= props
->Value
.ul
;
317 MAPIFreeBuffer(props
);
322 if (IMessage_GetProps(msg
, (LPSPropTagArray
) pT
, 0, &pc
, &props
) == S_OK
)
324 access
= props
->Value
.ul
;
325 MAPIFreeBuffer(props
);
328 pT
[1] = PR_MESSAGE_CLASS_A
;
330 if (IMessage_GetProps(msg
, (LPSPropTagArray
) pT
, 0, &pc
, &props
) == S_OK
)
332 /* Show the message form (edit window) */
334 ret
= IMAPISession_ShowForm(session
, 0, msg_store
, draft_folder
, NULL
,
335 token
, NULL
, 0, status
, flags
, access
,
341 retval
= SUCCESS_SUCCESS
;
344 case MAPI_E_USER_CANCEL
:
345 retval
= MAPI_E_USER_ABORT
;
349 TRACE("ShowForm failure: %x\n", ret
);
355 IMessage_Release(msg
);
358 /* Free up the resources we've used */
359 IMAPIFolder_Release(draft_folder
);
360 if (folder
) IMAPIFolder_Release(folder
);
361 IMsgStore_Release(msg_store
);
364 IMAPISession_Logoff(session
, 0, 0, 0);
365 IMAPISession_Release(session
);
372 /**************************************************************************
373 * MAPISendMail (MAPI32.211)
378 * session [I] Handle to a MAPI session.
379 * uiparam [I] Parent window handle.
380 * message [I] Pointer to a MAPIMessage structure.
382 * reserved [I] Reserved, pass 0.
385 * Success: SUCCESS_SUCCESS
386 * Failure: MAPI_E_FAILURE
389 ULONG WINAPI
MAPISendMail( LHANDLE session
, ULONG_PTR uiparam
,
390 lpMapiMessage message
, FLAGS flags
, ULONG reserved
)
392 WCHAR msg_title
[READ_BUF_SIZE
], error_msg
[READ_BUF_SIZE
];
394 /* Check to see if we have a Simple MAPI provider loaded */
395 if (mapiFunctions
.MAPISendMail
)
396 return mapiFunctions
.MAPISendMail(session
, uiparam
, message
, flags
, reserved
);
398 /* Check if we have an Extended MAPI provider - if so, use our wrapper */
399 if (MAPIInitialize(NULL
) == S_OK
)
400 return sendmail_extended_mapi(session
, uiparam
, message
, flags
, reserved
);
402 /* Display an error message since we apparently have no mail clients */
403 LoadStringW(hInstMAPI32
, IDS_NO_MAPI_CLIENT
, error_msg
, sizeof(error_msg
) / sizeof(WCHAR
));
404 LoadStringW(hInstMAPI32
, IDS_SEND_MAIL
, msg_title
, sizeof(msg_title
) / sizeof(WCHAR
));
406 MessageBoxW((HWND
) uiparam
, error_msg
, msg_title
, MB_ICONEXCLAMATION
);
408 return MAPI_E_NOT_SUPPORTED
;
411 ULONG WINAPI
MAPISendDocuments(ULONG_PTR uiparam
, LPSTR delim
, LPSTR paths
,
412 LPSTR filenames
, ULONG reserved
)
414 if (mapiFunctions
.MAPISendDocuments
)
415 return mapiFunctions
.MAPISendDocuments(uiparam
, delim
, paths
, filenames
, reserved
);
417 return MAPI_E_NOT_SUPPORTED
;