void CMtfTestActionSmtpGetAttachmentFileFromId::RunTestActionL()
	{
	CMsvSession* paramSession = ObtainParameterReferenceL<CMsvSession>(TestCase(),ActionParameters().Parameter(0));
	TMsvId messageEntry = ObtainValueParameterL<TMsvId>(TestCase(),ActionParameters().Parameter(1));
	TMsvAttachmentId attachId = ObtainValueParameterL<TMsvAttachmentId>(TestCase(),ActionParameters().Parameter(2));
	HBufC* dataFilePath   = ObtainParameterReferenceL<HBufC>(TestCase(),ActionParameters().Parameter(3), NULL);

	CMsvEntry* entry = paramSession->GetEntryL(messageEntry);
	CleanupStack::PushL(entry);
	
	CImEmailMessage* emailMsg = CImEmailMessage::NewL(*entry);
	CleanupStack::PushL(emailMsg);
	
	MMsvAttachmentManager& manager = emailMsg->AttachmentManager();
	
	RFile fileAttachment = manager.GetAttachmentFileL(attachId);
	CleanupClosePushL(fileAttachment);
	
	if( dataFilePath != NULL )
		{
		// check of contents of attachment file is requested
		CompareFileL(fileAttachment, *dataFilePath);
		}
	
	CleanupStack::PopAndDestroy(3, entry); // fileAttachment, emailMsg, entry
	}
void CMtfTestActionSmtpModifyAttachmentInfo::ExecuteActionL()
	{
	TestCase().INFO_PRINTF2(_L("Test Action %S start..."), &KTestActionSmtpModifyAttachmentInfo);
	CMsvSession* paramSession = ObtainParameterReferenceL<CMsvSession>(TestCase(),ActionParameters().Parameter(0));
	TMsvId messageEntry = ObtainValueParameterL<TMsvId>(TestCase(),ActionParameters().Parameter(1));
	TMsvAttachmentId attachmentId = ObtainValueParameterL<TMsvAttachmentId>(TestCase(),ActionParameters().Parameter(2));

	CMsvEntry* entry = paramSession->GetEntryL(messageEntry);
	CleanupStack::PushL(entry);
	
	CImEmailMessage* emailMsg = CImEmailMessage::NewL(*entry);
	CleanupStack::PushL(emailMsg);

	MMsvAttachmentManager& manager = emailMsg->AttachmentManager();
	
	CMsvMimeHeaders* mimeHeaders = CMsvMimeHeaders::NewL();
	CleanupStack::PushL(mimeHeaders);

	mimeHeaders->SetContentDescriptionL(KTxtContentDescription());
	mimeHeaders->SetContentBaseL(KTxtContentBase());
	mimeHeaders->SetContentLocationL(KTxtContentLocation());
	mimeHeaders->SetContentIdL(KTxtContentId());
	mimeHeaders->SetContentTypeL(KTxtContentType());
	mimeHeaders->SetContentSubTypeL(KTxtContentSubType());
	mimeHeaders->SetContentDispositionL(KTxtContentDisposition());
	mimeHeaders->ContentTypeParams().AppendL(KTxtParam1());
	mimeHeaders->ContentTypeParams().AppendL(KTxtParam2());
	mimeHeaders->ContentTypeParams().AppendL(KTxtParam3());
	mimeHeaders->ContentDispositionParams().AppendL(KTxtParam4());
	mimeHeaders->ContentDispositionParams().AppendL(KTxtParam5());
	mimeHeaders->ContentDispositionParams().AppendL(KTxtParam6());
	mimeHeaders->XTypeParams().AppendL(KTxtParam7());
	mimeHeaders->XTypeParams().AppendL(KTxtParam8());
	mimeHeaders->XTypeParams().AppendL(KTxtParam9());

	CMtfAsyncWaiter* waiter = CMtfAsyncWaiter::NewL();
	CleanupStack::PushL(waiter);
	CMsvAttachment* attachment = manager.GetAttachmentInfoL(attachmentId);
	CleanupStack::PushL(attachment);
	mimeHeaders->StoreL(*attachment);
	manager.ModifyAttachmentInfoL(attachment, waiter->iStatus);
	CleanupStack::Pop(attachment);
	waiter->StartAndWait();
	User::LeaveIfError(waiter->Result());

	CleanupStack::PopAndDestroy(2, mimeHeaders); // waiter, mimeHeaders
	
	CompareMimeHeadersL(*emailMsg, attachmentId);
	
	CleanupStack::PopAndDestroy(2, entry);//emailMsg, entry

	TestCase().INFO_PRINTF2(_L("Test Action %S completed."), &KTestActionSmtpModifyAttachmentInfo);
	TestCase().ActionCompletedL(*this);
	}
