1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: senddoc.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_shell.hxx"
33 #include <osl/diagnose.h>
35 #ifndef _RTL_STRING_HXX_
36 //#include <rtl/string.hxx>
38 #include "simplemapi.hxx"
40 #define WIN32_LEAN_AND_MEAN
42 #pragma warning(push, 1)
55 #if OSL_DEBUG_LEVEL > 2
59 typedef std::vector
<std::string
> StringList_t
;
60 typedef StringList_t::const_iterator StringListIterator_t
;
61 typedef std::vector
<MapiRecipDesc
> MapiRecipientList_t
;
62 typedef std::vector
<MapiFileDesc
> MapiAttachmentList_t
;
64 const int LEN_SMTP_PREFIX
= 5; // "SMTP:"
66 namespace /* private */
74 StringList_t gAttachments
;
79 Add a prefix to an email address. MAPI requires that that
80 email addresses have an 'SMTP:' prefix.
83 [in] the email address.
86 [in] the prefix to be added to the email address.
89 the email address prefixed with the specified prefix.
91 inline std::string
prefixEmailAddress(
92 const std::string
& aEmailAddress
,
93 const std::string
& aPrefix
= "SMTP:")
95 return (aPrefix
+ aEmailAddress
);
101 const std::string
& recipAddress
,
102 MapiRecipientList_t
* pMapiRecipientList
)
105 ZeroMemory(&mrd
, sizeof(mrd
));
107 mrd
.ulRecipClass
= recipClass
;
108 mrd
.lpszName
= const_cast<char*>(recipAddress
.c_str()) + LEN_SMTP_PREFIX
;
109 mrd
.lpszAddress
= const_cast<char*>(recipAddress
.c_str());
110 pMapiRecipientList
->push_back(mrd
);
114 void initRecipientList(MapiRecipientList_t
* pMapiRecipientList
)
116 OSL_ASSERT(pMapiRecipientList
->size() == 0);
119 StringListIterator_t iter
= gTo
.begin();
120 StringListIterator_t iter_end
= gTo
.end();
121 for (; iter
!= iter_end
; ++iter
)
122 addRecipient(MAPI_TO
, *iter
, pMapiRecipientList
);
126 iter_end
= gCc
.end();
127 for (; iter
!= iter_end
; ++iter
)
128 addRecipient(MAPI_CC
, *iter
, pMapiRecipientList
);
130 // add bcc recipients
132 iter_end
= gBcc
.end();
133 for (; iter
!= iter_end
; ++iter
)
134 addRecipient(MAPI_BCC
, *iter
, pMapiRecipientList
);
138 void initAttachementList(MapiAttachmentList_t
* pMapiAttachmentList
)
140 OSL_ASSERT(pMapiAttachmentList
->size() == 0);
142 StringListIterator_t iter
= gAttachments
.begin();
143 StringListIterator_t iter_end
= gAttachments
.end();
144 for (/**/; iter
!= iter_end
; ++iter
)
147 ZeroMemory(&mfd
, sizeof(mfd
));
148 mfd
.lpszPathName
= const_cast<char*>(iter
->c_str());
149 mfd
.nPosition
= sal::static_int_cast
<ULONG
>(-1);
150 pMapiAttachmentList
->push_back(mfd
);
155 void initMapiOriginator(MapiRecipDesc
* pMapiOriginator
)
157 ZeroMemory(pMapiOriginator
, sizeof(MapiRecipDesc
));
159 pMapiOriginator
->ulRecipClass
= MAPI_ORIG
;
160 pMapiOriginator
->lpszName
= "";
161 pMapiOriginator
->lpszAddress
= const_cast<char*>(gFrom
.c_str());
165 void initMapiMessage(
166 MapiRecipDesc
* aMapiOriginator
,
167 MapiRecipientList_t
& aMapiRecipientList
,
168 MapiAttachmentList_t
& aMapiAttachmentList
,
169 MapiMessage
* pMapiMessage
)
171 ZeroMemory(pMapiMessage
, sizeof(MapiMessage
));
173 pMapiMessage
->lpszSubject
= const_cast<char*>(gSubject
.c_str());
174 pMapiMessage
->lpszNoteText
= (gBody
.length() ? const_cast<char*>(gBody
.c_str()) : NULL
);
175 pMapiMessage
->lpOriginator
= aMapiOriginator
;
176 pMapiMessage
->lpRecips
= &aMapiRecipientList
[0];
177 pMapiMessage
->nRecipCount
= aMapiRecipientList
.size();
178 pMapiMessage
->lpFiles
= &aMapiAttachmentList
[0];
179 pMapiMessage
->nFileCount
= aMapiAttachmentList
.size();
182 char* KnownParameter
[] =
195 const size_t nKnownParameter
= (sizeof(KnownParameter
)/sizeof(KnownParameter
[0]));
198 bool isKnownParameter(const char* aParameterName
)
200 for (size_t i
= 0; i
< nKnownParameter
; i
++)
201 if (_tcsicmp(aParameterName
, KnownParameter
[i
]) == 0)
208 void initParameter(int argc
, char* argv
[])
210 for (int i
= 1; i
< argc
; i
++)
212 if (!isKnownParameter(argv
[i
]))
214 OSL_ENSURE(false, "Wrong parameter received");
218 if ((_tcsicmp(argv
[i
], TEXT("--mapi-dialog")) == 0))
220 gMapiFlags
|= MAPI_DIALOG
;
222 else if ((_tcsicmp(argv
[i
], TEXT("--mapi-logon-ui")) == 0))
224 gMapiFlags
|= MAPI_LOGON_UI
;
226 else if ((i
+1) < argc
) // is the value of a parameter available too?
228 if (_tcsicmp(argv
[i
], TEXT("--to")) == 0)
229 gTo
.push_back(prefixEmailAddress(argv
[i
+1]));
230 else if (_tcsicmp(argv
[i
], TEXT("--cc")) == 0)
231 gCc
.push_back(prefixEmailAddress(argv
[i
+1]));
232 else if (_tcsicmp(argv
[i
], TEXT("--bcc")) == 0)
233 gBcc
.push_back(prefixEmailAddress(argv
[i
+1]));
234 else if (_tcsicmp(argv
[i
], TEXT("--from")) == 0)
235 gFrom
= prefixEmailAddress(argv
[i
+1]);
236 else if (_tcsicmp(argv
[i
], TEXT("--subject")) == 0)
237 gSubject
= argv
[i
+1];
238 else if (_tcsicmp(argv
[i
], TEXT("--body")) == 0)
240 else if ((_tcsicmp(argv
[i
], TEXT("--attach")) == 0))
241 gAttachments
.push_back(argv
[i
+1]);
250 NOTE: Because this is program only serves implementation
251 purposes and should not be used by any end user the
252 parameter checking is very limited. Every unknown parameter
255 int main(int argc
, char* argv
[])
257 //MessageBox(NULL, "Debug", "Debug", MB_OK);
259 initParameter(argc
, argv
);
261 #if OSL_DEBUG_LEVEL > 2
265 ULONG ulRet
= MAPI_E_FAILURE
;
271 // #93007# we have to set the flag MAPI_NEW_SESSION,
272 // because in the case Outlook xxx (not Outlook Express!)
273 // is installed as Exchange and Mail Client a Profile
274 // selection dialog must appear because we specify no
275 // profile name, so the user has to specify a profile
276 FLAGS flFlag
= MAPI_NEW_SESSION
| MAPI_LOGON_UI
;
278 ulRet
= mapi
.MAPILogon(0, NULL
, NULL
, flFlag
, 0L, &hSession
);
280 if (ulRet
== SUCCESS_SUCCESS
)
282 MapiRecipDesc mapiOriginator
;
283 MapiRecipientList_t mapiRecipientList
;
284 MapiAttachmentList_t mapiAttachmentList
;
287 initMapiOriginator(&mapiOriginator
);
288 initRecipientList(&mapiRecipientList
);
289 initAttachementList(&mapiAttachmentList
);
290 initMapiMessage((gFrom
.length() ? &mapiOriginator
: NULL
), mapiRecipientList
, mapiAttachmentList
, &mapiMsg
);
292 ulRet
= mapi
.MAPISendMail(hSession
, 0, &mapiMsg
, gMapiFlags
, 0);
294 mapi
.MAPILogoff(hSession
, 0, 0, 0);
297 catch (const std::runtime_error
&
298 #if OSL_DEBUG_LEVEL > 0
303 OSL_ENSURE(false, ex
.what());
308 #if OSL_DEBUG_LEVEL > 2
311 std::ostringstream oss
;
313 if (gFrom
.length() > 0)
314 oss
<< "--from" << " " << gFrom
<< std::endl
;
316 if (gSubject
.length() > 0)
317 oss
<< "--subject" << " " << gSubject
<< std::endl
;
319 if (gBody
.length() > 0)
320 oss
<< "--body" << " " << gBody
<< std::endl
;
322 StringListIterator_t iter
= gTo
.begin();
323 StringListIterator_t iter_end
= gTo
.end();
324 for (/**/;iter
!= iter_end
; ++iter
)
325 oss
<< "--to" << " " << *iter
<< std::endl
;
328 iter_end
= gCc
.end();
329 for (/**/;iter
!= iter_end
; ++iter
)
330 oss
<< "--cc" << " " << *iter
<< std::endl
;
333 iter_end
= gBcc
.end();
334 for (/**/;iter
!= iter_end
; ++iter
)
335 oss
<< "--bcc" << " " << *iter
<< std::endl
;
337 iter
= gAttachments
.begin();
338 iter_end
= gAttachments
.end();
339 for (/**/;iter
!= iter_end
; ++iter
)
340 oss
<< "--attach" << " " << *iter
<< std::endl
;
342 if (gMapiFlags
& MAPI_DIALOG
)
343 oss
<< "--mapi-dialog" << std::endl
;
345 if (gMapiFlags
& MAPI_LOGON_UI
)
346 oss
<< "--mapi-logon-ui" << std::endl
;
348 MessageBox(NULL
, oss
.str().c_str(), "Arguments", MB_OK
| MB_ICONINFORMATION
);