Monday 25 June 2012

how to create a bounced mail handler.

Visual C++ sample source code showing how to create a bounced mail handler.

#define _CRTDBG_MAP_ALLOC
#include "stdafx.h"
#include <stdio.h>
#include <crtdbg.h>
#include "C:/ck2000/components/ChilkatLib/CkString.h"
#include "C:/ck2000/components/ChilkatLib/CkMailMan.h"
#include "C:/ck2000/components/ChilkatLib/CkEmail.h"
#include "C:/ck2000/components/ChilkatLib/CkEmailBundle.h"
#include "C:/ck2000/components/ChilkatLib/CkSettings.h"
#include "C:/ck2000/components/ChilkatLib/CkBounce.h"

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
    freopen("stdout.txt","w",stdout);

    // Create a mailman for reading mail.
    CkMailMan *mailman = new CkMailMan;
    mailman->UnlockComponent("MailUnlockCode");

    // Set the POP3 server and login credentials.
    mailman->put_MailHost("mail.chilkatsoft.com");
    mailman->put_PopUsername("login");
    mailman->put_PopPassword("password");

    CkBounce *bounce = new CkBounce;
    bounce->UnlockComponent("BounceUnlockCode");

    // Download the entire mailbox, leaving it on the POP3 server.
    CkEmailBundle *bundle = mailman->CopyMail();
    if (bundle)
        {
        // Loop over the messages.
        int i;
        int n = bundle->get_MessageCount();
        for (i=0; i<n; i++)
        {
        CkEmail *email = bundle->GetEmail(i);

        int bounceType;
        CkString bounceAddr;
        CkString bounceData;
        bounce->CheckEmail(*email,bounceAddr,bounceData,bounceType);

        if (bounceType == 0)
            {
            // Not a bounce, do nothing.
            mailman->DeleteEmail(email);
            }
        else if (bounceType == 1)
            {
            printf("Hard Bounce (%s)\n",bounceAddr.getString());
            }
        else if (bounceType == 2)
            {
            printf("Soft Bounce (%s)\n",bounceAddr.getString());
            }
        else if (bounceType == 3)
            {
            printf("General Bounce, no email address available\n");
            }
        else if (bounceType == 4)
            {
            printf("General Bounce (%s)\n",bounceAddr.getString());
            }
        else if (bounceType == 5)
            {
            printf("Mail Blocked (%s)\n",bounceAddr.getString());
            }
        else if (bounceType == 6)
            {
            printf("Auto-Reply (%s)\n",bounceAddr.getString());
            }
        else if (bounceType == 7)
            {
            printf("Transient Message (%s)\n",bounceAddr.getString());
            }
        else if (bounceType == 8)
            {
            printf("Subscribe Request (%s)\n",bounceAddr.getString());
            }
        else if (bounceType == 9)
            {
            printf("Unsubscribe Request (%s)\n",bounceAddr.getString());
            }
        else if (bounceType == 10)
            {
            printf("Virus Notification (%s)\n",bounceAddr.getString());
            }
        else if (bounceType == 11)
            {
            printf("Suspected Bounce!\n");
            }

        delete email;
        }

        // Save the entire bundle of emails as XML.
        bundle->SaveXml("bundle.xml");
        }

    delete bounce;
    delete bundle;
    delete mailman;

    // Only necessary if checking for memory leaks.
    // Only call just before exiting the program.
     CkSettings::cleanupMemory();

    _CrtDumpMemoryLeaks();

    return 0;
}

0 comments:

Post a Comment