void CMtfTestActionSmtpAddLinkedAttachment::RunTestL()
	{
	CMsvSession* paramSession = ObtainParameterReferenceL<CMsvSession>(TestCase(),ActionParameters().Parameter(0));
	TMsvId messageEntry = ObtainValueParameterL<TMsvId>(TestCase(),ActionParameters().Parameter(1));
	HBufC* paramFilePath   = ObtainParameterReferenceL<HBufC>(TestCase(),ActionParameters().Parameter(2));
	HBufC8* paramMimeType = ObtainParameterReferenceL<HBufC8>(TestCase(),ActionParameters().Parameter(3));

	CMsvEntry* entry = paramSession->GetEntryL(messageEntry);
	CleanupStack::PushL(entry);
	
	CImEmailMessage* emailMsg = CImEmailMessage::NewL(*entry);
	CleanupStack::PushL(emailMsg);
	
	CMtfAsyncWaiter* waiter = CMtfAsyncWaiter::NewL();
	CleanupStack::PushL(waiter);
	
	CMsvAttachment* attachment = CMsvAttachment::NewL(CMsvAttachment::EMsvLinkedFile);
	CleanupStack::PushL(attachment);
	
	attachment->SetMimeTypeL(*paramMimeType);
	
	TParse fileNameParser;
	User::LeaveIfError(fileNameParser.Set(*paramFilePath, NULL, NULL));
	attachment->SetAttachmentNameL(fileNameParser.NameAndExt());
	
	TEntry fileEntry;
	User::LeaveIfError(paramSession->FileSession().Entry( fileNameParser.FullName(), fileEntry));
	attachment->SetSize(fileEntry.iSize);

	MMsvAttachmentManager& manager = emailMsg->AttachmentManager();
	
	manager.AddLinkedAttachmentL(*paramFilePath, attachment, waiter->iStatus);
	CleanupStack::Pop(attachment); // ownership passed to manager
	waiter->StartAndWait();
	User::LeaveIfError(waiter->Result());
	CleanupStack::PopAndDestroy(waiter);
	
	attachment = NULL;
	
	TInt attachmentCount = manager.AttachmentCount();
	attachment = manager.GetAttachmentInfoL(attachmentCount - 1);
	CleanupStack::PushL(attachment);
	
	TMsvAttachmentId attachmentId = attachment->Id();
	
	CleanupStack::PopAndDestroy(attachment);
	
	CleanupStack::PopAndDestroy(2, entry); // emailMsg, entry
	
	StoreParameterL<TMsvAttachmentId>(TestCase(),attachmentId,ActionParameters().Parameter(4));
	}
TInt32 CSmtpClientMtm::GetAttachmentSizeL(CImEmailMessage& aMessage, TMsvId aMsvId)
	{
	// Calculate the total size of all attachments associated with this message
	TInt total=0;
	aMessage.GetAttachmentsListL(iWait->iStatus, aMsvId, CImEmailMessage::EAllAttachments,CImEmailMessage::EThisMessageOnly);
	iWait->Start();		// wait for the asynch operation to complete
	TInt numAttachments=aMessage.AttachmentManager().AttachmentCount();
	for (TInt n=0 ; n<numAttachments ; ++n)
		{
		CMsvAttachment* attachment = aMessage.AttachmentManager().GetAttachmentInfoL(n);
		total+=attachment->Size();
		delete attachment;
		}
	return total;
	}
