C Program to extract string from the body of the mail using left and right boundaries - MAPI protocol

MAPI mapi = 0;

Action()

{

long logfile;

long fromfile;

char *CStr; //variable in which the response header will be saved

char *LB = "personal "; //Left Boundary of Session ID

char *RB = "until "; //Right Boundary of Session ID

char *CorrParam = NULL; //variable in which Session ID will be saved

char *start, *end;

 

int rc;

int loopTime;

int loopCount;

char * szSubj; 

int iCmpResult;

int ifromResult;

int iEmailCounter = 0;

char * szMyHost; 

char * message;

char * fromid;


//Logon to Exchange with proper credentials 

mapi_logon_ex(&mapi,"Logon", 

"ProfileName=Outlook",

"ProfilePass=",

LAST);

 

lr_think_time(5);

lr_start_transaction("MAILING");

loopTime=0;

do {

mapi_set_property_sz_ex(&mapi,"Message ID", ""); 

loopCount =0;

lr_output_message( "Never Loop");

// Preview all mails in Mailbox

do {

// Peek at next mail (don't mark it as read) 

rc = mapi_read_next_mail_ex(&mapi,

"NextMail",

"Show=all", // 

"Peek=True", // [Optional: default = False]

"Type=IPM",

"Save=body",

LAST); 


//Saving the mail content and from address to a C variable

message = mapi_get_property_sz_ex(&mapi,"Content");

fromid = mapi_get_property_sz_ex(&mapi,"From");


sleep (100);

loopTime++;

loopCount++;

if (rc != LR_PASS) break;

if (loopCount >=5) {

break;

}

 

 

//Obtains the subject & from address and compare with the expected subject & from address

szSubj=mapi_get_property_sz_ex(&mapi, "Subject");

iCmpResult = strstr(szSubj, lr_eval_string("{Subject}") );// check if email has a relevant SUBJECT

lr_output_message("the Subject of the mail is %s", szSubj);


fromid = mapi_get_property_sz_ex(&mapi,"From");

//ifromResult = strstr(fromid, lr_eval_string("") );

ifromResult = strstr(fromid, "Rudraraju, Madhusudhanaraju");// check if email has a relevant SUBJECT

lr_output_message("the from of the mail is %s", fromid);


if (iCmpResult != NULL && ifromResult != NULL) {

iEmailCounter++;

break;

}

 } while (rc == LR_PASS);

 if(iCmpResult !=NULL && ifromResult != NULL) {

break;

}

 } while ( loopTime < 900 );

 if (iEmailCounter != 0) {

//print the body property 

lr_output_message("the body of the mail is %s", message);


//String extraction using LB & RB

CStr = (char *)calloc(8000000,sizeof(char));

strcpy(CStr, message); //Save it to the C variable

if ( start = (char *)strstr( CStr, LB ) ) //Finds the first occurance of LB in CStr

    {

        start += strlen( LB );

        if ( end = (char *)strstr( start, RB ) ) //Finds the first occurance of RB in CStr

        {

            CorrParam = ( char * )malloc( end - start + 1 );

            memcpy( CorrParam, start, end - start );

            CorrParam[end - start] = '\0';

        }

    }

//Save the extracted string to a text file

if ((logfile = fopen("C:\\Users\\vinoth.srinivasan\\Desktop\\Email\\AuthCode.txt", "a+")) == NULL) 

{

return -1;

}

fprintf(logfile, "%s\n", CorrParam);

fclose(logfile);

if ((fromfile = fopen("C:\\Users\\vinoth.srinivasan\\Desktop\\Email\\AuthCodeFromID.txt", "a+")) == NULL) 

{

return -1;

}

fprintf(fromfile, "%s\n", fromid);

fclose(fromfile);

}

free( CorrParam );

lr_end_transaction("MAILING", LR_PASS);

mapi_logout_ex(&mapi);

return 0;

}



No comments:

Post a Comment