mapi32: Log on via Extended MAPI and get the default message store.
[wine/testsucceed.git] / dlls / mapi32 / sendmail.c
blobd644525a9d14be51fa8a1d10f7d9fc5bf296c107
1 /*
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
22 #include "config.h"
23 #include "wine/port.h"
25 #include <stdio.h>
26 #include <stdarg.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winerror.h"
31 #include "objbase.h"
32 #include "mapi.h"
33 #include "mapix.h"
34 #include "mapiutil.h"
35 #include "mapidefs.h"
36 #include "winreg.h"
37 #include "shellapi.h"
38 #include "shlwapi.h"
39 #include "wine/debug.h"
40 #include "util.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(mapi);
45 Internal function to send a message via Extended MAPI. Wrapper around the Simple
46 MAPI function MAPISendMail.
48 static ULONG sendmail_extended_mapi(LHANDLE mapi_session, ULONG_PTR uiparam, lpMapiMessage message,
49 FLAGS flags, ULONG reserved)
51 ULONG retval = MAPI_E_FAILURE;
52 IMAPISession *session = NULL;
53 IMAPITable* msg_table;
54 LPSRowSet rows = NULL;
55 HRESULT ret;
57 TRACE("Using Extended MAPI wrapper for MAPISendMail\n");
59 /* Attempt to log on via Extended MAPI */
61 ret = MAPILogonEx(0, NULL, NULL, MAPI_EXTENDED | MAPI_USE_DEFAULT | MAPI_NEW_SESSION, &session);
62 TRACE("MAPILogonEx: %x\n", ret);
64 if (ret != S_OK)
66 retval = MAPI_E_LOGIN_FAILURE;
67 goto cleanup;
70 /* Open the default message store */
72 if (IMAPISession_GetMsgStoresTable(session, 0, &msg_table) == S_OK)
74 /* We want the default store */
75 SizedSPropTagArray(2, columns) = {2, {PR_ENTRYID, PR_DEFAULT_STORE}};
77 /* Set the columns we want */
78 if (IMAPITable_SetColumns(msg_table, (LPSPropTagArray) &columns, 0) == S_OK)
80 while (1)
82 if (IMAPITable_QueryRows(msg_table, 1, 0, &rows) != S_OK)
84 MAPIFreeBuffer(rows);
85 rows = NULL;
87 else if (rows->cRows != 1)
89 FreeProws(rows);
90 rows = NULL;
92 else
94 /* If it's not the default store, try the next row */
95 if (!rows->aRow[0].lpProps[1].Value.b)
97 FreeProws(rows);
98 continue;
102 break;
106 IMAPITable_Release(msg_table);
109 /* Did we manage to get the right store? */
110 if (!rows)
111 goto logoff;
113 /* We don't need this any more */
114 FreeProws(rows);
116 logoff: ;
117 IMAPISession_Logoff(session, (ULONG) NULL, 0, 0);
118 IMAPISession_Release(session);
120 cleanup: ;
121 MAPIUninitialize();
122 return retval;
125 /**************************************************************************
126 * MAPISendMail (MAPI32.211)
128 * Send a mail.
130 * PARAMS
131 * session [I] Handle to a MAPI session.
132 * uiparam [I] Parent window handle.
133 * message [I] Pointer to a MAPIMessage structure.
134 * flags [I] Flags.
135 * reserved [I] Reserved, pass 0.
137 * RETURNS
138 * Success: SUCCESS_SUCCESS
139 * Failure: MAPI_E_FAILURE
141 * NOTES
142 * The fallback procedure is a temporary hack.
144 ULONG WINAPI MAPISendMail( LHANDLE session, ULONG_PTR uiparam,
145 lpMapiMessage message, FLAGS flags, ULONG reserved )
147 ULONG ret = MAPI_E_FAILURE;
148 unsigned int i, to_count = 0, cc_count = 0, bcc_count = 0;
149 unsigned int to_size = 0, cc_size = 0, bcc_size = 0, subj_size, body_size;
151 char *to = NULL, *cc = NULL, *bcc = NULL;
152 const char *address, *subject, *body;
153 static const char format[] =
154 "mailto:\"%s\"?subject=\"%s\"&cc=\"%s\"&bcc=\"%s\"&body=\"%s\"";
155 char *mailto = NULL, *escape = NULL;
156 char empty_string[] = "";
157 HRESULT res;
158 DWORD size;
160 TRACE( "(0x%08x 0x%08lx %p 0x%08x 0x%08x)\n", session, uiparam,
161 message, flags, reserved );
163 /* Check to see if we have a Simple MAPI provider loaded */
164 if (mapiFunctions.MAPISendMail)
165 return mapiFunctions.MAPISendMail(session, uiparam, message, flags, reserved);
167 /* Check if we have an Extended MAPI provider - if so, use our wrapper */
168 if (MAPIInitialize(NULL) == S_OK)
169 return sendmail_extended_mapi(session, uiparam, message, flags, reserved);
171 /* Fall back on our own implementation */
172 if (!message) return MAPI_E_FAILURE;
174 for (i = 0; i < message->nRecipCount; i++)
176 if (!message->lpRecips)
178 WARN("No recipients found\n");
179 return MAPI_E_FAILURE;
182 address = message->lpRecips[i].lpszAddress;
183 if (address)
185 switch (message->lpRecips[i].ulRecipClass)
187 case MAPI_ORIG:
188 TRACE( "From: %s\n", debugstr_a(address) );
189 break;
190 case MAPI_TO:
191 TRACE( "To: %s\n", debugstr_a(address) );
192 to_size += lstrlenA( address ) + 1;
193 break;
194 case MAPI_CC:
195 TRACE( "Cc: %s\n", debugstr_a(address) );
196 cc_size += lstrlenA( address ) + 1;
197 break;
198 case MAPI_BCC:
199 TRACE( "Bcc: %s\n", debugstr_a(address) );
200 bcc_size += lstrlenA( address ) + 1;
201 break;
202 default:
203 TRACE( "Unknown recipient class: %d\n",
204 message->lpRecips[i].ulRecipClass );
207 else
208 FIXME("Name resolution and entry identifiers not supported\n");
210 if (message->nFileCount) FIXME("Ignoring attachments\n");
212 subject = message->lpszSubject ? message->lpszSubject : "";
213 body = message->lpszNoteText ? message->lpszNoteText : "";
215 TRACE( "Subject: %s\n", debugstr_a(subject) );
216 TRACE( "Body: %s\n", debugstr_a(body) );
218 subj_size = lstrlenA( subject );
219 body_size = lstrlenA( body );
221 ret = MAPI_E_INSUFFICIENT_MEMORY;
222 if (to_size)
224 to = HeapAlloc( GetProcessHeap(), 0, to_size );
225 if (!to) goto exit;
226 to[0] = 0;
228 if (cc_size)
230 cc = HeapAlloc( GetProcessHeap(), 0, cc_size );
231 if (!cc) goto exit;
232 cc[0] = 0;
234 if (bcc_size)
236 bcc = HeapAlloc( GetProcessHeap(), 0, bcc_size );
237 if (!bcc) goto exit;
238 bcc[0] = 0;
241 if (message->lpOriginator)
242 TRACE( "From: %s\n", debugstr_a(message->lpOriginator->lpszAddress) );
244 for (i = 0; i < message->nRecipCount; i++)
246 address = message->lpRecips[i].lpszAddress;
247 if (address)
249 switch (message->lpRecips[i].ulRecipClass)
251 case MAPI_TO:
252 if (to_count) lstrcatA( to, "," );
253 lstrcatA( to, address );
254 to_count++;
255 break;
256 case MAPI_CC:
257 if (cc_count) lstrcatA( cc, "," );
258 lstrcatA( cc, address );
259 cc_count++;
260 break;
261 case MAPI_BCC:
262 if (bcc_count) lstrcatA( bcc, "," );
263 lstrcatA( bcc, address );
264 bcc_count++;
265 break;
269 ret = MAPI_E_FAILURE;
270 size = sizeof(format) + to_size + cc_size + bcc_size + subj_size + body_size;
272 mailto = HeapAlloc( GetProcessHeap(), 0, size );
273 if (!mailto) goto exit;
275 sprintf( mailto, format, to ? to : "", subject, cc ? cc : "", bcc ? bcc : "", body );
277 size = 1;
278 res = UrlEscapeA( mailto, empty_string, &size, URL_ESCAPE_SPACES_ONLY );
279 if (res != E_POINTER) goto exit;
281 escape = HeapAlloc( GetProcessHeap(), 0, size );
282 if (!escape) goto exit;
284 res = UrlEscapeA( mailto, escape, &size, URL_ESCAPE_SPACES_ONLY );
285 if (res != S_OK) goto exit;
287 if ((UINT_PTR)ShellExecuteA( NULL, "open", escape, NULL, NULL, 0 ) > 32)
288 ret = SUCCESS_SUCCESS;
290 exit:
291 HeapFree( GetProcessHeap(), 0, to );
292 HeapFree( GetProcessHeap(), 0, cc );
293 HeapFree( GetProcessHeap(), 0, bcc );
294 HeapFree( GetProcessHeap(), 0, mailto );
295 HeapFree( GetProcessHeap(), 0, escape );
297 return ret;
300 ULONG WINAPI MAPISendDocuments(ULONG_PTR uiparam, LPSTR delim, LPSTR paths,
301 LPSTR filenames, ULONG reserved)
303 if (mapiFunctions.MAPISendDocuments)
304 return mapiFunctions.MAPISendDocuments(uiparam, delim, paths, filenames, reserved);
306 return MAPI_E_NOT_SUPPORTED;