void CMtfTestActionSmtpRemoveEntryAttachmentById::RunTestL()
	{
	CMsvSession* paramSession = ObtainParameterReferenceL<CMsvSession>(TestCase(),ActionParameters().Parameter(0));
	TMsvId messageEntry = ObtainValueParameterL<TMsvId>(TestCase(),ActionParameters().Parameter(1));
	TMsvAttachmentId attachId = ObtainValueParameterL<TMsvAttachmentId>(TestCase(),ActionParameters().Parameter(2));

	CMsvEntry* entry = paramSession->GetEntryL(messageEntry);
	CleanupStack::PushL(entry);
	
	CImEmailMessage* emailMsg = CImEmailMessage::NewL(*entry);
	CleanupStack::PushL(emailMsg);
	
	MMsvAttachmentManager& manager = emailMsg->AttachmentManager();
	
	CMsvAttachment* attachInfo = manager.GetAttachmentInfoL(attachId);
	CleanupStack::PushL(attachInfo);
	
	// First ensure that the attachment is a file attachment
	if( attachInfo->Type() != CMsvAttachment::EMsvMessageEntry )
		{
		User::Leave(KErrGeneral);
		}
		
	// Get the linked file
	TMsvId msgId = attachInfo->EntryAttachmentId();
	CleanupStack::PopAndDestroy(attachInfo);
		
	// Remove the attachment
	CMtfAsyncWaiter* waiter = CMtfAsyncWaiter::NewL();
	CleanupStack::PushL(waiter);
	manager.RemoveAttachmentL(attachId, waiter->iStatus);
	waiter->StartAndWait();
	User::LeaveIfError(waiter->Result());
	CleanupStack::PopAndDestroy(waiter);	
	
	CleanupStack::PopAndDestroy(2, entry); // email msg, entry

	// Ensure that the message entry still exists
	CMsvEntry* checkEntry = paramSession->GetEntryL(msgId);
	delete checkEntry;
	checkEntry = NULL;
	}
void CSmtpClientMtm::GetBodyTextL(CImEmailMessage& aMessage, TMsvId aMsvId)
	{
	CRichText* messageText = CRichText::NewL(iParaFormatLayer, iCharFormatLayer);
	CleanupStack::PushL(messageText);

	aMessage.GetBodyTextL(aMsvId, CImEmailMessage::EThisMessageOnly, *messageText,*iParaFormatLayer,*iCharFormatLayer);

	Body().Reset();
	Body().AppendTakingSolePictureOwnershipL(*messageText);

	CleanupStack::PopAndDestroy();
	}
void CTestCImPlainTextStore::TestStoreRichTextAsPlainTextL()
	{	
	CreateNewPlaintextMessageL();
	
	CMsvEntry* entry = testUtils->iMsvSession->GetEntryL(newMessageId);
	CImEmailMessage* imEmailMessage = CImEmailMessage::NewLC(*entry);
	
	iCImPlainBodyTextStore = imEmailMessage->OpenPlainBodyTextForWriteL();

	CParaFormatLayer* paraLayer = CParaFormatLayer::NewL();
	
	CCharFormatLayer* charLayer = CCharFormatLayer::NewL();
		
	CRichText* richText=CRichText::NewL(paraLayer, charLayer, CEditableText::EFlatStorage, KBufferGranularity);


	TBuf<50> theMessageContents = _L("Test message contents");
	richText->InsertL(0, theMessageContents);
	TInt textLength = richText->DocumentLength();
	
	iCImPlainBodyTextStore->StoreRichTextAsPlainTextL(*richText);
	iCImPlainBodyTextStore->CommitL(iActiveWaiter->iStatus);
	iActiveWaiter->WaitActive();
	
	delete richText;

	richText=CRichText::NewL(paraLayer, charLayer, CEditableText::EFlatStorage, KBufferGranularity);
	imEmailMessage->GetBodyTextL(newMessageId, CImEmailMessage::EThisMessageOnly, *richText, *paraLayer, *charLayer);	
	richText->DocumentLength();
	ASSERT_EQUALS(richText->DocumentLength(), textLength);

	delete richText;
	delete charLayer;
	delete paraLayer;
	CleanupStack::PopAndDestroy(imEmailMessage); 
	}	
