1 //=============================================================================
2 // File:       exampl04.cpp
3 // Contents:   Source code for Example 4 -- Parsing a multipart message
4 // Maintainer: Doug Sauder <dwsauder@fwb.gulf.net>
5 // WWW:        http://www.fwb.gulf.net/~dwsauder/mimepp.html
6 // $Revision: 1.5 $
7 // $Date: 1997/09/27 11:55:42 $
8 //
9 // Copyright (c) 1996, 1997 Douglas W. Sauder
10 // All rights reserved.
11 //
12 // IN NO EVENT SHALL DOUGLAS W. SAUDER BE LIABLE TO ANY PARTY FOR DIRECT,
13 // INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
14 // THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF DOUGLAS W. SAUDER
15 // HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16 //
17 // DOUGLAS W. SAUDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT
18 // NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
19 // PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"
20 // BASIS, AND DOUGLAS W. SAUDER HAS NO OBLIGATION TO PROVIDE MAINTENANCE,
21 // SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
22 //
23 //=============================================================================
24 
25 #include <stdlib.h>
26 #include <iostream>
27 #include <fstream.h>
28 #include "multipar.h"
29 
30 
main()31 int main()
32 {
33     // Initialize the library
34 
35     DwInitialize();
36 
37     // Read message from file
38 
39     DwString messageStr = "";
40     DwString line;
41     ifstream istrm("exampl04.txt");
42     while (DwTrue) {
43         getline(istrm, line);
44         if (istrm.eof()) {
45             break;
46         }
47         messageStr += line + DW_EOL;
48     }
49     istrm.close();
50 
51     // Create a DwMessage and parse it.  The DwMessage should be created on
52     // the free store, since it will be added to the MultipartMessage.
53 
54     DwMessage* msg = DwMessage::NewMessage(messageStr, 0);
55     msg->Parse();
56 
57     // Make sure it is a multipart message
58     // If is not a multipart message, we could create a BasicMessage instead,
59     // but we won't do that in this example.
60 
61     if (msg->Headers().ContentType().Type() != DwMime::kTypeMultipart) {
62         cerr << "Not a multipart message\n";
63         return 0;
64     }
65 
66     // Create a MultipartMessage
67 
68     MultipartMessage multipart(msg);
69 
70     // Open file stream for output
71 
72     ofstream ostrm("exampl04.out");
73 
74     // Print the header fields
75 
76     ostrm << "Type -> "    << multipart.TypeStr()    << "\n";
77     ostrm << "Subtype -> " << multipart.SubtypeStr() << "\n";
78     ostrm << "Date -> "    << multipart.DateStr()    << "\n";
79     ostrm << "From -> "    << multipart.From()       << "\n";
80     ostrm << "To -> "      << multipart.To()         << "\n";
81     ostrm << "Cc -> "      << multipart.Cc()         << "\n";
82     ostrm << "Bcc -> "     << multipart.Bcc()        << "\n";
83     ostrm << "Subject -> " << multipart.Subject()    << "\n";
84 
85     // Read the body parts and print them
86 
87     MultipartBodyPart part;
88     DwString body;
89     int numParts = multipart.NumberOfParts();
90     for (int idx=0; idx < numParts; ++idx) {
91         multipart.BodyPart(idx, part);
92         ostrm << "\nBody part number " << idx << "\n";
93         ostrm << "Type -> " << part.TypeStr() << "\n";
94         ostrm << "Subtype -> " << part.SubtypeStr() << "\n";
95         ostrm << "Content transfer encoding -> "
96             << part.ContentTransferEncodingStr() << "\n";
97         ostrm << "Content description -> "
98             << part.ContentDescription() << "\n";
99         int cte = part.ContentTransferEncoding();
100         if (cte == DwMime::kCteBase64) {
101             DwDecodeBase64(part.Body(), body);
102             ostrm << "Body (decoded) ->\n" << body << "\n";
103         }
104         else if (cte == DwMime::kCteQuotedPrintable) {
105             DwDecodeQuotedPrintable(part.Body(), body);
106             ostrm << "Body (decoded) ->\n" << body << "\n";
107         }
108         else {
109             body = part.Body();
110             ostrm << "Body ->\n" << body << "\n";
111         }
112     }
113     return 0;
114 }
115 
116