void CSmtpClientMtm::StoreEmailMessageL()
	{
	__ASSERT_DEBUG(KUidMsvMessageEntryValue==iMsvEntry->Entry().iType.iUid, gPanic(ESmtcMTMNotAMessageEntry));
	CMsvStore* store = iMsvEntry->EditStoreL();
	CleanupStack::PushL(store);

	// Save the Email header stream...
	iHeader->SetSubjectL(SubjectL());
	const TInt numberOfRecipients=iAddresseeList->Count();
	// put all addressees in the To: field...
	for (TInt n=0 ; n < numberOfRecipients ; ++n)
		{
		// validation done by ValidateMessage()
		switch(iAddresseeList->Type(n))
			{
		case EMsvRecipientTo:
			iHeader->ToRecipients().AppendL(AddresseeList()[n]);
			break;
		case EMsvRecipientCc:
			iHeader->CcRecipients().AppendL(AddresseeList()[n]);
			break;
		case EMsvRecipientBcc:
			iHeader->BccRecipients().AppendL(AddresseeList()[n]);
			break;
			}
		}

	iHeader->StoreL(*store);
	store->CommitL();

	// NB the From field will be set by server MTM when the email message is sent

	CleanupStack::PopAndDestroy();

	TMsvEntry messageEntry=iMsvEntry->Entry();
	CImEmailMessage* emailMessage = CImEmailMessage::NewLC(*iMsvEntry);

	// now store the body text
	emailMessage->StoreBodyTextL(messageEntry.Id(), Body(), iWait->iStatus);
	iWait->Start();		// wait for the asynch operation to complete
	
	// 
	// I have stored away all the email data,
	// now update the TMsventry that represents
	// the email to ensure that it is up to date
	//
	TInt32 totalSizeOfAllAttachments = GetAttachmentSizeL(*emailMessage, messageEntry.Id());

	messageEntry.iSize = iHeader->DataSize() + Body().DocumentLength() + totalSizeOfAllAttachments;
	messageEntry.iDescription.Set(iHeader->Subject());
	messageEntry.iDate.UniversalTime();

	// fix for DEF051564 - SMTP client MTM CreateMessageL not creating message in correct state
	//
	// since CreateMessageL creates message as in preparation and non-visible, the SaveMessageL
	// must now make it visible and not in preparation
	messageEntry.SetVisible(ETrue);
	messageEntry.SetInPreparation(EFalse);

	if (iHeader->ToRecipients().Count()>0)
		messageEntry.iDetails.Set(iHeader->ToRecipients()[0]);
	else if (iHeader->CcRecipients().Count())
		messageEntry.iDetails.Set(iHeader->CcRecipients()[0]);
	else if (iHeader->BccRecipients().Count())
		messageEntry.iDetails.Set(iHeader->BccRecipients()[0]);
	//else do nothing as there are no recipients yet!

	if (totalSizeOfAllAttachments>0)
		messageEntry.SetAttachment(ETrue);

	// if there are multiple recipients then set the flag
	if ((iHeader->ToRecipients().Count() + iHeader->CcRecipients().Count() + iHeader->BccRecipients().Count()) >=2)
		messageEntry.SetMultipleRecipients(ETrue);

	// update the contents of the message entry
	iMsvEntry->ChangeL(messageEntry);

	CleanupStack::PopAndDestroy();	// emailMessage
	}