/*
-----------------------------------------------------------------------------
----------------------------------------------------------------------------
*/
void CMailBoxContainer::ReadMailFolderL(TMsvId aSelected)
{
	iCurrentMailBox = aSelected;

	delete iIDArray;
	iIDArray = NULL;
	
	if(iSession && iSelectionBox)
	{
		TInt OrgCount = iSelectionBox->Model()->ItemTextArray()->MdcaCount();
		STATIC_CAST(CDesCArray*,iSelectionBox->Model()->ItemTextArray())->Reset();
		
		RPointerArray<CFFileTypeItem> ItemTypeArray  = iUtils.GetFileUtils().GetItemTypeArray();

		iIDArray = new(ELeave)CArrayFixFlat<TMyMailEntry>(10);
	
		TMsvSelectionOrdering sort;
		sort.SetShowInvisibleEntries(ETrue);
		sort.SetSorting(EMsvSortByDate);
		// Take a handle to the folder entry
		CMsvEntry* parentEntry = CMsvEntry::NewL(*iSession,aSelected,sort);
		CleanupStack::PushL(parentEntry);
		
		// A selection of all BT entries
		CMsvEntrySelection* entries = parentEntry->ChildrenL();//ChildrenWithMtmL(KUidMsgTypeBt);
		CleanupStack::PushL(entries);
			
	
		//Process all entries
		for(TInt ii = 0; ii < entries->Count(); ii++)
		{
			//Get entry
			CMsvEntry* btEntry = iSession->GetEntryL((*entries)[ii]);
			CleanupStack::PushL(btEntry);
			
			//Then get entrys child
			CMsvEntrySelection* btChildren = btEntry->ChildrenL();
			CleanupStack::PushL(btChildren);
			
			TInt childc = btChildren->Count();
			if (childc>0)
			{
				for(TInt i=0; i<childc; i++)
				{
					TMsvId btAtt = (*btChildren)[i];
					btEntry->SetEntryL(btAtt);
				
					if(btEntry->Entry().iMtm == KUidMsgTypePOP3)
					{
						CImEmailMessage* emailMessage = CImEmailMessage::NewL(*btEntry);
						CleanupStack::PushL(emailMessage);
						
						MMsvAttachmentManager& attMngr = emailMessage->AttachmentManager(); 
						GetAttachmentsL(attMngr,*btEntry,(*entries)[ii],btAtt);
						
						CleanupStack::PopAndDestroy(emailMessage);
					}
					else if (btEntry->HasStoreL())
					{
						CMsvStore* store = btEntry->ReadStoreL();
						CleanupStack::PushL(store);
						
						MMsvAttachmentManager& attMngr = store->AttachmentManagerL();
						GetAttachmentsL(attMngr,*btEntry,(*entries)[ii],btAtt);
						
						CleanupStack::PopAndDestroy(store);
					}

				}
				
			}
			
			CleanupStack::PopAndDestroy(2);
		}	
					
		CleanupStack::PopAndDestroy(2);

		CDesCArray* itemArray = STATIC_CAST( CDesCArray* ,iSelectionBox->Model()->ItemTextArray());		
		if(itemArray->Count() > OrgCount)
		{
			iSelectionBox->HandleItemAdditionL();
			iSelectionBox->SetCurrentItemIndex(0); 
		}
		else
		{
			iSelectionBox->HandleItemRemovalL();
			
			if(itemArray->Count())
			{
				iSelectionBox->SetCurrentItemIndex(0); 
			}
		}
		
		UpdateScrollBar(iSelectionBox);
		DrawNow();
	}
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
	}
