How do I add an attachment to the message ?
Answer:
The code snippet below allows you to create and insert a BT/IR message with attachment in the Inbox of the messaging app. You can use this code in your application to help you for example to debug your recognizer for handling your MIME type (instead of testing it on the device by beaming your file over BT or IR), notify a user of an event, deliver a message to the user etc. The code has been compiled against Series60 1.2 and 2.1.
You will need these libraries...
msgs.lib // main messaging library
efsrv.lib
Estor.lib
Include the following header files...
#include
#include OR
#include
// Use these files if you are creating a BT message
//
#include // Series60
#include // UIQ
Step 1
In one of your classes in your code sub-class from MMsvSessionObserver (defined in mtclreg.h) for use when opening a session
with the message server.
class CMyClass : public CBase, public MMsvSessionObserver
{
...
- /**
* From MMsvSessionObserver.
*/
- /**
* Creates a message in the Inbox.
*/
void CreateInboxMessageL();
...
...
Step 2
Use these methods and invoke CreateInboxMessageL() at the appropriate point.
void ExternaliseAndCommit1FileNameL(CMsvEntry* aEntry, const TUid aMsgTypeUid, const TDesC16* aFileName)
{
CMsvStore* messageStore = aEntry->EditStoreL();
CleanupStack::PushL(messageStore);
RMsvWriteStream messageWriteStream;
messageWriteStream.AssignL(*messageStore, aMsgTypeUid);
messageWriteStream.WriteInt32L(aFileName->Length());
messageWriteStream.WriteL(*aFileName);
messageWriteStream.CommitL();
messageWriteStream.Close();
messageStore->CommitL();
CleanupStack::PopAndDestroy(); //messageStore
}
struct TEntryInfoHolder
{
CMsvSession* messageSession;
TMsvId iEntryId;
};
LOCAL_C void DeleteEntry(TAny* aObject)
{
TEntryInfoHolder* object = reinterpret_cast (aObject);
object->messageSession->RemoveEntry(object->iEntryId);
}
void CMyClass::CreateInboxMessageL()
{
CMsvSession* messageSession;
CClientMtmRegistry* registry;
TFileName details;
TFileName description;
TFileName fileName;
TEntryInfoHolder infoHolder;
messageSession = CMsvSession::OpenSyncL(*this);
CleanupStack::PushL(messageSession);
registry = CClientMtmRegistry::NewL(*messageSession);
CleanupStack::PushL(registry);
infoHolder.messageSession=messageSession;
infoHolder.iEntryId=0; // No entry yet !
CleanupStack::PushL(TCleanupItem(DeleteEntry, &infoHolder));
// Subject
_LIT(KSub, "Test Hello");
details.Copy(KSub());
_LIT(KFile, "c:\\testSIS.sis");
_LIT(KFileName, "testSIS.sis");
description.Copy(KFileName());
CMsvEntry* inbox=messageSession->GetEntryL(KMsvGlobalInBoxIndexEntryIdValue);
CleanupStack::PushL(inbox);
TMsvEntry newEntry;
// If using BT uncomment line
// newEntry.iMtm = TUid::Uid(KUidMsgTypeBT);
newEntry.iMtm = TUid::Uid(KUidMsgTypeIr);
newEntry.SetVisible(EFalse);
newEntry.SetInPreparation(ETrue);
newEntry.iType = KUidMsvMessageEntry;
newEntry.iServiceId = KMsvUnknownServiceIndexEntryId;
newEntry.iDate.HomeTime();
newEntry.iSize = 0;//todo
newEntry.iDetails.Set(details);
newEntry.iDescription.Set(description);
inbox->CreateL(newEntry);
infoHolder.iEntryId=newEntry.Id();
CClientMtmRegistry* mtmReg = CClientMtmRegistry::NewL(*messageSession);
CleanupStack::PushL(mtmReg);
CMsvEntry* entry=messageSession->GetEntryL(newEntry.Id());
CleanupStack::PushL(entry);
CBaseMtm *mtm = mtmReg->NewMtmL(entry->Entry().iMtm);
mtm->SetCurrentEntryL(entry); // takes ownership
CleanupStack::Pop(); // entry
CleanupStack::PushL(mtm);
// Create an empty attachment
TFileName tempfileName;
TMsvId attachmentId;
mtm->CreateAttachmentL(attachmentId, tempfileName);
// Construct the path of the file to be created as attachment
HBufC* completeFilePath = HBufC::NewLC(500);
TPtr completeFilePathPtr = completeFilePath->Des();
completeFilePathPtr.Append(tempfileName);
completeFilePathPtr.Append(description);
RFs session;
session.Connect();
CleanupClosePushL(session);
CFileMan* fman = CFileMan::NewL(session);
TInt err =fman->Copy(KFileName(),*completeFilePath);
delete fman;
CleanupStack::PopAndDestroy(); // completeFilePath
CleanupStack::PopAndDestroy(); // session
CMsvEntry* attachment=messageSession->GetEntryL(attachmentId);
TMsvEntry attachmentEntry=attachment->Entry();
CleanupStack::PushL(attachment);
fileName.Append(description);
TEntry fileEntry;
messageSession->FileSession().Entry(fileName, fileEntry);
attachmentEntry.iDetails.Set(description);
attachmentEntry.iDate.HomeTime();
attachmentEntry.iSize = fileEntry.iSize;
// When using BT uncomment below
//
// ExternaliseAndCommit1FileNameL(attachment, TUid::Uid( KUidMsgTypeBT ), &description); // Note: IR does not require the BT UID param.
ExternaliseAndCommit1FileNameL(attachment, TUid::Uid( KUidMsgTypeIr) , &description); // Note: IR does not require the BT UID param.
attachment->ChangeL(attachmentEntry);
CleanupStack::PopAndDestroy(); // attachment
newEntry = entry->Entry();
newEntry.iSize = fileEntry.iSize;
newEntry.SetVisible(ETrue);
newEntry.SetInPreparation(EFalse);
newEntry.SetUnread(ETrue);
entry->ChangeL(newEntry);
CleanupStack::PopAndDestroy(3); // inbox, mtmReg and mtm
CleanupStack::Pop(); // TCleanupItem(DeleteEntry, &infoHolder
CleanupStack::PopAndDestroy(2); // messageSession, registry
}
Caveats
1. Code might compile but not work depending on if BT or IR is supported for a given device.
2. On the Emulator the user is NOT notified after the message has been added in the Inbox. This means that the user has to start the messaging app manually.
3. Refer to FAQ-1138 for limitations on creating user defined mailboxes