LOCAL_C TMsvId CreateMessageL()
	{
	testUtils->TestStart(testNo++);
	CTestActive* testActive = new (ELeave) CTestActive();
	CleanupStack::PushL(testActive);

	TMsvPartList partList = KMsvMessagePartBody | KMsvMessagePartAttachments | KMsvMessagePartDescription | KMsvMessagePartOriginator;
	TMsvEmailTypeList msvEmailTypeList = 0;
	CImEmailOperation* emailOperation = CImEmailOperation::CreateNewL(testActive->iStatus, *(testUtils->iMsvSession), KMsvGlobalOutBoxIndexEntryId, partList, msvEmailTypeList, KUidMsgTypeSMTP);
	CleanupStack::PushL(emailOperation);

	TestUiTimer* testUiTimer = TestUiTimer::NewLC(test.Console(), emailOperation);
	testActive->StartL();
	testUiTimer->IssueRequest();
	CActiveScheduler::Start();

	TMsvId temp;	
	TPckgC<TMsvId> paramPack(temp);
	const TDesC8& progBuf = emailOperation->ProgressL();
	paramPack.Set(progBuf);
	TMsvId messageId=paramPack();
	TInt error = emailOperation->iStatus.Int();

	CleanupStack::PopAndDestroy(3); // testActive, emailOperation, testUiTimer
	testUtils->TestFinish(testNo-1, error);
	log->AppendComment(_L8(" Messages created in the outbox"));

	// fill in details for email message
	testUtils->iMsvEntry->SetEntryL(messageId);
	CMsvStore* store = testUtils->iMsvEntry->EditStoreL();
	CleanupStack::PushL(store);
	CImHeader* header = CImHeader::NewLC();
	header->RestoreL(*store);
	header->ToRecipients().AppendL(_L("kashifn@msg4"));
	header->SetSubjectL(_L("Message from T_SMTC_IPV6 !!"));
	header->StoreL(*store);
	store->CommitL();
 	CleanupStack::PopAndDestroy(2); //header, store

	//store some body text!!
	CParaFormatLayer* paraFormatLayer=CParaFormatLayer::NewL();
	CleanupStack::PushL(paraFormatLayer);

	CCharFormatLayer* charFormatLayer=CCharFormatLayer::NewL(); 
	CleanupStack::PushL(charFormatLayer);

	CRichText* bodyText=CRichText::NewL(paraFormatLayer, charFormatLayer, CEditableText::EFlatStorage, 256);
	CleanupStack::PushL(bodyText);

	TBuf<100> bodyContents = _L("Test body contents so that we can test the store body text code.");
	bodyText->InsertL(0, bodyContents);

	// store the body text
	testActive = new (ELeave) CTestActive();
	CleanupStack::PushL(testActive);

	CImEmailMessage* imEmailMessage = CImEmailMessage::NewLC(*(testUtils->iMsvEntry));
	imEmailMessage->StoreBodyTextL(messageId, *bodyText, testActive->iStatus);
	testActive->StartL();
	CActiveScheduler::Start();	// wait for the asynch operation to complete
	CleanupStack::PopAndDestroy(2); //imEmailMessage, testActive

	// Add an attachment
	testActive = new (ELeave) CTestActive();
	CleanupStack::PushL(testActive);
//mkn start comment
	_LIT(KAttachment1, "C:\\mailtest\\IMCM\\rfc82201.txt");
	imEmailMessage = CImEmailMessage::NewLC(*(testUtils->iMsvEntry));
	testUtils->iMsvEntry->SetEntryL(messageId);
	CMsvAttachment* attachmentInfo = CMsvAttachment::NewL(CMsvAttachment::EMsvFile);
	CleanupStack::PushL(attachmentInfo);
	imEmailMessage->AttachmentManager().AddAttachmentL(KAttachment1,attachmentInfo,testActive->iStatus);
	CleanupStack::Pop(attachmentInfo);
	testActive->StartL();
	CActiveScheduler::Start();	// wait for the asynch operation to complete
//mkn end comment
	CleanupStack::PopAndDestroy(5); //testActive, paraFormatLayer, charFormatLayer, bodyText
	return messageId;
	}
