Chilkat HOME ASP Visual Basic VB.NET C# Visual C++ C MFC Delphi FoxPro Java Perl PHP Python Ruby SQL Server VBScript
|
MIME Parsing and Manipulation in C++
This C++ sample program demonstrates how to parse and modify a MIME message. // Example.cpp : Defines the entry point for the application. // #define _CRTDBG_MAP_ALLOC #include "stdafx.h" #include <stdio.h> #include <crtdbg.h> #include "C:/ck2000/components/ChilkatLib/CkByteData.h" #include "C:/ck2000/components/ChilkatLib/CkString.h" #include "C:/ck2000/components/ChilkatLib/CkMime.h" void DoTest(void) { // This example demonstrates how to do MIME parsing and manipulation // in Visual C++. // It loads a MIME text file containing the raw MIME text of an // HTML email with an embedded image, as well as a GIF image attachment. // It then accesses various parts of the MIME, and modifies parts. CkMime *mime = new CkMime; // Before doing anything, unlock the class. This call is only necessary for // the first object instance created. // 30-day trial unlock codes can be obtained at http://www.chilkatsoft.com/register30.asp mime->UnlockComponent("unlock-code"); // Load the MIME. mime->LoadMimeFile("sampleMime.txt"); // Get some header fields. CkString str; mime->GetHeaderField("Subject",str); printf("subject = [%s]\n",str.getString()); str.clear(); // GetHeaderField appends to the str, so clear it first. mime->GetHeaderField("Content-Type",str); printf("content-type = [%s]\n",str.getString()); // This particular MIME text is nested. The outermost layer // is multipart/mixed. Inside of that there is a multipart/related // MIME message, and inside of that there is a multpart/alternative. // Get the GIF file attachment, which is the 2nd part of the multipart/mixed // message. CkMime *gifPart = mime->GetPart(1); // The first part is at index 0. // Get the GIF byte data, decoded from Base64. CkByteData gifData; gifPart->GetBodyBinary(gifData); // Do what you wish with the data... // .... const unsigned char *byteData = gifData.getData(); //... // Now save it to a file. gifData.saveFile("p1.gif"); // We didn't have to get the GIF data as memory, but we could've simply // saved it to a file. gifPart->SaveBody("p2.gif"); delete gifPart; // Get the HTML and plain-text bodies. CkMime *mpRelated = mime->GetPart(0); CkMime *mpAlternative = mpRelated->GetPart(0); CkMime *plainTextPart = mpAlternative->GetPart(0); CkMime *htmlPart = mpAlternative->GetPart(1); str.clear(); plainTextPart->GetBodyDecoded(str); printf("plain text body:\n%s\n-----\n",str.getString()); CkString htmlBody; htmlPart->GetBodyDecoded(htmlBody); printf("HTML body:\n%s\n-----\n",htmlBody.getString()); // Modify the plain-text and HTML bodies, and save the MIME. plainTextPart->SetBodyFromPlainText("This is the NEW plain-text body"); htmlBody.replaceAllOccurances("This is a test HTML email","This is the NEW HTML"); htmlPart->SetBodyFromHtml(htmlBody.getString()); // Make sure we save from the root... mime->SaveMime("newMime.txt"); delete mpRelated; delete mpAlternative; delete plainTextPart; delete htmlPart; // Finally, let's find the filename of the attachment. gifPart = mime->GetPart(1); str.clear(); gifPart->get_Filename(str); printf("Attachment filename = [%s]\n",str.getString()); delete mime; return; } int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { // Redirect standard output to the file "stdout.txt" freopen("stdout.txt","w",stdout); DoTest(); return 0; }
|
Need a specific example? Send a request to support@chilkatsoft.com
© 2000-2008 Chilkat Software, Inc. All Rights Reserved.