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"
39 #include "wine/debug.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
;
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
);
66 retval
= MAPI_E_LOGIN_FAILURE
;
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
)
82 if (IMAPITable_QueryRows(msg_table
, 1, 0, &rows
) != S_OK
)
87 else if (rows
->cRows
!= 1)
94 /* If it's not the default store, try the next row */
95 if (!rows
->aRow
[0].lpProps
[1].Value
.b
)
106 IMAPITable_Release(msg_table
);
109 /* Did we manage to get the right store? */
113 /* We don't need this any more */
117 IMAPISession_Logoff(session
, (ULONG
) NULL
, 0, 0);
118 IMAPISession_Release(session
);
125 /**************************************************************************
126 * MAPISendMail (MAPI32.211)
131 * session [I] Handle to a MAPI session.
132 * uiparam [I] Parent window handle.
133 * message [I] Pointer to a MAPIMessage structure.
135 * reserved [I] Reserved, pass 0.
138 * Success: SUCCESS_SUCCESS
139 * Failure: MAPI_E_FAILURE
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
[] = "";
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
;
185 switch (message
->lpRecips
[i
].ulRecipClass
)
188 TRACE( "From: %s\n", debugstr_a(address
) );
191 TRACE( "To: %s\n", debugstr_a(address
) );
192 to_size
+= lstrlenA( address
) + 1;
195 TRACE( "Cc: %s\n", debugstr_a(address
) );
196 cc_size
+= lstrlenA( address
) + 1;
199 TRACE( "Bcc: %s\n", debugstr_a(address
) );
200 bcc_size
+= lstrlenA( address
) + 1;
203 TRACE( "Unknown recipient class: %d\n",
204 message
->lpRecips
[i
].ulRecipClass
);
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
;
224 to
= HeapAlloc( GetProcessHeap(), 0, to_size
);
230 cc
= HeapAlloc( GetProcessHeap(), 0, cc_size
);
236 bcc
= HeapAlloc( GetProcessHeap(), 0, bcc_size
);
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
;
249 switch (message
->lpRecips
[i
].ulRecipClass
)
252 if (to_count
) lstrcatA( to
, "," );
253 lstrcatA( to
, address
);
257 if (cc_count
) lstrcatA( cc
, "," );
258 lstrcatA( cc
, address
);
262 if (bcc_count
) lstrcatA( bcc
, "," );
263 lstrcatA( bcc
, address
);
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
);
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
;
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
);
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
;