diff options
Diffstat (limited to 'doc/book/msg.tex')
-rw-r--r-- | doc/book/msg.tex | 133 |
1 files changed, 78 insertions, 55 deletions
diff --git a/doc/book/msg.tex b/doc/book/msg.tex index 50344b62..ce9d8a80 100644 --- a/doc/book/msg.tex +++ b/doc/book/msg.tex @@ -94,8 +94,8 @@ vmime::messageParser mp(msg); std::cout << "Message has " << mp.getAttachmentCount() << " attachment(s)" << std::endl; -for (int i = 0 ; i < mp.getAttachmentCount() ; ++i) -{ +for (int i = 0 ; i < mp.getAttachmentCount() ; ++i) { + vmime::shared_ptr <const vmime::attachment> att = mp.getAttachmentAt(i); std::cout << " - " << att->getType().generate() << std::endl; } @@ -104,13 +104,13 @@ for (int i = 0 ; i < mp.getAttachmentCount() ; ++i) std::cout << "Message has " << mp.getTextPartCount() << " text part(s)" << std::endl; -for (int i = 0 ; i < mp.getTextPartCount() ; ++i) -{ +for (int i = 0 ; i < mp.getTextPartCount() ; ++i) { + vmime::shared_ptr <const vmime::textPart> tp = mp.getTextPartAt(i); // text/html - if (tp->getType().getSubType() == vmime::mediaTypes::TEXT_HTML) - { + if (tp->getType().getSubType() == vmime::mediaTypes::TEXT_HTML) { + vmime::shared_ptr <const vmime::htmlTextPart> htp = vmime::dynamicCast <const vmime::htmlTextPart>(tp); @@ -118,18 +118,18 @@ for (int i = 0 ; i < mp.getTextPartCount() ; ++i) // Plain text is in tp->getPlainText() // Enumerate embedded objects - for (int j = 0 ; j < htp->getObjectCount() ; ++j) - { + for (int j = 0 ; j < htp->getObjectCount() ; ++j) { + vmime::shared_ptr <const vmime::htmlTextPart::embeddedObject> obj = htp->getObjectAt(j); // Identifier (Content-Id or Content-Location) is obj->getId() // Object data is in obj->getData() } - } + // text/plain or anything else - else - { + } else { + // Text is in tp->getText() } } @@ -172,8 +172,7 @@ hdr->appendField(subjectField); vmime::shared_ptr <vmime::headerField> fromField = hfFactory->create(vmime::fields::FROM); -fromField->setValue - (vmime::make_shared <vmime::mailbox>("[email protected]")); +fromField->setValue(vmime::make_shared <vmime::mailbox>("[email protected]")); hdr->appendField(fromField); // Append a 'To:' field @@ -190,8 +189,11 @@ toField->setValue(recipients); hdr->appendField(toField); // Set the body contents -bdy->setContents(vmime::make_shared <vmime::stringContentHandler> - ("This is the text of your message...")); +bdy->setContents( + vmime::make_shared <vmime::stringContentHandler>( + "This is the text of your message..." + ) +); // Output raw message data to standard output vmime::utility::outputStreamAdapter out(std::cout); @@ -207,19 +209,23 @@ previous example, using the {\vcode vmime::messageBuilder} object: \begin{lstlisting}[caption={Building a simple message using {\vcode vmime::messageBuilder}}] -try -{ +try { + vmime::messageBuilder mb; // Fill in some header fields and message body mb.setSubject(vmime::text("Message subject")); mb.setExpeditor(vmime::mailbox("[email protected]")); - mb.getRecipients().appendAddress - (vmime::make_shared <vmime::mailbox>("[email protected]")); + mb.getRecipients().appendAddress( + vmime::make_shared <vmime::mailbox>("[email protected]") + ); mb.getTextPart()->setCharset(vmime::charsets::ISO8859_15); - mb.getTextPart()->setText(vmime::make_shared <vmime::stringContentHandler> - ("This is the text of your message...")); + mb.getTextPart()->setText( + vmime::make_shared <vmime::stringContentHandler>( + "This is the text of your message..." + ) + ); // Message construction vmime::shared_ptr <vmime::message> msg = mb.construct(); @@ -227,15 +233,15 @@ try // Output raw message data to standard output vmime::utility::outputStreamAdapter out(std::cout); msg->generate(out); -} + // VMime exception -catch (vmime::exception& e) -{ +} catch (vmime::exception& e) { + std::cerr << "vmime::exception: " << e.what() << std::endl; -} + // Standard exception -catch (std::exception& e) -{ +} catch (std::exception& e) { + std::cerr << "std::exception: " << e.what() << std::endl; } \end{lstlisting} @@ -250,17 +256,17 @@ previous example to attach a file to the message: {\vcode vmime::messageBuilder}}] // Create an attachment vmime::shared_ptr <vmime::fileAttachment> att = - vmime::make_shared <vmime::fileAttachment> - ( - /* full path to file */ "/home/vincent/paris.jpg", - /* content type */ vmime::mediaType("image/jpeg), - /* description */ vmime::text("My holidays in Paris") + vmime::make_shared <vmime::fileAttachment>( + /* full path to file */ "/home/vincent/paris.jpg", + /* content type */ vmime::mediaType("image/jpeg), + /* description */ vmime::text("My holidays in Paris") ); // You can also set some infos about the file att->getFileInfo().setFilename("paris.jpg"); -att->getFileInfo().setCreationDate - (vmime::datetime("30 Apr 2003 14:30:00 +0200")); +att->getFileInfo().setCreationDate( + vmime::datetime("30 Apr 2003 14:30:00 +0200") +); // Add this attachment to the message mb.appendAttachment(att); @@ -283,14 +289,19 @@ using the {\vcode vmime::messageBuilder}}] // Fill in some header fields mb.setSubject(vmime::text("An HTML message")); mb.setExpeditor(vmime::mailbox("[email protected]")); -mb.getRecipients().appendAddress - (vmime::make_shared <vmime::mailbox>("[email protected]")); +mb.getRecipients().appendAddress( + vmime::make_shared <vmime::mailbox>("[email protected]") +); // Set the content-type to "text/html": a text part factory must be // available for the type you are using. The following code will make // the message builder construct the two text parts. -mb.constructTextPart(vmime::mediaType - (vmime::mediaTypes::TEXT, vmime::mediaTypes::TEXT_HTML)); +mb.constructTextPart( + vmime::mediaType( + vmime::mediaTypes::TEXT, + vmime::mediaTypes::TEXT_HTML + ) +); // Set contents of the text parts; the message is available in two formats: // HTML and plain text. The HTML format also includes an embedded image. @@ -306,12 +317,18 @@ const vmime::string id = textPart->addObject("<...image data...>", // -- Set the text textPart->setCharset(vmime::charsets::ISO8859_15); -textPart->setText(vmime::make_shared <vmime::stringContentHandler> - ("This is the <b>HTML text</b>, and the image:<br/>" - "<img src=\"") + id + vmime::string("\"/>")); - -textPart->setPlainText(vmime::make_shared <vmime::stringContentHandler> - ("This is the plain text.")); +textPart->setText( + vmime::make_shared <vmime::stringContentHandler>( + "This is the <b>HTML text</b>, and the image:<br/>" + "<img src=\"") + id + vmime::string("\"/>" + ) +); + +textPart->setPlainText( + vmime::make_shared <vmime::stringContentHandler>( + "This is the plain text." + ) +); \end{lstlisting} This will create a message having the following structure: @@ -336,11 +353,18 @@ vmime::shared_ptr <vmime::utility::file> imageFile = fs->create(fs->stringToPath("/path/to/image.jpg")); vmime::shared_ptr <vmime::contentHandler> imageCts = - vmime::make_shared <vmime::streamContentHandler> - (imageFile->getFileReader()->getInputStream(), imageFile->getLength()); + vmime::make_shared <vmime::streamContentHandler>( + imageFile->getFileReader()->getInputStream(), + imageFile->getLength() + ); -const vmime::string cid = textPart.addObject(imageCts, - vmime::mediaType(vmime::mediaTypes::IMAGE, vmime::mediaTypes::IMAGE_JPEG)); +const vmime::string cid = textPart.addObject( + imageCts, + vmime::mediaType( + vmime::mediaTypes::IMAGE, + vmime::mediaTypes::IMAGE_JPEG + ) +); \end{lstlisting} @@ -361,8 +385,8 @@ extract its contents to the standard output: \begin{lstlisting}[caption={Testing if a body part is an attachment}] vmime::shared_ptr <vmime::bodyPart> part; // suppose we have a body part -if (vmime::attachmentHelper::isBodyPartAnAttachment(part)) -{ +if (vmime::attachmentHelper::isBodyPartAnAttachment(part)) { + // The body part contains an attachment, get it vmime::shared_ptr <const vmime::attachment> attach = attachmentHelper::getBodyPartAttachment(part); @@ -394,11 +418,10 @@ vmime::shared_ptr <vmime::message> msg; // suppose we have a message // Create an attachment vmime::shared_ptr <vmime::fileAttachment> att = - vmime::make_shared <vmime::fileAttachment> - ( - /* full path to file */ "/home/vincent/paris.jpg", - /* content type */ vmime::mediaType("image/jpeg), - /* description */ vmime::text("My holidays in Paris") + vmime::make_shared <vmime::fileAttachment>( + /* full path to file */ "/home/vincent/paris.jpg", + /* content type */ vmime::mediaType("image/jpeg), + /* description */ vmime::text("My holidays in Paris") ); // Attach it to the message |