void CTestCImPlainTextStore::TestStoreRestore16BitChunkAsyncL()
	{
	CreateNewPlaintextMessageL();
	
	CMsvEntry* entry = testUtils->iMsvSession->GetEntryL(newMessageId);
	CImEmailMessage* imEmailMessage = CImEmailMessage::NewLC(*entry);
	
	iCImPlainBodyTextStore = imEmailMessage->OpenPlainBodyTextForWriteL();

	iCImPlainBodyTextStore->StoreChunkL(iFilebuf1->Des(),iActiveWaiter->iStatus);
	iActiveWaiter->WaitActive();
	TInt bodyTextSize = iFilebuf1->Length();
		
	iCImPlainBodyTextStore->StoreChunkL(iFilebuf2->Des(),iActiveWaiter->iStatus);
	iActiveWaiter->WaitActive();
	bodyTextSize += iFilebuf2->Length();
	
	iCImPlainBodyTextStore->StoreChunkL(iFilebuf3->Des(),iActiveWaiter->iStatus);
	iActiveWaiter->WaitActive();
	bodyTextSize += iFilebuf3->Length();
	
	iCImPlainBodyTextStore->CommitL(iActiveWaiter->iStatus);
	iActiveWaiter->WaitActive();
	
	delete iCImPlainBodyTextStore;
	iCImPlainBodyTextStore = NULL;
	
	//Restore the data in chunks.	
	HBufC16* filebuf11 = HBufC16::NewL(100+KTestStringLength);

	
	TInt origSize = bodyTextSize;
	TInt sizeOfRestoredText = 0;

	TPtr bufPtr = filebuf11->Des();
	TInt pos = 0;
	
	iCImPlainBodyTextStore = imEmailMessage->OpenPlainBodyTextForReadL(CImEmailMessage::EThisMessageOnly,100);
	iCImPlainBodyTextStore->NextChunkL(bufPtr,iActiveWaiter->iStatus);
	iActiveWaiter->WaitActive();
	
	RArray<TPtr> chunkArray;
	chunkArray.Append(bufPtr);
	
	sizeOfRestoredText += bufPtr.Length();
	
	while(bufPtr.Length() > 0 && bufPtr.Length() == 100)
		{
		iCImPlainBodyTextStore->NextChunkL(bufPtr,iActiveWaiter->iStatus);
		iActiveWaiter->WaitActive();
		// put retrived buffer in a array.
		chunkArray.Append(bufPtr);
		sizeOfRestoredText += bufPtr.Length();
		}

	ASSERT_EQUALS(bodyTextSize, sizeOfRestoredText);

	for(TInt j=chunkArray.Count() - 1;j >0; --j )
		{
		bufPtr.SetLength(0);
		iCImPlainBodyTextStore->PreviousChunkL(bufPtr,iActiveWaiter->iStatus);
		iActiveWaiter->WaitActive();
		// Compare chunk retrieved with chunk retrieved by NextChunkL
		TInt comp = bufPtr.Compare(chunkArray[j-1]);
		ASSERT_EQUALS(comp, 0);
		}
	CleanupStack::PopAndDestroy(imEmailMessage); 
	}
LOCAL_C TMsvId CreateNewPlaintextMessageWithIncompleteAttachmentL()
	{
	TMsvEmailTypeList msvEmailTypeList = 0;
	TMsvPartList partList = 0;

	CTestActive* testActive = new (ELeave) CTestActive();
	CleanupStack::PushL(testActive);
	partList = (KMsvMessagePartBody | KMsvMessagePartAttachments);
	CImEmailOperation* emailOperation = CImEmailOperation::CreateNewL(testActive->iStatus, *(testUtils->iMsvSession), KMsvGlobalInBoxIndexEntryId, partList, msvEmailTypeList, KUidMsgTypeSMTP);
	CleanupStack::PushL(emailOperation);

	testActive->StartL();
	CActiveScheduler::Start();
	if (testActive->iStatus.Int())
		{
		testUtils->WriteComment(_L("CreateNewPlaintextMessageWithIncompleteAttachmentL failed"));
		testUtils->TestHarnessFailed(testActive->iStatus.Int());
		}

	//check progress
	TMsvId temp;	
	TPckgC<TMsvId> paramPack(temp);
	const TDesC8& progBuf1 = emailOperation->ProgressL();	
	paramPack.Set(progBuf1);
	TMsvId messageId = paramPack();	

	CleanupStack::PopAndDestroy(2); // testActive, emailOperation

	//
	// Add an attachment
	//
	testActive = new (ELeave) CTestActive();
	CleanupStack::PushL(testActive);

	_LIT(KAttachment1, "C:\\mailtest\\IMCM\\rfc82201.txt");
	_LIT(KAttachment2, "C:\\mailtest\\IMCM\\rfc82202.txt");

	CImEmailMessage* 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

	if (testActive->iStatus.Int())
		{
		testUtils->WriteComment(_L("CreateNewPlaintextMessageWithIncompleteAttachmentL failed"));
		testUtils->TestHarnessFailed(testActive->iStatus.Int());
		}

	testUtils->iMsvEntry->SetEntryL(messageId);
	attachmentInfo = CMsvAttachment::NewL(CMsvAttachment::EMsvFile);
	CleanupStack::PushL(attachmentInfo);
	imEmailMessage->AttachmentManager().AddAttachmentL(KAttachment2, attachmentInfo, testActive->iStatus);
	CleanupStack::Pop(attachmentInfo);	

	testActive->StartL();
	CActiveScheduler::Start();	// wait for the asynch operation to complete

	if (testActive->iStatus.Int())
		{
		testUtils->WriteComment(_L("CreateNewPlaintextMessageWithIncompleteAttachmentL failed"));
		testUtils->TestHarnessFailed(testActive->iStatus.Int());
		}
	const TDesC8& progBuf2 = imEmailMessage->ProgressL();	
	paramPack.Set(progBuf2);
	TMsvId attachmentId = paramPack();	

	CleanupStack::PopAndDestroy(2); // imEmailMessage, testActive

	//
	// Set the attachment to be not complete
	//
	
	testUtils->iMsvEntry->SetEntryL(attachmentId);
	TMsvEmailEntry attachmentEntry = testUtils->iMsvEntry->Entry();
	attachmentEntry.SetComplete(EFalse);
	testUtils->iMsvEntry->ChangeL(attachmentEntry);

	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
	}
