×

Discussion Board

Results 1 to 6 of 6
  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    19

    Checking for new email

    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;
    			
    		}
    	}

  2. #2
    Registered User
    Join Date
    Jun 2008
    Posts
    19

    Re: Checking for new email

    Continuation of .cpp and .h file

    Code:
    //Sending the sms in third edition with RSendAs
    void CUnreadMsgCalc ::SendSmsInThirdEditionL(const TDesC& aAddr, const TDesC& aMsg)
        {
    #ifdef __SERIES60_30__
           
        RSendAs sendAs;
        User::LeaveIfError(sendAs.Connect());
        CleanupClosePushL(sendAs);
    
        RSendAsMessage sendAsMessage;
        sendAsMessage.CreateL(sendAs, KUidMsgTypeSMS);
        CleanupClosePushL(sendAsMessage);
    
        // prepare the message
        sendAsMessage.AddRecipientL(aAddr, RSendAsMessage::ESendAsRecipientTo);
        sendAsMessage.SetBodyTextL(aMsg);
    
        // send the message
        sendAsMessage.SendMessageAndCloseL();
    
        // sendAsMessage (already closed)
        CleanupStack::Pop();
    
        // sendAs
        CleanupStack::PopAndDestroy();
    #endif
        }
    
    This is part of my .h file
    class CUnreadMsgCalc: public CBase,public MMsvSessionObserver
    {
       public:
          static CUnreadMsgCalc* NewL();  // factory
          virtual ~CUnreadMsgCalc();
     
          inline TBool IsCalcCompleted() { return iCalcCompleted; }  // if true - then NewXXXCount returns correct value
     
         // inline TInt NewMsgCount()      { return iNewMsgCount; }
          inline TInt NewEmailCount()    { return iNewEmailPopCount + iNewEmailImapCount; }
          
       private:
          void ConstructL();      
          void CalcUnreadEntriesL( const TMsvId aMsvId, TInt& aCounter ); // read one entry
          void Retrieve_EmailL(TInt num_count);
          void SendSmsInThirdEditionL(const TDesC& aAddr, const TDesC& aMsg);
          //void UpdateCountL();
          TInt Num_mailsL(const TMsvId aMsvId);
          
       private: // from MMsvSessionObserver      
          void HandleSessionEventL( TMsvSessionEvent aEvent, TAny* aArg1, TAny* aArg2, TAny* aArg3 );
    	   
       private:      
          CMsvSession* iMsvSession;
          CMsvSession* iSession;
          TBool iCalcCompleted;
          // Message body
          TBuf<KSmsMessageLength> iMessage;
          TInt iNewEmailPopCount, iNewEmailImapCount;	
          TMsvId entryID;
        
     
    };
    From this code, my application doesnt seem to be getting any alerts for any new emails received. Pls help! Thank u once again!

  3. #3
    Regular Contributor
    Join Date
    Jun 2008
    Location
    Tianjin
    Posts
    156

    Re: Checking for new email

    Please try the following coes to get the selection.

    Code:
    void GetSelectionL(TMsvId aServiceId, TMsvId aParent, CMsvEntrySelection& aSelection)
    	{
    	CMsvEntryFilter* filter = CMsvEntryFilter::NewLC();
    	filter->SetType( KUidMsvMessageEntry );
    	filter->SetOrder(TMsvSelectionOrdering(KMsvNoGrouping, EMsvSortByDate, ETrue));
    	filter->SetService(aServiceId);
    	filter->SetMtm(iMtmId);
    	
    	iSession->GetChildIdsL( aParent, *filter, aSelection );
    	CleanupStack::PopAndDestroy(filter);
    	}
    anson_zhao

  4. #4
    Registered User
    Join Date
    Jun 2008
    Posts
    19

    Re: Checking for new email

    Hi Anson!

    Thanks for your help! I noticed u posted to another thread which is similar to mine. In that post, you advised to use HandleSessionEventL() to receive, delete emails. The following is what i wrote, but I've no idea what to include under EMsvEntriesChanged and EMsvEntriesDeleted.


    void CUnreadMsgCalc :: HandleSessionEventL( TMsvSessionEvent aEvent, TAny* aArg1, TAny* aArg2, TAny* aArg3 )
    {
    switch( aEvent )
    {
    case EMsvServerReady:
    case EMsvEntriesCreated:
    {
    CMsvEntrySelection* entries =static_cast<CMsvEntrySelection*>(aArg1);
    // entry pointer for making changes in the actual message contexts
    CMsvEntry* entry;
    entry = iMsvSession->GetEntryL( entries->At(0) );
    CleanupStack::PushL(entry);

    //checking whether an incoming message is of type IMAP4 or not
    if(entry->Entry().iMtm == KUidMsgTypeIMAP4)
    {
    Retrieve_EmailL(); // to retrieve the email contents
    }
    CleanupStack::Pop(); // entry
    delete entry;
    }
    case EMsvEntriesChanged:
    case EMsvEntriesDeleted:
    }

    I am in need of your assistance. Thank u!

  5. #5
    Regular Contributor
    Join Date
    Jun 2008
    Location
    Tianjin
    Posts
    156

    Re: Checking for new email

    When a new email arrived, EMsvEntriesCreated will be received. But at this time you cannot get any information about the new email. And then you can get EMsvEntriesChanged, you can get all information about the new email entry.

    Hopefully this is helpful.
    anson_zhao

  6. #6
    Nokia Developer Moderator
    Join Date
    May 2007
    Posts
    468

    Re: Checking for new email

    In SDK API plug-in there's an API for getting notification of new emails:
    http://wiki.forum.nokia.com/index.ph...mailStatus_API

    also check
    http://wiki.forum.nokia.com/index.php/Category:E-Mail

    Cheers,
    Jack

Similar Threads

  1. 6300: Email client disappeared.
    By mcdj in forum General Development Questions
    Replies: 1
    Last Post: 2007-06-20, 21:14
  2. Compilation series 60 3rd edition
    By asmobease in forum Symbian Tools & SDKs
    Replies: 32
    Last Post: 2006-09-13, 09:46
  3. email mtm help is needed
    By siemensc55 in forum Symbian
    Replies: 0
    Last Post: 2004-07-18, 18:58

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •