comparison 3rdparty/vmime/examples/example2.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 messageBuilder component
28 // to build a simple message with an attachment.
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::messageBuilder mb;
60
61 // Fill in the basic fields
62 mb.setExpeditor(vmime::mailbox("me@somewhere.com"));
63
64 vmime::addressList to;
65 to.appendAddress(vmime::make_shared <vmime::mailbox>("you@elsewhere.com"));
66
67 mb.setRecipients(to);
68
69 vmime::addressList bcc;
70 bcc.appendAddress(vmime::make_shared <vmime::mailbox>("you-bcc@nowhere.com"));
71
72 mb.setBlindCopyRecipients(bcc);
73
74 mb.setSubject(vmime::text("My first message generated with vmime::messageBuilder"));
75
76 // Message body
77 mb.getTextPart()->setText(vmime::make_shared <vmime::stringContentHandler>(
78 "I'm writing this short text to test message construction " \
79 "with attachment, using the vmime::messageBuilder component."));
80
81 // Adding an attachment
82 vmime::shared_ptr <vmime::fileAttachment> a = vmime::make_shared <vmime::fileAttachment>
83 (
84 __FILE__, // full path to file
85 vmime::mediaType("application/octet-stream"), // content type
86 vmime::text("My first attachment") // description
87 );
88
89 a->getFileInfo().setFilename("example2.cpp");
90 a->getFileInfo().setCreationDate(vmime::datetime("30 Apr 2003 14:30:00 +0200"));
91
92 mb.attach(a);
93
94 // Construction
95 vmime::shared_ptr <vmime::message> msg = mb.construct();
96
97 // Raw text generation
98 vmime::string dataToSend = msg->generate();
99
100 std::cout << "Generated message:" << std::endl;
101 std::cout << "==================" << std::endl;
102 std::cout << std::endl;
103 std::cout << dataToSend << std::endl;
104 }
105 // VMime exception
106 catch (vmime::exception& e)
107 {
108 std::cout << "vmime::exception: " << e.what() << std::endl;
109 throw;
110 }
111 // Standard exception
112 catch (std::exception& e)
113 {
114 std::cout << "std::exception: " << e.what() << std::endl;
115 throw;
116 }
117
118 std::cout << std::endl;
119 }
120