Adding attachments to messages

The messaging framework allows you to add attachments to messages.

A message attachment, once it has been created, must be added to its message in a separate procedure explained in this document.


  1. Create an active object implementation. All the MMsvAttachmentManager functions that modify attachments for an entry are asynchronous, so a request must be called with an active object implementation.
    //* Active object request function to add an attachment.
    Arguments:
    aEntry -  entry to make attachment for
    aAttachPath -  path of file to attach
    aSize - size of attachment in bytes 
    */
    
    void CFoo::AttachFileL(CMsvEntry& aEntry, const TFileName& aAttachPath, TInt aSize)
        { 
    ...
        // Wait for request to complete
        User::WaitForRequest(status);
            
        CleanupStack::PopAndDestroy(store);    
        }
    
    
  2. Get the details of the message to which you want to add an attachment using CMsvEntry::EditStoreL(). This function returns CMsvStore in writable mode.
        // Get store   
        CMsvStore* store = aEntry.EditStoreL();
        CleanupStack::PushL(store);
    
  3. Get an MMsvAttachmentManager attachment manager for the message entry, using CMsvStore::AttachmentManagerL().
        // Get attachment manager from the entry's store     
        MMsvAttachmentManager& attManager = store->AttachmentManagerL();    
    

  4. Create a new attachment attributes object using the CMsvAttachment::NewL() function.
        // Create a new attachment attributes object    
        CMsvAttachment* attachment = CMsvAttachment::NewL(CMsvAttachment::EMsvFile);    
        CleanupStack::PushL(attachment);    
    

  5. Set the attachments name and size attributes using the functions of the CMsvAttachment class.
        // Set the attachment name and size attributes    
        TParse fparse;    
        User::LeaveIfError(fparse.Set(aAttachPath,NULL,NULL));    
        attachment->SetAttachmentNameL(fparse.NameAndExt());    
        attachment->SetSize(aSize);
    

  6. Add the attachment using any of the following functions:

    1. For copied attachments, use the MMsvAttachmentManager::AddAttachmentL() function.

    2. For linked attachments, use the MMsvAttachmentManager::AddLinkedAttachmentL() function.
    Note: For OBEX MTM, to add headers to an attachment, use the CObexHeaderList::ExportToAttachmentL() function. Headers can be retrieved using the CObexHeaderList::ImportFromAttachmentL() function.
        TRequestStatus status;
             
        // Ownership of attachment will transfer to attachManager    
        CleanupStack::Pop(attachment);
        // Add the attachment    
        attManager.AddAttachmentL(aAttachPath, attachment, status);    
    

  7. Commit the store.

You now have an attachment added to its message.

You can now retrieve and modify the message attachment.

Related information