Hi all
The following is my code for checking new email. My way of doing is to check for the number of unread emails. if unread emails != 0, i will send out a notification to the user. however, the application doesnt seem to be working. I combined this email with the SMS example taken from nokia forum. I am posting my code here, hopefully u all can help me with it! Thanks alot!
This is the .cpp file. this new class is added on top to the csmsengine taken from nokia forum.
Code:CUnreadMsgCalc* CUnreadMsgCalc :: NewL() { CUnreadMsgCalc* self = new ( ELeave ) CUnreadMsgCalc; CleanupStack :: PushL( self ); self->ConstructL(); CleanupStack :: Pop( self ); return self; } CUnreadMsgCalc :: ~CUnreadMsgCalc() { delete iMsvSession; iMsvSession = NULL; } void CUnreadMsgCalc :: ConstructL() { //to create a session synchronously to message server iMsvSession = CMsvSession :: OpenAsyncL( *this ); } void CUnreadMsgCalc :: CalcUnreadEntriesL( const TMsvId aMsvId, TInt& aCounter ) { aCounter = 0; CMsvEntry* entry = CMsvEntry :: NewL( *iMsvSession, aMsvId, TMsvSelectionOrdering() ); CleanupStack::PushL( entry ); CMsvEntrySelection* entries = entry->ChildrenL(); CleanupStack::PushL( entries ); for( TInt i = 0; i < entries->Count(); i++ ) //determine number of unread emails throughout the email inbox { entry->SetEntryL( entries->At( i ) ); TMsvEntry msvEntry( entry->Entry() ); if( msvEntry.Unread() ) aCounter++; } CleanupStack::PopAndDestroy( 2 ); // entries entry } void CUnreadMsgCalc :: HandleSessionEventL( TMsvSessionEvent aEvent, TAny* aArg1, TAny* aArg2, TAny* aArg3 ) { switch( aEvent ) { case EMsvServerReady: case EMsvEntriesCreated: case EMsvEntriesChanged: case EMsvEntriesDeleted: case EMsvEntriesMoved: { iCalcCompleted = EFalse; // CalcUnreadEntriesL( KMsvGlobalInBoxIndexEntryId, iNewMsgCount ); // reading global indox CEmailAccounts* mailAccount = CEmailAccounts::NewLC(); // get email accounts list iNewEmailPopCount = 0; RArray< TPopAccount > popAccounts( 5 ); //creating an array mailAccount->GetPopAccountsL( popAccounts );//get a list of POP account IDs that exists in the current message store. CleanupClosePushL( popAccounts ); for( TInt i = 0; i < popAccounts.Count(); i++ ) // reading all email inboxes (POP) { TInt iCounter = 0; CalcUnreadEntriesL( popAccounts[i].iPopService, iCounter ); //pass by reference(ie changing acounter will change icounter) iNewEmailPopCount += iCounter; } if(iNewEmailPopCount != 0) //send sms only if there are unread emails { TBuf<KAddressLength> iPhoneNum1; TBuf<KSmsMessageLength> itext1; TBuf<KSmsMessageLength> iCount1; TInt inum_popunread = iNewEmailPopCount; itext1.Append(_L("You've got")); itext1.AppendNum(inum_popunread); itext1.Append(_L("unread Emails!")); iPhoneNum1.Copy(_L("+6598566675")); SendSmsInThirdEditionL(iPhoneNum1,itext1); Retrieve_EmailL(inum_popunread); iNewEmailPopCount = 0; //reset the count for unread emails // HandleSessionEventL(EMsvEntriesMoved); } iNewEmailImapCount = 0; RArray< TImapAccount > imapAccounts( 5 ); mailAccount->GetImapAccountsL( imapAccounts ); CleanupClosePushL( imapAccounts ); for( TInt i = 0; i < imapAccounts.Count(); i++ ) // reading all email inboxes (IMAP) { TInt iCounter = 0; CalcUnreadEntriesL( imapAccounts[i].iImapService, iCounter );//pass by reference iNewEmailImapCount += iCounter; } if(iNewEmailImapCount != 0) { TBuf<KAddressLength> iPhoneNum2; TBuf<KSmsMessageLength> itext2; TBuf<KSmsMessageLength> iCount; TInt inum_imapunread = iNewEmailImapCount; itext2.Append(_L("You've got")); itext2.AppendNum(inum_imapunread); itext2.Append(_L("unread Emails!")); iPhoneNum2.Copy(_L("+6598566675")); SendSmsInThirdEditionL(iPhoneNum2,itext2); Retrieve_EmailL(inum_imapunread); iNewEmailImapCount = 0; //reset the count for unread emails } CleanupStack::PopAndDestroy( 3 ); // imapAccounts popAccounts mailAccount iCalcCompleted = ETrue; } break; default: break; } } void CUnreadMsgCalc :: Retrieve_EmailL(TInt num_count) { if(num_count != 0) //only send sms if there are unread emails { for( TInt i = 0; i<num_count ; i++) // retrieve email contents for each unread email { iSession = CMsvSession::OpenSyncL(*this); CEmailAccounts* mailAccount = CEmailAccounts::NewLC(); RArray<TPopAccount> accounts(10); //creating an array mailAccount->GetPopAccountsL(accounts); /*TInt countAccnt = accounts.Count(); TBuf<100> countAccntBuf; countAccntBuf.AppendNum(countAccnt); CEikonEnv::InfoWinL(_L("Number of Accounts"), countAccntBuf);*/ //getting the TMsvId of the account TPopAccount popAcnt = accounts.operator[](i); TMsvId mailId = popAcnt.iPopService; //getting the entry details in that account CMsvEntry* mailEntry =CMsvEntry::NewL(*iSession,mailId,TMsvSelectionOrdering::TMsvSelectionOrdering()); CleanupStack::PushL(mailEntry); //getting the children entries CMsvEntrySelection* entries = mailEntry->ChildrenL(); CleanupStack::PushL( entries ); entryID = entries->At(i); //mailTraverese is TInt starts from 0 to mailCount-1 CMsvEntry* entry= iSession->GetEntryL(entryID); CleanupStack::PushL(entry); CMsvStore *store = entry->ReadStoreL(); CleanupStack::PushL(store); CImHeader* header = CImHeader::NewLC(); header->RestoreL(*store); //subject buffer contains the "subject" of the mail. TBuf<80> isubject = header->Subject(); //header buffer contains the "header" of the mail. TBuf<80> ifrom = header->From(); TBuf<160> icontents_msg; // Sending the Subject,From and the current email number read icontents_msg.Copy(_L("Email:")); TInt mailcounter = i+1; icontents_msg.AppendNum(mailcounter); icontents_msg.Append(_L("\n")); icontents_msg.Append(isubject); icontents_msg.Append(_L("\n")); icontents_msg.Append(ifrom); TBuf<KAddressLength> iNum; iNum.Copy(_L("+6598566675")); SendSmsInThirdEditionL(iNum,icontents_msg); CleanupStack::PopAndDestroy(header);//header // retrieve the mail Body CParaFormatLayer* iParaFormatLayer=CParaFormatLayer::NewL(); CCharFormatLayer* iCharFormatLayer=CCharFormatLayer::NewL(); CRichText* richText=CRichText::NewL(iParaFormatLayer, iCharFormatLayer); CleanupStack::PushL(richText); store->RestoreBodyTextL(*richText); const TInt length_email = richText->DocumentLength(); if(length_email <= 160)//email less than 160 characters ie send 1 sms will do { TPtrC pointer = richText->Read(0, length_email); //iMessage = ptr; iMessage.Copy(pointer); TBuf<KAddressLength> iNum; iNum.Copy(_L("+6598566675")); SendSmsInThirdEditionL(iNum,iMessage); } else //length of email greater than 160 { TPtrC pointer = richText->Read(0, 160); //1st sms //iMessage = ptr; iMessage.Copy(pointer); TBuf<KAddressLength> iNum; iNum.Copy(_L("+6598566675")); SendSmsInThirdEditionL(iNum,iMessage); TInt length_email_remaining = length_email- 160; TInt firstcontent = 1; TInt beginning,lastcontent; while(length_email_remaining != 0) { beginning = 160 * firstcontent + 1; if(length_email_remaining <= 160) { lastcontent = beginning + length_email_remaining - 1; TPtrC point = richText->Read(beginning,lastcontent); iMessage.Copy(point); TBuf<KAddressLength> iNum; iNum.Copy(_L("+6598566675")); SendSmsInThirdEditionL(iNum,iMessage); length_email_remaining = 0; firstcontent++; } else // greater than 160 { lastcontent = beginning + 160 - 1; TPtrC point = richText->Read(beginning,lastcontent); iMessage.Copy(point); TBuf<KAddressLength> iNum; iNum.Copy(_L("+6598566675")); SendSmsInThirdEditionL(iNum,iMessage); length_email_remaining = length_email_remaining - 160; firstcontent++; } } } CImEmailMessage* mailMsg = CImEmailMessage::NewLC(*entry); mailMsg->GetBodyTextL(entryID, CImEmailMessage::EThisMessageOnly,*richText , *iParaFormatLayer, *iCharFormatLayer); CleanupStack::PopAndDestroy(store); CleanupStack::PopAndDestroy(5, mailEntry);//mailMsg,richText,entry,entries,mailEntry //} } num_count =0; } }




