Creating, Replying and Forwarding Email Messages

This section describes how to draft, reply and forward email messages using Email Client APIs.

Perform the steps described in Listing, Accessing and Launching Mailboxes.

There are three ways to create messages:

  • Creating a draft message by drafting a new email.

  • Creating a reply message by replying to an existing email.

  • Creating a forward message by forwarding an existing email.

Create a message.

Messages can be created using one of the following methods:

  • Create a new draft message.

    MEmailMailbox* mailbox = mailboxes[i];
    MEmailMessage* message = mailbox->CreateDraftMessageL();
    CleanupReleasePushL(*message);
    
  • Create a reply message.

    MEmailMailbox* mailbox = mailboxes[i];
    MEmailMessage* message = mailbox->CreateReplyMessageL();
    CleanupReleasePushL(*message);
    
  • Create a forward message.

    MEmailMailbox* mailbox = mailboxes[i];
    MEmailMessage* message = mailbox->CreateForwardMessageL();
    CleanupReleasePushL(*message);
    

The following code snippet shows an example of how to create a draft email message.

TInt CEmailClientApiTester::CreateDraftMessageL( CItemParser& /*aItem*/ )
	{
		
		TInt ret( KErrNone);
		MEmailClientApi* mailClient = CreateFactoryAndClientApiLC();
		RMailboxPtrArray mailboxes;
		CleanupResetAndRelease<MEmailMailbox>::PushL( mailboxes );
		mailClient->GetMailboxesL( mailboxes );
		for ( TInt i=0;i<mailboxes.Count(); i++) 
			{
		    MEmailMailbox* mailbox = mailboxes[i];
				MEmailMessage* message = mailbox->CreateDraftMessageL();
				CleanupReleasePushL( *message );
	  		MEmailAddress* replyto = message->ReplyToAddressL();
				if (replyto)
				{
					iLog->Log( _L( "reply-to: %S %S" ), &replyto->DisplayName(), replyto->Address());
				}
				REmailAddressArray array;
				CleanupResetAndRelease<MEmailAddress>::PushL( array );
				CEmailInterfaceFactory* factory = CEmailInterfaceFactory::NewL();
				CleanupStack::PushL( factory );
				MEmailAddress* address = static_cast<MEmailAddress*>(
				factory->InterfaceL( KEmailIFUidAddress ) );        
				CleanupReleasePushL( *address );
         
				address->SetAddressL( _L("abc.sender@xyz.com") );
				address->SetDisplayNameL( _L("Abc Sender") );
				message->SetReplyToAddressL(*address);        
				address->SetAddressL( _L("etu.suku@domain.fi") );
				address->SetDisplayNameL( _L("Etu Suku") );
				array.Append( address );
				message->SetRecipientsL( MEmailAddress::ETo, array ); 
				CleanupStack::PopAndDestroy(); // address
				message->SetFlag( EFlag_Important );
				message->SetSubjectL( _L("Overlook by night") );
				message->SetPlainTextBodyL( _L("Here's Johnny!") );
				message->SaveChangesL();
				iLog->Log( _L( "%S id=%d " ),&mailbox>MailboxName(),  
				mailbox->MailboxId().iId);
				iLog->Log( _L( "msgid=%d folderid=%d" ), 
				message->MessageId(), 
				message->ParentFolderId() );
				message->Flags() ); //Message Flags
				REmailAddressArray array2;
				CleanupResetAndRelease<MEmailAddress>::PushL( array2 );
				TInt count = message->GetRecipientsL( MEmailAddress::EUndefined, array2 );
				iLog->Log( _L( "recipient count= %d" ), count);                                   
				for (TInt i = 0; i < count; i++)
				{
					iLog->Log( _L( "recipient %S %S, role %d" ), &array2[i]->DisplayName(), &array2[i]->Address(), array2[i]->Role());            
				}
				MEmailAddress* reply = message->ReplyToAddressL();
				iLog->Log( _L( "reply-to: %S %S" ), &reply->DisplayName(), &reply->Address());
				/* remove the last recipient */
				message->RemoveRecipientL(*array2[count-1]);        
				CleanupStack::PopAndDestroy(); // array2
				CleanupResetAndRelease<MEmailAddress>::PushL( array2 );
				TInt respcount = message->GetRecipientsL( MEmailAddress::EUndefined, array2 );
				iLog->Log( _L( "recipient count= %d" ), respcount); 
                          
				for (TInt i = 0; i < respcount; i++)
				{
				 iLog->Log( _L( "recipient %S %S, role %d" ), &array2[i]->DisplayName(), &array2[i]->Address(), array2[i]->Role());            
				}
				message->SaveChangesL();
				CleanupStack::PopAndDestroy(4); // message, array, factory, array2
	}    
	CleanupStack::PopAndDestroy( 3 ); // mailboxes, mailclient, factory
	iLog->Log( _L( "CreateDraftMessageL ends ret=%d" ), ret );
	return ret;
}
Related concepts
Email Client API Overview