comparison 3rdparty/vmime/examples/example4.cpp @ 0:a4671277546c tip

created the repository for the thymian project
author ferencd
date Tue, 17 Aug 2021 11:19:54 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:a4671277546c
1 //
2 // VMime library (http://www.vmime.org)
3 // Copyright (C) 2002-2013 Vincent Richard <vincent@vmime.org>
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 3 of
8 // the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 //
19 // Linking this library statically or dynamically with other modules is making
20 // a combined work based on this library. Thus, the terms and conditions of
21 // the GNU General Public License cover the whole combination.
22 //
23
24 //
25 // EXAMPLE DESCRIPTION:
26 // ====================
27 // This sample program demonstrate the use of the messageParser component
28 // to enumerate the text parts in a message.
29 //
30 // For more information, please visit:
31 // http://www.vmime.org/
32 //
33
34 #include <iostream>
35 #include <locale>
36 #include <clocale>
37
38 #include "vmime/vmime.hpp"
39 #include "vmime/platforms/posix/posixHandler.hpp"
40
41
42 int main()
43 {
44 std::cout << std::endl;
45
46 // Set the global C and C++ locale to the user-configured locale.
47 // The locale should use UTF-8 encoding for these tests to run successfully.
48 try
49 {
50 std::locale::global(std::locale(""));
51 }
52 catch (std::exception &)
53 {
54 std::setlocale(LC_ALL, "");
55 }
56
57 try
58 {
59 vmime::messageParser mp("<...MIME message content...>");
60
61 // Enumerate text parts
62 for (int i = 0 ; i < mp.getTextPartCount() ; ++i)
63 {
64 const vmime::textPart& part = *mp.getTextPartAt(i);
65
66 // Output content-type of the part
67 std::cout << part.getType().generate() << std::endl;
68
69 // text/html
70 if (part.getType().getSubType() == vmime::mediaTypes::TEXT_HTML)
71 {
72 const vmime::htmlTextPart& hp = dynamic_cast<const vmime::htmlTextPart&>(part);
73
74 // HTML text is in "hp.getText()"
75 // Corresponding plain text is in "hp.getPlainText()"
76
77 // Enumerate embedded objects (eg. images)
78 for (int j = 0 ; j < hp.getObjectCount() ; ++j)
79 {
80 const vmime::htmlTextPart::embeddedObject& obj = *hp.getObjectAt(j);
81
82 // Identifier (content-id or content-location) is in "obj.getId()"
83 // Object data is in "obj.getData()"
84 }
85 }
86 // text/plain
87 else
88 {
89 const vmime::textPart& tp = dynamic_cast<const vmime::textPart&>(part);
90
91 // Text is in "tp.getText()"
92 }
93 }
94 }
95 // VMime exception
96 catch (vmime::exception& e)
97 {
98 std::cout << "vmime::exception: " << e.what() << std::endl;
99 throw;
100 }
101 // Standard exception
102 catch (std::exception& e)
103 {
104 std::cout << "std::exception: " << e.what() << std::endl;
105 throw;
106 }
107
108 std::cout << std::endl;
109 }