LOCAL_C void CreateMessageL()
	{	
	testUtils->TestStart(testNo++);
	testUtils->WriteComment(_L("CreateMessageL"));
	CTestActive* testActive = new (ELeave) CTestActive();
	CleanupStack::PushL(testActive);

	TMsvPartList partList = KMsvMessagePartBody | KMsvMessagePartAttachments | KMsvMessagePartDescription | KMsvMessagePartOriginator;
	TMsvEmailTypeList msvEmailTypeList = 0;
	CImEmailOperation* emailOperation = CImEmailOperation::CreateNewL(testActive->iStatus, *(testUtils->iMsvSession), KMsvGlobalOutBoxIndexEntryId, partList, msvEmailTypeList, KUidMsgTypeSMTP);
	CleanupStack::PushL(emailOperation);

	TestUiTimer* testUiTimer = TestUiTimer::NewLC(test.Console(), emailOperation);
	testActive->StartL();
	testUiTimer->IssueRequest();
	CActiveScheduler::Start();

	TMsvId temp;	
	TPckgC<TMsvId> paramPack(temp);
	const TDesC8& progBuf = emailOperation->ProgressL();
	paramPack.Set(progBuf);
	TMsvId messageId=paramPack();
	TInt error = emailOperation->iStatus.Int();

	CleanupStack::PopAndDestroy(3, testActive); // testActive, emailOperation, testUiTimer
	testUtils->TestFinish(testNo-1, error);

	// fill in details for email message
	testUtils->iMsvEntry->SetEntryL(messageId);
	CMsvStore* store = testUtils->iMsvEntry->EditStoreL();
	CleanupStack::PushL(store);
	CImHeader* header = CImHeader::NewLC();
	header->RestoreL(*store);
	header->ToRecipients().AppendL(_L("*****@*****.**"));
	header->SetSubjectL(_L("Message from T_SMTC Typhoon!!"));
	header->StoreL(*store);
	store->CommitL();
 	CleanupStack::PopAndDestroy(2, store); // header, store

	//store some body text!!
	CParaFormatLayer* paraFormatLayer=CParaFormatLayer::NewL();
	CleanupStack::PushL(paraFormatLayer);

	CCharFormatLayer* charFormatLayer=CCharFormatLayer::NewL(); 
	CleanupStack::PushL(charFormatLayer);

	CRichText* bodyText=CRichText::NewL(paraFormatLayer, charFormatLayer, CEditableText::EFlatStorage, 256);
	CleanupStack::PushL(bodyText);

	TBuf<100> bodyContents = _L("Test body contents so that we can test the store body text code.");
	bodyText->InsertL(0, bodyContents);

	// store the body text
	testActive = new (ELeave) CTestActive();
	CleanupStack::PushL(testActive);

	CImEmailMessage* imEmailMessage = CImEmailMessage::NewLC(*(testUtils->iMsvEntry));
	imEmailMessage->StoreBodyTextL(messageId, *bodyText, testActive->iStatus);
	testActive->StartL();
	CActiveScheduler::Start();	// wait for the asynch operation to complete
	CleanupStack::PopAndDestroy(5, paraFormatLayer); // imEmailMessage, testActive, bodyText, charFormatLayer, paraFormatLayer
	}