LOCAL_C void CreateNewPlaintextMessageL()
	{
	CTestActive* testActive = new(ELeave) CTestActive();
	CleanupStack::PushL(testActive);
	TMsvEmailTypeList msvEmailTypeList = 0;
	TMsvPartList partList = (KMsvMessagePartBody | KMsvMessagePartAttachments);
	CImEmailOperation* emailOperation = CImEmailOperation::CreateNewL(testActive->iStatus, *(testUtils->iMsvSession),KMsvGlobalOutBoxIndexEntryId, partList, msvEmailTypeList, KUidMsgTypeSMTP);
	CleanupStack::PushL(emailOperation);
	TestMsvOperationTimer* testMsvOperationTimer = TestMsvOperationTimer::NewLC(test.Console(), emailOperation, test);
	testActive->StartL();
	testMsvOperationTimer->IssueRequest();
	CActiveScheduler::Start();
	//check progress
	TMsvId temp;
	TPckgC<TMsvId> paramPack(temp);
	const TDesC8& progBuf = emailOperation->ProgressL();
	paramPack.Set(progBuf);
	TMsvId newMessageId;
	newMessageId = paramPack();
	test(newMessageId != NULL);
	CImEmailMessage* imEmailMessage = CImEmailMessage::NewLC(*(testUtils->iMsvEntry));
	// Make sure you are set on the Message Id
	testUtils->iMsvEntry->SetEntryL(newMessageId);
	CMsvAttachment* attachmentInfo = CMsvAttachment::NewL(CMsvAttachment::EMsvFile);
	CleanupStack::PushL(attachmentInfo);
	attachmentInfo->SetAttachmentNameL(KAttachmentFileName1());
	CleanupStack::Pop(attachmentInfo);	 // ownership passed to attachment manager
	imEmailMessage->AttachmentManager().AddAttachmentL(KAttachment1, attachmentInfo, testActive->iStatus);
	testActive->StartL();
	CActiveScheduler::Start();	// wait for the asynch operation to complete
	if (testActive->iStatus.Int())
		{
		testUtils->WriteComment(_L("CreateNewPlaintextMessageWithIncompleteAttachmentL failed"));
		testUtils->TestHarnessFailed(testActive->iStatus.Int());
		}
	testUtils->iMsvEntry->SetEntryL(newMessageId);
	CMsvAttachment* attachmentInfo1 = CMsvAttachment::NewL(CMsvAttachment::EMsvFile);
	CleanupStack::PushL(attachmentInfo1);
	attachmentInfo1->SetAttachmentNameL(KAttachmentFileName2());
    CleanupStack::Pop(attachmentInfo1);	 // ownership passed to attachment manager
	imEmailMessage->AttachmentManager().AddAttachmentL(KAttachment2, attachmentInfo1, testActive->iStatus);
	testActive->StartL();
	CActiveScheduler::Start();	// wait for the asynch operation to complete
	if (testActive->iStatus.Int())
		{
		testUtils->WriteComment(_L("CreateNewPlaintextMessageWithIncompleteAttachmentL failed"));
		testUtils->TestHarnessFailed(testActive->iStatus.Int());
		}
	testUtils->iMsvEntry->SetEntryL(newMessageId);
	CMsvAttachment* attachmentInfo2 = CMsvAttachment::NewL(CMsvAttachment::EMsvFile);
	CleanupStack::PushL(attachmentInfo2);
	attachmentInfo2->SetAttachmentNameL(KAttachmentFileName3());
    CleanupStack::Pop(attachmentInfo2);	 // ownership passed to attachment manager
	imEmailMessage->AttachmentManager().AddAttachmentL(KAttachment3, attachmentInfo2, testActive->iStatus);
	testActive->StartL();
	CActiveScheduler::Start();	// wait for the asynch operation to complete
	if (testActive->iStatus.Int())
		{
		testUtils->WriteComment(_L("CreateNewPlaintextMessageWithIncompleteAttachmentL failed"));
		testUtils->TestHarnessFailed(testActive->iStatus.Int());
		}	
	const TDesC8& progBuf2 = imEmailMessage->ProgressL();	
	paramPack.Set(progBuf2);
	TMsvId attachmentId = paramPack();
	testUtils->WriteComment(_L("\t Created New plaintext message"));
	testUtils->iMsvEntry->SetEntryL(attachmentId);
	
    imEmailMessage->GetAttachmentsListL(testActive->iStatus, newMessageId, CImEmailMessage::EAllAttachments, CImEmailMessage::EThisMessageOnly);	
  	testActive->StartL();
	CActiveScheduler::Start();	// wait for the asynch operation to complete
    const CMsvEntrySelection& selection = imEmailMessage->Selection();
	testUtils->Printf(KTotalNoOfAttachment , selection.Count());
	imEmailMessage->GetBodyTextEntryIdL(testActive->iStatus,newMessageId,CImEmailMessage::EThisMessageOnly);
	testActive->StartL();
	CActiveScheduler::Start();	// wait for the asynch operation to complete
	const CMsvEntrySelection& selection1 = imEmailMessage->Selection();
	TInt count = imEmailMessage->Selection().Count();
    if( count > 0)
			{
			// Get the Id of the body text
			// The body text part Id is present at index 0.  Not checking for other
			//  Ids, as currently only Plain text part is verified.
			TMsvId	iBodyTextId = imEmailMessage->Selection().At(0);
			testUtils->Printf(KBodytextId, iBodyTextId);
			}
	CleanupStack::PopAndDestroy(); // imEmailMessage  
		testUtils->iMsvEntry->SetEntryL(attachmentId);
    CMsvStore* store1 = testUtils->iMsvEntry->EditStoreL();
	CleanupStack::PushL(store1);
	CImMimeHeader *Head1 = CImMimeHeader::NewLC();
    Head1->RestoreL(*store1);
	const TPtrC8 contentSubType = Head1->ContentSubType();
	store1->CommitL();
    
    if(contentSubType.Size() == 0 )
    {
    testUtils->Printf(_L("ContentSubType Has NO SUB CONTENT****") );
    test(contentSubType.Size() != 0);
    }
    else
    {
    testUtils->Printf(_L("ContentSubType Has ****SUB CONTENT****"));	
    }
   	CleanupStack::PopAndDestroy(2, store1);//store1,Head1,Buffer
    testUtils->iMsvEntry->SetEntryL(newMessageId);
    CMsvStore* store = testUtils->iMsvEntry->EditStoreL();
	CleanupStack::PushL(store);
	CImHeader* header = CImHeader::NewLC();
	header->RestoreL(*store);
	TInt serverLength = testUtils->MachineName().Length();
	HBufC* serverad = HBufC::NewL(serverLength + 7 ); // 7 is the length of KServerAddress
	CleanupStack::PushL(serverad);
	serverad->Des().Copy(testUtils->MachineName());
	serverad->Des().Append(KServer);	
    HBufC* emailaddress = HBufC::NewL(serverLength + 8 + serverad->Des().Length() + 1 );
   	CleanupStack::PushL(emailaddress);
   	emailaddress->Des().Copy(testUtils->MachineName());
   	emailaddress->Des().Append(KAt);
   	emailaddress->Des().Append(*serverad);
   	header->ToRecipients().AppendL(*emailaddress);
	header->SetSubjectL(_L("Test sending message using CMsvEntry::CopyL!!"));
	header->StoreL(*store);
	store->CommitL();
	CleanupStack::PopAndDestroy(emailaddress);
	CleanupStack::PopAndDestroy(serverad);
	CleanupStack::PopAndDestroy(5,testActive); // header,store,testMsvOperationTimer,emailOperation,testActive
	
		
	}
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;
	}
