Creating attachments to messages

The messaging framework allows you to create attachments to be added to messages.

Attachments to messages are created as empty files before they are filled and added to a message.


  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.
    void CFoo::CreateAttachmentL(CMsvEntry& aEntry )
        {
        _LIT(KFileName, "textfile.txt");
    ...
        // Wait for request to complete
        User::WaitForRequest(status);
    
        // Write to the file at the current offset within the file. 
        User::LeaveIfError(file.Write(_L8("text file as attachment")));
    
        
        CleanupStack::PopAndDestroy(&file);
        CleanupStack::PopAndDestroy(store);
        }
    
  2. Get the details of the message to which you want to add an attachment using CMsvEntry::EditStoreL().
        CMsvStore* store = aEntry.EditStoreL();
        CleanupStack::PushL(store);
    
    This function returns CMsvStore in writable mode.
  3. Get an MMsvAttachmentManager attachment manager for the message entry, using CMsvStore::AttachmentManagerL().
        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);
        
        // set attachment file name
        attachment->SetAttachmentNameL(KFileName());
    

  5. Initialise a file handle.
        // A file handle
        RFile file;
        CleanupClosePushL(file);
        TRequestStatus status;
    

  6. Create a new attachment using the MMsvAttachmentManager::CreateAttachmentL() function. The CreateAttachmentL() function creates a new empty attachment file and returns an open writable file handle to the empty attachment file in the Message Store. You must pass an uninitialised file handle. The file handle cannot be used until the asynchronous request completes successfully. If the request is successful, the file handle is opened for writing.
        // CreateAttachmentL API will return an open writable file handle 
        // to an empty attachment file in the message store. 
        attManager.CreateAttachmentL(KFileName, file, attachment, status);
    

  7. Commit the store.
        // Commit the store
        store->CommitL();
    

The result of creating an attachment is an empty file with a handle opened for writing.

You can now add the attachment to a file and retrieve and modify it.

Related information