void CPerformanceInitialisationClient::CreateMessageL(const TMsvId aTMsvId,TInt aCount,TDesC& aBody,TPtrC& aAddress,TDesC& aSubject,TDesC& aAttachment1,TDesC& aAttachment2,TDesC& aAttachment3)
	{
	
	CParaFormatLayer* paraFormatLayer=CParaFormatLayer::NewL();
	CleanupStack::PushL(paraFormatLayer);

	CCharFormatLayer* charFormatLayer=CCharFormatLayer::NewL(); 
	CleanupStack::PushL(charFormatLayer);
	
	CRichText* bodyText=CRichText::NewL(paraFormatLayer, charFormatLayer, CEditableText::EFlatStorage, 256);
	CleanupStack::PushL(bodyText);	

	if(aBody!=KNullFileName)
		{
		//read body text form a file
		RFs fsSession;              
		User::LeaveIfError(fsSession.Connect()); 
		RFile infile; 
	 	User::LeaveIfError(infile.Open(fsSession, aBody, EFileRead));
		TInt size;
		TInt err=infile.Size(size);
		 
		HBufC8* filebuf;
		filebuf = HBufC8::NewLC(size);  
		TPtr8 ptr8=filebuf->Des();	  
		infile.Read(ptr8);
		HBufC* filebuf16;
		filebuf16 = HBufC::NewLC(size);
		TPtr ptr16=filebuf16->Des();
		
		ptr16.Copy(ptr8);
		//insert the body text
		bodyText->InsertL(0, ptr16);
		
		infile.Close();
		fsSession.Close();	

		CleanupStack::PopAndDestroy(2,filebuf); 
		}
	
	
	for(TInt i=0;i<aCount;i++)
		{
		//create the message
		TMsvPartList partList = KMsvMessagePartBody;
		CImEmailOperation* emailOp = CImEmailOperation::CreateNewL(iTestActive->iStatus,*iSession,aTMsvId,partList,0,KUidMsgTypeSMTP); 
		CleanupStack::PushL(emailOp);
			
		// Active object which stops the active scheduler
		iTestActive->StartL(); 
		CActiveScheduler::Start();
			
		// Get the new message id.
		TMsvId messageId;
		TPckg<TMsvId> param(messageId);
		param.Copy(emailOp->FinalProgress());
		CleanupStack::PopAndDestroy(emailOp);
		
		// Save the message body text
		TMsvSelectionOrdering ordering;	
		CMsvEntry* entry = CMsvEntry::NewL(*iSession, messageId,ordering);
		CleanupStack::PushL(entry);
		CImEmailMessage* message = CImEmailMessage::NewL(*entry);
		CleanupStack::PushL(message); 
		
		//add attachments
		if(aAttachment1!=KNullFileName)
			{
			CMsvAttachment* attachmentInfo = CMsvAttachment::NewL(CMsvAttachment::EMsvFile);
			CleanupStack::PushL(attachmentInfo);
			message->AttachmentManager().AddAttachmentL(aAttachment1, attachmentInfo, iTestActive->iStatus);
			CleanupStack::Pop(attachmentInfo);	
			iTestActive->StartL();
			CActiveScheduler::Start();	// wait for the asynch operation to complete
			}
		
		if(aAttachment2!=KNullFileName)
			{
			CMsvAttachment* attachmentInfo = CMsvAttachment::NewL(CMsvAttachment::EMsvFile);
			CleanupStack::PushL(attachmentInfo);
			message->AttachmentManager().AddAttachmentL(aAttachment2, attachmentInfo, iTestActive->iStatus);
			CleanupStack::Pop(attachmentInfo);	
			iTestActive->StartL();
			CActiveScheduler::Start();	// wait for the asynch operation to complete
			}
		
		if(aAttachment3!=KNullFileName)
			{
			CMsvAttachment* attachmentInfo = CMsvAttachment::NewL(CMsvAttachment::EMsvFile);
			CleanupStack::PushL(attachmentInfo);
			message->AttachmentManager().AddAttachmentL(aAttachment3, attachmentInfo, iTestActive->iStatus);
			CleanupStack::Pop(attachmentInfo);	
			iTestActive->StartL();
			CActiveScheduler::Start();	// wait for the asynch operation to complete
			}

		iTestActive->StartL();
		message->StoreBodyTextL(messageId, *bodyText, iTestActive->iStatus);
		CActiveScheduler::Start();
		CleanupStack::PopAndDestroy(message); 
		
		// fill in header details for email message
		CMsvStore* store = 	entry->EditStoreL();
		CleanupStack::PushL(store);
		CImHeader* header = CImHeader::NewLC();
		header->RestoreL(*store);
		header->ToRecipients().AppendL(aAddress);
		header->SetSubjectL(aSubject);	
		header->StoreL(*store);
	
		store->CommitL();	
	 	CleanupStack::PopAndDestroy(3,entry); 
		}
		
	CleanupStack::PopAndDestroy(3,paraFormatLayer); 

	}