void CMtfTestActionSmtpModifyAttachmentInfo::CompareMimeHeadersL(CImEmailMessage& imEmailMsg, TMsvAttachmentId aAttachId)
{
	CMsvAttachment* attachment = imEmailMsg.AttachmentManager().GetAttachmentInfoL(aAttachId);
	CleanupStack::PushL(attachment);
	
	CMsvMimeHeaders* mimeHeaders = CMsvMimeHeaders::NewL();
	CleanupStack::PushL(mimeHeaders);
	
	mimeHeaders->RestoreL(*attachment);
	if( mimeHeaders->ContentDescription().Compare(KTxtContentDescription()) != 0 )
		User::Leave(KErrNotFound);
	if( mimeHeaders->ContentBase().Compare(KTxtContentBase()) != 0 )
		User::Leave(KErrNotFound);
	if( mimeHeaders->ContentLocation().Compare(KTxtContentLocation()) != 0 )
		User::Leave(KErrNotFound);
	if( mimeHeaders->ContentId().Compare(KTxtContentId()) != 0 )
		User::Leave(KErrNotFound);
	if( mimeHeaders->ContentType().Compare(KTxtContentType()) != 0 )
		User::Leave(KErrNotFound);
	if( mimeHeaders->ContentSubType().Compare(KTxtContentSubType()) != 0 )
		User::Leave(KErrNotFound);
	if( mimeHeaders->ContentDisposition().Compare(KTxtContentDisposition()) != 0 )
		User::Leave(KErrNotFound);
	
	CDesC8Array& desArray1 = mimeHeaders->ContentTypeParams();
	if( desArray1.Count() == 3 )
		{
		if( desArray1[0].Compare(KTxtParam1()) != 0 )
			User::Leave(KErrNotFound);
		if( desArray1[1].Compare(KTxtParam2()) != 0 )
			User::Leave(KErrNotFound);
		if( desArray1[2].Compare(KTxtParam3()) != 0 )
			User::Leave(KErrNotFound);
		}
	else
		User::Leave(KErrNotFound);
	
	CDesC8Array& desArray2 = mimeHeaders->ContentDispositionParams();
	if( desArray2.Count() == 3 )
		{
		if( desArray2[0].Compare(KTxtParam4()) != 0 )
			User::Leave(KErrNotFound);
		if( desArray2[1].Compare(KTxtParam5()) != 0 )
			User::Leave(KErrNotFound);
		if( desArray2[2].Compare(KTxtParam6()) != 0 )
			User::Leave(KErrNotFound);
		}
	else
		User::Leave(KErrNotFound);

	CDesC8Array& desArray3 = mimeHeaders->XTypeParams();
	if( desArray3.Count() == 3 )
		{
		if( desArray3[0].Compare(KTxtParam7()) != 0 )
			User::Leave(KErrNotFound);
		if( desArray3[1].Compare(KTxtParam8()) != 0 )
			User::Leave(KErrNotFound);
		if( desArray3[2].Compare(KTxtParam9()) != 0 )
			User::Leave(KErrNotFound);
		}
	else
		User::Leave(KErrNotFound);
	
	CleanupStack::PopAndDestroy(2, attachment); // mimeHeaders, attachment
}
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); 

	}