ferencd@0: // ferencd@0: // VMime library (http://www.vmime.org) ferencd@0: // Copyright (C) 2002-2013 Vincent Richard ferencd@0: // ferencd@0: // This program is free software; you can redistribute it and/or ferencd@0: // modify it under the terms of the GNU General Public License as ferencd@0: // published by the Free Software Foundation; either version 3 of ferencd@0: // the License, or (at your option) any later version. ferencd@0: // ferencd@0: // This program is distributed in the hope that it will be useful, ferencd@0: // but WITHOUT ANY WARRANTY; without even the implied warranty of ferencd@0: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ferencd@0: // General Public License for more details. ferencd@0: // ferencd@0: // You should have received a copy of the GNU General Public License along ferencd@0: // with this program; if not, write to the Free Software Foundation, Inc., ferencd@0: // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ferencd@0: // ferencd@0: // Linking this library statically or dynamically with other modules is making ferencd@0: // a combined work based on this library. Thus, the terms and conditions of ferencd@0: // the GNU General Public License cover the whole combination. ferencd@0: // ferencd@0: ferencd@0: // ferencd@0: // EXAMPLE DESCRIPTION: ferencd@0: // ==================== ferencd@0: // This sample program demonstrate the use of the messageBuilder component ferencd@0: // to build a complex message (HTML content, plain text and embedded image). ferencd@0: // ferencd@0: // For more information, please visit: ferencd@0: // http://www.vmime.org/ ferencd@0: // ferencd@0: ferencd@0: #include ferencd@0: #include ferencd@0: #include ferencd@0: ferencd@0: #include "vmime/vmime.hpp" ferencd@0: #include "vmime/platforms/posix/posixHandler.hpp" ferencd@0: ferencd@0: ferencd@0: int main() ferencd@0: { ferencd@0: std::cout << std::endl; ferencd@0: ferencd@0: // Set the global C and C++ locale to the user-configured locale. ferencd@0: // The locale should use UTF-8 encoding for these tests to run successfully. ferencd@0: try ferencd@0: { ferencd@0: std::locale::global(std::locale("")); ferencd@0: } ferencd@0: catch (std::exception &) ferencd@0: { ferencd@0: std::setlocale(LC_ALL, ""); ferencd@0: } ferencd@0: ferencd@0: try ferencd@0: { ferencd@0: vmime::messageBuilder mb; ferencd@0: ferencd@0: // Fill in the basic fields ferencd@0: mb.setExpeditor(vmime::mailbox("me@somewhere.com")); ferencd@0: ferencd@0: vmime::addressList to; ferencd@0: to.appendAddress(vmime::make_shared ("you@elsewhere.com")); ferencd@0: ferencd@0: mb.setRecipients(to); ferencd@0: ferencd@0: vmime::addressList bcc; ferencd@0: bcc.appendAddress(vmime::make_shared ("you-bcc@nowhere.com")); ferencd@0: ferencd@0: mb.setBlindCopyRecipients(bcc); ferencd@0: ferencd@0: mb.setSubject(vmime::text("My first message generated with vmime::messageBuilder")); ferencd@0: ferencd@0: // Set the content-type to "text/html" ferencd@0: mb.constructTextPart(vmime::mediaType ferencd@0: (vmime::mediaTypes::TEXT, vmime::mediaTypes::TEXT_HTML)); ferencd@0: ferencd@0: // Fill in the text part: the message is available in two formats: HTML and plain text. ferencd@0: // HTML text part also includes an inline image (embedded into the message). ferencd@0: vmime::htmlTextPart& textPart = *vmime::dynamicCast (mb.getTextPart()); ferencd@0: ferencd@0: // -- embed an image (the returned "CID" (content identifier) is used to reference ferencd@0: // -- the image into HTML content). ferencd@0: vmime::shared_ptr fs = ferencd@0: vmime::platform::getHandler()->getFileSystemFactory(); ferencd@0: ferencd@0: vmime::shared_ptr imageFile = ferencd@0: fs->create(fs->stringToPath("/path/to/image.jpg")); ferencd@0: ferencd@0: vmime::shared_ptr fileReader = ferencd@0: imageFile->getFileReader(); ferencd@0: ferencd@0: vmime::shared_ptr imageCts = ferencd@0: vmime::make_shared ferencd@0: (fileReader->getInputStream(), imageFile->getLength()); ferencd@0: ferencd@0: vmime::shared_ptr obj = textPart.addObject ferencd@0: (imageCts, vmime::mediaType(vmime::mediaTypes::IMAGE, vmime::mediaTypes::IMAGE_JPEG)); ferencd@0: ferencd@0: // -- message text ferencd@0: textPart.setText(vmime::make_shared ferencd@0: (vmime::string("This is the HTML text.
" ferencd@0: "getReferenceId() + vmime::string("\"/>"))); ferencd@0: textPart.setPlainText(vmime::make_shared ferencd@0: ("This is the plain text (without HTML formatting).")); ferencd@0: ferencd@0: // Construction ferencd@0: vmime::shared_ptr msg = mb.construct(); ferencd@0: ferencd@0: // Raw text generation ferencd@0: vmime::string dataToSend = msg->generate(); ferencd@0: ferencd@0: std::cout << "Generated message:" << std::endl; ferencd@0: std::cout << "==================" << std::endl; ferencd@0: std::cout << std::endl; ferencd@0: std::cout << dataToSend << std::endl; ferencd@0: } ferencd@0: // VMime exception ferencd@0: catch (vmime::exception& e) ferencd@0: { ferencd@0: std::cout << "vmime::exception: " << e.what() << std::endl; ferencd@0: throw; ferencd@0: } ferencd@0: // Standard exception ferencd@0: catch (std::exception& e) ferencd@0: { ferencd@0: std::cout << "std::exception: " << e.what() << std::endl; ferencd@0: throw; ferencd@0: } ferencd@0: ferencd@0: std::cout << std::endl; ferencd@0: } ferencd@0: