Mercurial > thymian
comparison 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 |
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 complex message (HTML content, plain text and embedded image). | |
| 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 // Set the content-type to "text/html" | |
| 77 mb.constructTextPart(vmime::mediaType | |
| 78 (vmime::mediaTypes::TEXT, vmime::mediaTypes::TEXT_HTML)); | |
| 79 | |
| 80 // Fill in the text part: the message is available in two formats: HTML and plain text. | |
| 81 // HTML text part also includes an inline image (embedded into the message). | |
| 82 vmime::htmlTextPart& textPart = *vmime::dynamicCast <vmime::htmlTextPart>(mb.getTextPart()); | |
| 83 | |
| 84 // -- embed an image (the returned "CID" (content identifier) is used to reference | |
| 85 // -- the image into HTML content). | |
| 86 vmime::shared_ptr <vmime::utility::fileSystemFactory> fs = | |
| 87 vmime::platform::getHandler()->getFileSystemFactory(); | |
| 88 | |
| 89 vmime::shared_ptr <vmime::utility::file> imageFile = | |
| 90 fs->create(fs->stringToPath("/path/to/image.jpg")); | |
| 91 | |
| 92 vmime::shared_ptr <vmime::utility::fileReader> fileReader = | |
| 93 imageFile->getFileReader(); | |
| 94 | |
| 95 vmime::shared_ptr <vmime::contentHandler> imageCts = | |
| 96 vmime::make_shared <vmime::streamContentHandler> | |
| 97 (fileReader->getInputStream(), imageFile->getLength()); | |
| 98 | |
| 99 vmime::shared_ptr <const vmime::htmlTextPart::embeddedObject> obj = textPart.addObject | |
| 100 (imageCts, vmime::mediaType(vmime::mediaTypes::IMAGE, vmime::mediaTypes::IMAGE_JPEG)); | |
| 101 | |
| 102 // -- message text | |
| 103 textPart.setText(vmime::make_shared <vmime::stringContentHandler> | |
| 104 (vmime::string("This is the <b>HTML text</b>.<br/>" | |
| 105 "<img src=\"") + obj->getReferenceId() + vmime::string("\"/>"))); | |
| 106 textPart.setPlainText(vmime::make_shared <vmime::stringContentHandler> | |
| 107 ("This is the plain text (without HTML formatting).")); | |
| 108 | |
| 109 // Construction | |
| 110 vmime::shared_ptr <vmime::message> msg = mb.construct(); | |
| 111 | |
| 112 // Raw text generation | |
| 113 vmime::string dataToSend = msg->generate(); | |
| 114 | |
| 115 std::cout << "Generated message:" << std::endl; | |
| 116 std::cout << "==================" << std::endl; | |
| 117 std::cout << std::endl; | |
| 118 std::cout << dataToSend << std::endl; | |
| 119 } | |
| 120 // VMime exception | |
| 121 catch (vmime::exception& e) | |
| 122 { | |
| 123 std::cout << "vmime::exception: " << e.what() << std::endl; | |
| 124 throw; | |
| 125 } | |
| 126 // Standard exception | |
| 127 catch (std::exception& e) | |
| 128 { | |
| 129 std::cout << "std::exception: " << e.what() << std::endl; | |
| 130 throw; | |
| 131 } | |
| 132 | |
| 133 std::cout << std::endl; | |
| 134 } | |
| 135 |
