1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
|
//
// VMime library (http://www.vmime.org)
// Copyright (C) 2002-2005 Vincent Richard <[email protected]>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of
// the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
#include <iostream>
#include "vmime/vmime.hpp"
#include "vmime/platforms/posix/posixHandler.hpp"
//
// Authentification handler
//
class my_auth : public vmime::messaging::authenticator
{
const vmime::messaging::authenticationInfos requestAuthInfos() const
{
vmime::string username, password;
std::cout << "Username: "; std::cout.flush();
std::cin >> username;
std::cout << "Password: "; std::cout.flush();
std::cin >> password;
return (vmime::messaging::authenticationInfos(username, password));
}
};
void printStructure(const vmime::messaging::structure& s, int level = 0)
{
for (int i = 1 ; i <= s.getCount() ; ++i)
{
const vmime::messaging::part& part = s[i];
for (int j = 0 ; j < level * 2 ; ++j)
std::cout << " ";
std::cout << part.getNumber() << ". "
<< part.getType().generate()
<< " [" << part.getSize() << " byte(s)]"
<< std::endl;
printStructure(part.getStructure(), level + 1);
}
}
int main()
{
// VMime initialization
vmime::platformDependant::setHandler<vmime::platforms::posix::posixHandler>();
//
// Test the new enumeration system for encoders
//
#if 1
vmime::encoderFactory* ef = vmime::encoderFactory::getInstance();
std::cout << "Available encoders:" << std::endl;
for (int i = 0 ; i < ef->getEncoderCount() ; ++i)
{
const vmime::encoderFactory::registeredEncoder& enc = *ef->getEncoderAt(i);
std::cout << " * " << enc.getName() << std::endl;
vmime::encoder* e = enc.create();
std::vector <vmime::string> props = e->getAvailableProperties();
for (std::vector <vmime::string>::const_iterator it = props.begin() ; it != props.end() ; ++it)
std::cout << " - " << *it << std::endl;
delete (e);
}
#endif
// ======================================================================================
//
// Test the new enumeration system for messaging services
//
#if 1
vmime::messaging::serviceFactory* sf = vmime::messaging::serviceFactory::getInstance();
std::cout << "Available messaging services:" << std::endl;
for (int i = 0 ; i < sf->getServiceCount() ; ++i)
{
const vmime::messaging::serviceFactory::registeredService& serv = *sf->getServiceAt(i);
std::cout << " * " << serv.getName() << " (" << serv.getInfos().getDefaultPort() << ")" << std::endl;
std::vector <vmime::string> props = serv.getInfos().getAvailableProperties();
for (std::vector <vmime::string>::const_iterator it = props.begin() ; it != props.end() ; ++it)
std::cout << " - " << serv.getInfos().getPropertyPrefix() + *it << std::endl;
}
#endif
vmime::messaging::session sess;
sess.getProperties()["store.protocol"] = "imap";
sess.getProperties()["transport.protocol"] = "smtp";
my_auth auth;
try
{
//
// Test the sending of a message
//
#if 0
// Transport protocol configuration
vmime::messaging::transport* tr = sess.getTransport();
//sess.getProperties()[tr->getInfos().getPropertyPrefix() + "auth.username"] = "username";
//sess.getProperties()[tr->getInfos().getPropertyPrefix() + "auth.password"] = "password";
sess.getProperties()[tr->getInfos().getPropertyPrefix() + "server.address"] = "smtp.mydomain.com";
//sess.getProperties()[tr->getInfos().getPropertyPrefix() + "options.need-authentification"] = true;
// Connection
tr->connect();
// Expeditor
vmime::mailbox from("[email protected]");
// Recipients list
vmime::mailboxList to;
to.appendMailbox(new vmime::mailbox("[email protected]"));
to.appendMailbox(new vmime::mailbox("[email protected]"));
std::istringstream iss("[MESSAGE DATA: HEADER + BODY]");
tr->send(from, to, iss);
// Note: you could also write this:
// vmime::message msg;
// ...
// tr->send(&msg);
tr->disconnect();
#endif
//
// Test the access to a mail store
//
#if 1
// If no authenticator is given in argument to getStore(), a default one
// is used. Its behaviour is to get the user credentials from the
// session properties "auth.username" and "auth.password".
vmime::messaging::store* st = sess.getStore(&auth);
// Store protocol configuration
//sess.getProperties()[st->getInfos().getPropertyPrefix() + "auth.username"] = "username";
//sess.getProperties()[st->getInfos().getPropertyPrefix() + "auth.password"] = "password";
sess.getProperties()[st->getInfos().getPropertyPrefix() + "server.address"] = "imap.mydomain.com";
//sess.getProperties()[st->getInfos().getPropertyPrefix() + "server.port"] = 110;
//sess.getProperties()[st->getInfos().getPropertyPrefix() + "server.socket-factory"] = "default";
//sess.getProperties()[st->getInfos().getPropertyPrefix() + "options.apop"] = false;
//sess.getProperties()[st->getInfos().getPropertyPrefix() + "options.apop.fallback"] = true;
// Connection
st->connect();
// Open the default folder in this store
vmime::messaging::folder* f = st->getDefaultFolder();
f->open(vmime::messaging::folder::MODE_READ_WRITE);
std::cout << f->getMessageCount() << " message(s) in your inbox" << std::endl;
// Get a pointer to the first message
vmime::messaging::message* m = f->getMessage(1);
// To fetch the header
f->fetchMessage(m, vmime::messaging::folder::FETCH_ENVELOPE |
vmime::messaging::folder::FETCH_CONTENT_INFO);
// To retrieve the whole message
std::ostringstream oss;
vmime::utility::outputStreamAdapter out(oss);
m->extract(out);
// To fetch the header
f->fetchMessage(m, vmime::messaging::folder::FETCH_ENVELOPE |
vmime::messaging::folder::FETCH_CONTENT_INFO |
vmime::messaging::folder::FETCH_STRUCTURE |
vmime::messaging::folder::FETCH_SIZE |
//vmime::messaging::folder::FETCH_FULL_HEADER |
vmime::messaging::folder::FETCH_SIZE |
vmime::messaging::folder::FETCH_FLAGS |
vmime::messaging::folder::FETCH_UID);
// Print structure
std::cout << "STRUCTURE:" << std::endl;
std::cout << "==========" << std::endl;
printStructure(m->getStructure());
std::cout << std::endl;
std::cout << "Size = " << m->getSize() << " byte(s)" << std::endl;
std::cout << "UID = " << m->getUniqueId() << std::endl;
std::cout << std::endl;
std::cout << "ENVELOPE:" << std::endl;
std::cout << "=========" << std::endl;
try { std::cout << m->getHeader().From().generate() << std::endl; } catch (...) { }
try { std::cout << m->getHeader().To().generate() << std::endl; } catch (...) { }
try { std::cout << m->getHeader().Date().generate() << std::endl; } catch (...) { }
try { std::cout << m->getHeader().Subject().generate() << std::endl; } catch (...) { }
std::cout << std::endl;
std::cout << "FULL HEADER:" << std::endl;
std::cout << "============" << std::endl;
std::cout << m->getHeader().generate() << std::endl;
std::cout << std::endl;
std::cout << "=========================================================" << std::endl;
vmime::utility::outputStreamAdapter out2(std::cout);
m->extractPart(m->getStructure()[1][2][1], out2, NULL); //, 0, 10);
std::cout << "=========================================================" << std::endl;
std::cout << std::endl;
std::cout << "=========================================================" << std::endl;
m->fetchPartHeader(m->getStructure()[1][2][1]);
std::cout << m->getStructure()[1][2][1].getHeader().generate() << std::endl;
std::cout << "=========================================================" << std::endl;
// Flags manipulation
std::cout << "Flags = " << m->getFlags() << std::endl;
m->setFlags(vmime::messaging::message::FLAG_REPLIED, vmime::messaging::message::FLAG_MODE_ADD);
std::cout << "Flags = " << m->getFlags() << std::endl;
m->setFlags(vmime::messaging::message::FLAG_REPLIED, vmime::messaging::message::FLAG_MODE_REMOVE);
std::cout << "Flags = " << m->getFlags() << std::endl;
f->setMessageFlags(m->getNumber(), m->getNumber(), vmime::messaging::message::FLAG_REPLIED, vmime::messaging::message::FLAG_MODE_ADD);
std::cout << "Flags = " << m->getFlags() << std::endl;
f->setMessageFlags(m->getNumber(), m->getNumber(), vmime::messaging::message::FLAG_REPLIED, vmime::messaging::message::FLAG_MODE_REMOVE);
std::cout << "Flags = " << m->getFlags() << std::endl;
std::cout << "=========================================================" << std::endl;
// Append message
/*
std::istringstream iss(
"From: me@localhost\r\n"
"To: you@localhost\r\n"
"Subject: Message Text\r\n"
"\r\n"
"This is a test message...\r\n"
"Bye bye!\r\n"
);
f->addMessage(iss, iss.str().size());
*/
/*
// Folder renaming
{
vmime::messaging::folder* f = st->getFolder(vmime::messaging::folder::path("c"));
f->rename(vmime::messaging::folder::path("c2"));
delete (f);
vmime::messaging::folder* g = st->getFolder(vmime::messaging::folder::path("c2"));
g->rename(vmime::messaging::folder::path("c"));
delete (g);
}
*/
/*
// Message copy
{
vmime::messaging::folder* g = st->getFolder(vmime::messaging::folder::path("TEMP"));
if (!g->exists())
g->create(vmime::messaging::folder::TYPE_CONTAINS_MESSAGES);
f->copyMessages(g->getFullPath());
delete (g);
}
*/
delete (m);
f->close(true);
delete (f);
st->disconnect();
delete (st);
#endif
}
catch (vmime::exceptions::authentication_error& e)
{
std::cout << "vmime::authentication_error: " << e.what() << std::endl
<< "Response is: '" << e.response() << "'." << std::endl;
throw;
}
catch (vmime::exceptions::command_error& e)
{
std::cout << "vmime::command_error: " << e.what() << std::endl
<< "Response is: '" << e.response() << "'." << std::endl;
throw;
}
catch (vmime::exception& e)
{
std::cout << "vmime::exception: " << e.what() << std::endl;
throw;
}
catch (std::exception& e)
{
std::cout << "std::exception: " << e.what() << std::endl;
throw;
}
}
|