September 12, 2009

Scan/Replace Text in Email Body in C++

Filed under: VC++, email — Tags: , , — admin @ 6:37 am

This example may help C++ programmers needing to scan email bodies for strings and automatically replace:

void EmailBodyExample(void)
    {
    CkEmail email;

    // Set the plain-text and HTML alternative bodies.
    // Note: Because we're using literal strings, the strings passed
    // to these methods are ANSI strings (i.e. they are 1-byte per char
    // in this case).
    email.AddPlainTextAlternativeBody("á, é, í, ó, ú, abc");
    email.AddHtmlAlternativeBody("<html><body><p>á, é, í, ó, ú, abc</p></body></html>");

    // Suppose we don't know the language or charset of the email?
    // Maybe it's Chinese, perhaps it's Hebrew...
    // You can get the body text in utf-8 (the multibyte encoding of Unicode)
    // for all languages.  Here's how to do it:
    CkString strPlainTextBody;
    CkString strHtmlBody;

    // Get the email bodies into the CkString objects:
    email.GetPlainTextBody(strPlainTextBody);
    email.GetHtmlBody(strHtmlBody);

    // Now get the utf-8 (null-terminated) strings:
    const char *strUtf8PlainTextBody = strPlainTextBody.getUtf8();
    const char *strUtf8HtmlBody = strHtmlBody.getUtf8();

    // Suppose you want to modify the body and replace it?
    // Let's replace "abc" with "123".
    // One way to do it is like this:
    strPlainTextBody.replaceAllOccurances("abc","123");

    // Tell the email object that "const char *" arguments will point to utf-8,
    // not ANSI:
    email.put_Utf8(true);

    // Pass the modified utf-8 string.
    // AddPlainTextAlternativeBody will replace the existing plain-text
    // body if it already exists.
    email.AddPlainTextAlternativeBody(strPlainTextBody.getUtf8());

    // Another way to do it:
    char *tmpBuf = new char[strHtmlBody.getSizeUtf8()+1];   // Add 1 for the terminating null
    strcpy(tmpBuf,strUtf8HtmlBody);
    char *abc = strstr(tmpBuf,”abc”);
    if (abc)
	{
	*abc = ‘1′; abc++;
	*abc = ‘2′; abc++;
	*abc = ‘3′;
	}
    email.AddHtmlAlternativeBody(tmpBuf);

    delete [] tmpBuf;

    // Save the email and examine it using a text editor or email client..
    email.SaveEml(”out.eml”);
    }

November 19, 2008

Utf8 C++ property allows for utf-8 or ANSI “const char *”

Filed under: VC++ — Tags: , , , — admin @ 3:34 pm

All Chilkat C++ classes have a Utf8 property. For example:

class CkEmail : public CkObject
{
    public:

	CkEmail();
	virtual ~CkEmail();

...
	bool get_Utf8(void) const;
	void put_Utf8(bool b);

...
	const char *addFileAttachment(const char *fileName);
...
};

The Utf8 property controls how the bytes pointed by “const char *” arguments are interpreted. By default, “const char *” strings are interpreted as ANSI bytes. If the Utf8 property is set to true by calling put_Utf8(true), then “const char *” inputs are interpreted as utf-8. This allows any application to pass either ANSI or utf-8 strings to any Chilkat method.

The Utf8 property also controls whether utf-8 or ANSI strings are returned by methods that return a “const char *”.

October 1, 2008

Zip with Unicode Filenames (utf-8)

Filed under: unicode, zip — Tags: , , — admin @ 12:36 pm

New examples demonstrating how to create a Zip archive using Unicode filenames:

ASP: Create Zip with utf-8 Filenames (Unicode filenames)
SQL Server: Create Zip with utf-8 Filenames (Unicode filenames)
C#: Create Zip with utf-8 Filenames (Unicode filenames)
C++: Create Zip with utf-8 Filenames (Unicode filenames)
MFC: Create Zip with utf-8 Filenames (Unicode filenames)
C: Create Zip with utf-8 Filenames (Unicode filenames)
Delphi: Create Zip with utf-8 Filenames (Unicode filenames)
Visual FoxPro: Create Zip with utf-8 Filenames (Unicode filenames)
Java: Create Zip with utf-8 Filenames (Unicode filenames)
Perl: Create Zip with utf-8 Filenames (Unicode filenames)
PHP: Create Zip with utf-8 Filenames (Unicode filenames)
Python: Create Zip with utf-8 Filenames (Unicode filenames)
Ruby: Create Zip with utf-8 Filenames (Unicode filenames)
VB.NET: Create Zip with utf-8 Filenames (Unicode filenames)
Visual Basic: Create Zip with utf-8 Filenames (Unicode filenames)
VBScript: Create Zip with utf-8 Filenames (Unicode filenames)