annotate 3rdparty/vmime/examples/example3.cpp @ 0:a4671277546c tip

created the repository for the thymian project
author ferencd
date Tue, 17 Aug 2021 11:19:54 +0200
parents
children
rev   line source
ferencd@0 1 //
ferencd@0 2 // VMime library (http://www.vmime.org)
ferencd@0 3 // Copyright (C) 2002-2013 Vincent Richard <vincent@vmime.org>
ferencd@0 4 //
ferencd@0 5 // This program is free software; you can redistribute it and/or
ferencd@0 6 // modify it under the terms of the GNU General Public License as
ferencd@0 7 // published by the Free Software Foundation; either version 3 of
ferencd@0 8 // the License, or (at your option) any later version.
ferencd@0 9 //
ferencd@0 10 // This program is distributed in the hope that it will be useful,
ferencd@0 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
ferencd@0 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
ferencd@0 13 // General Public License for more details.
ferencd@0 14 //
ferencd@0 15 // You should have received a copy of the GNU General Public License along
ferencd@0 16 // with this program; if not, write to the Free Software Foundation, Inc.,
ferencd@0 17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
ferencd@0 18 //
ferencd@0 19 // Linking this library statically or dynamically with other modules is making
ferencd@0 20 // a combined work based on this library. Thus, the terms and conditions of
ferencd@0 21 // the GNU General Public License cover the whole combination.
ferencd@0 22 //
ferencd@0 23
ferencd@0 24 //
ferencd@0 25 // EXAMPLE DESCRIPTION:
ferencd@0 26 // ====================
ferencd@0 27 // This sample program demonstrate the use of the messageBuilder component
ferencd@0 28 // to build a complex message (HTML content, plain text and embedded image).
ferencd@0 29 //
ferencd@0 30 // For more information, please visit:
ferencd@0 31 // http://www.vmime.org/
ferencd@0 32 //
ferencd@0 33
ferencd@0 34 #include <iostream>
ferencd@0 35 #include <locale>
ferencd@0 36 #include <clocale>
ferencd@0 37
ferencd@0 38 #include "vmime/vmime.hpp"
ferencd@0 39 #include "vmime/platforms/posix/posixHandler.hpp"
ferencd@0 40
ferencd@0 41
ferencd@0 42 int main()
ferencd@0 43 {
ferencd@0 44 std::cout << std::endl;
ferencd@0 45
ferencd@0 46 // Set the global C and C++ locale to the user-configured locale.
ferencd@0 47 // The locale should use UTF-8 encoding for these tests to run successfully.
ferencd@0 48 try
ferencd@0 49 {
ferencd@0 50 std::locale::global(std::locale(""));
ferencd@0 51 }
ferencd@0 52 catch (std::exception &)
ferencd@0 53 {
ferencd@0 54 std::setlocale(LC_ALL, "");
ferencd@0 55 }
ferencd@0 56
ferencd@0 57 try
ferencd@0 58 {
ferencd@0 59 vmime::messageBuilder mb;
ferencd@0 60
ferencd@0 61 // Fill in the basic fields
ferencd@0 62 mb.setExpeditor(vmime::mailbox("me@somewhere.com"));
ferencd@0 63
ferencd@0 64 vmime::addressList to;
ferencd@0 65 to.appendAddress(vmime::make_shared <vmime::mailbox>("you@elsewhere.com"));
ferencd@0 66
ferencd@0 67 mb.setRecipients(to);
ferencd@0 68
ferencd@0 69 vmime::addressList bcc;
ferencd@0 70 bcc.appendAddress(vmime::make_shared <vmime::mailbox>("you-bcc@nowhere.com"));
ferencd@0 71
ferencd@0 72 mb.setBlindCopyRecipients(bcc);
ferencd@0 73
ferencd@0 74 mb.setSubject(vmime::text("My first message generated with vmime::messageBuilder"));
ferencd@0 75
ferencd@0 76 // Set the content-type to "text/html"
ferencd@0 77 mb.constructTextPart(vmime::mediaType
ferencd@0 78 (vmime::mediaTypes::TEXT, vmime::mediaTypes::TEXT_HTML));
ferencd@0 79
ferencd@0 80 // Fill in the text part: the message is available in two formats: HTML and plain text.
ferencd@0 81 // HTML text part also includes an inline image (embedded into the message).
ferencd@0 82 vmime::htmlTextPart& textPart = *vmime::dynamicCast <vmime::htmlTextPart>(mb.getTextPart());
ferencd@0 83
ferencd@0 84 // -- embed an image (the returned "CID" (content identifier) is used to reference
ferencd@0 85 // -- the image into HTML content).
ferencd@0 86 vmime::shared_ptr <vmime::utility::fileSystemFactory> fs =
ferencd@0 87 vmime::platform::getHandler()->getFileSystemFactory();
ferencd@0 88
ferencd@0 89 vmime::shared_ptr <vmime::utility::file> imageFile =
ferencd@0 90 fs->create(fs->stringToPath("/path/to/image.jpg"));
ferencd@0 91
ferencd@0 92 vmime::shared_ptr <vmime::utility::fileReader> fileReader =
ferencd@0 93 imageFile->getFileReader();
ferencd@0 94
ferencd@0 95 vmime::shared_ptr <vmime::contentHandler> imageCts =
ferencd@0 96 vmime::make_shared <vmime::streamContentHandler>
ferencd@0 97 (fileReader->getInputStream(), imageFile->getLength());
ferencd@0 98
ferencd@0 99 vmime::shared_ptr <const vmime::htmlTextPart::embeddedObject> obj = textPart.addObject
ferencd@0 100 (imageCts, vmime::mediaType(vmime::mediaTypes::IMAGE, vmime::mediaTypes::IMAGE_JPEG));
ferencd@0 101
ferencd@0 102 // -- message text
ferencd@0 103 textPart.setText(vmime::make_shared <vmime::stringContentHandler>
ferencd@0 104 (vmime::string("This is the <b>HTML text</b>.<br/>"
ferencd@0 105 "<img src=\"") + obj->getReferenceId() + vmime::string("\"/>")));
ferencd@0 106 textPart.setPlainText(vmime::make_shared <vmime::stringContentHandler>
ferencd@0 107 ("This is the plain text (without HTML formatting)."));
ferencd@0 108
ferencd@0 109 // Construction
ferencd@0 110 vmime::shared_ptr <vmime::message> msg = mb.construct();
ferencd@0 111
ferencd@0 112 // Raw text generation
ferencd@0 113 vmime::string dataToSend = msg->generate();
ferencd@0 114
ferencd@0 115 std::cout << "Generated message:" << std::endl;
ferencd@0 116 std::cout << "==================" << std::endl;
ferencd@0 117 std::cout << std::endl;
ferencd@0 118 std::cout << dataToSend << std::endl;
ferencd@0 119 }
ferencd@0 120 // VMime exception
ferencd@0 121 catch (vmime::exception& e)
ferencd@0 122 {
ferencd@0 123 std::cout << "vmime::exception: " << e.what() << std::endl;
ferencd@0 124 throw;
ferencd@0 125 }
ferencd@0 126 // Standard exception
ferencd@0 127 catch (std::exception& e)
ferencd@0 128 {
ferencd@0 129 std::cout << "std::exception: " << e.what() << std::endl;
ferencd@0 130 throw;
ferencd@0 131 }
ferencd@0 132
ferencd@0 133 std::cout << std::endl;
ferencd@0 134 }
ferencd@0 135