Retrieving and modifying attachments to messages

The messaging framework allows you to retrieve and modify attachments to messages.

Attachments may be retrieved and modified after they have been added to a message.

  1. Get the details of the message to which you want to add an attachment using CMsvEntry::EditStoreL(). This function returns CMsvStore in writable mode.
  2. Get an MMsvAttachmentManager attachment manager for the message entry, using CMsvStore::AttachmentManagerL().

  3. Find how many attachments a message entry has using the MMsvAttachmentManager::AttachmentCount() function.

  4. Get attachment attributes using the appropriate CMsvAttachment functions. .

  5. Get attachment information using the MMsvAttachmentManager::GetattachmentL() function.

Retrieval and modification example

The following code uses the MMsvAttachmentManager::AttachmentCount() and GetattachmentL() functions to get attachment information. It then calls the various CMsvAttachment accessor functions to get the attachment attributes, and to access the attachment file itself:

void CFoo::GetAttachmentInfoL(MMsvAttachmentManager& attManager)
    {
    // get number of attachments
    TInt numAttach = attManager.AttachmentCount();
    if ( numAttach == 0 ) 
       { // no attachments
       return;
       }

    // Get attachment attributes for the first attachment (index 0)
    CMsvAttachment* attachInfo = attManager.GetAttachmentInfoL(0);

    // Ownership of attachInfo is transferred to the caller - so push it onto the CleanupStack
    CleanupStack::PushL(attachInfo);
    
    // Get attachment's name 
    const TDesC& attachName = attachInfo->AttachmentName();

    // Get attachment's size 
    TInt attachSize = attachInfo->Size();
    
    // Get attachment's type 
    CMsvAttachment::TMsvAttachmentType attachType = attachInfo->Type();
    
    // Get MIME type 
    const TDesC8& attachMType = attachInfo->MimeType();
    
    // Get completeness flag   
    TBool complete = attachInfo->Complete(); 

    ...

    // Destroy attachInfo when finished.  
    CleanupStack::PopAndDestroy(attachInfo);
    }

You can modify attachment information for an existing attachment using the ModifyattachmentL() function. The attachment information object passed in replaces the existing attachment information object.

void CFoo::ModifyAttachmentInfoL(CMsvEntry& aEntry)
    {
    // Get the store
    CMsvStore* store = aEntry.EditStoreL();
    CleanupStack::PushL(store);
    
    MMsvAttachmentManager& attManager = store->AttachmentManagerL();
    
    // Get number of attachments
    TInt numAttach = attManager.AttachmentCount();
    if ( numAttach == 0 ) 
       { // no attachments
       CleanupStack::PopAndDestroy() ; // store
       return;
       }
       
    // Get attachment attributes for the first attachment (index 0)
    CMsvAttachment* attachment = attManager.GetAttachmentInfoL(0);
    CleanupStack::PushL( attachment ) ;
    
    // Rename the file
    TRequestStatus status;
    _LIT(KFileName, "hello.txt");
    
    attachment->SetAttachmentNameL(KFileName);
    attachment->SetComplete(EFalse);
    
    // Modify attachment info
    attManager.ModifyAttachmentInfoL(attachment, status);
    
    // Wait for request to complete
    User::WaitForRequest(status);
    attachment->SetComplete(ETrue);  
    
    store->CommitL();

    CleanupStack::PopAndDestroy(2, store); // attachment, store
    }

Related information