diff -r 89d6a7a84779 -r 25a17d01db0c Symbian3/Examples/guid-6013a680-57f9-415b-8851-c4fa63356636/pop3_example_8cpp-source.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Symbian3/Examples/guid-6013a680-57f9-415b-8851-c4fa63356636/pop3_example_8cpp-source.html Fri Jan 22 18:26:19 2010 +0000 @@ -0,0 +1,386 @@ + + +TB10.1 Example Applications: examples/Messaging/Pop3Example/pop3Example.cpp Source File + + + + +

examples/Messaging/Pop3Example/pop3Example.cpp

Go to the documentation of this file.
00001 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
+00002 // All rights reserved.
+00003 // This component and the accompanying materials are made available
+00004 // under the terms of "Eclipse Public License v1.0"
+00005 // which accompanies this distribution, and is available
+00006 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
+00007 //
+00008 // Initial Contributors:
+00009 // Nokia Corporation - initial contribution.
+00010 //
+00011 // Contributors:
+00012 //
+00013 // Description:
+00014 // This is a simple example code that demonstrates the usage of the POP3 protocol to retrieve emails.
+00015 // The code creates a simple SMTP mail, sends it and then retrieves the same using POP. 
+00016 //
+00017 
+00018 
+00019 
+00023 #include "pop3Example.h"
+00024 #include "MAsyncWaiter.h"
+00025 
+00026 _LIT(KTextPressAKey, "\n\n Press any key to step through the example");
+00027 _LIT(KExit,"\n\n Press any key to exit the application ");
+00028 _LIT(KCreateMtm,"\n\n Creating a Pop3 client MTM");
+00029 _LIT(KKeyPress,"\n\n Press any key");
+00030 _LIT(KTxtAccountName, "PopAccount");
+00031 _LIT(KCreatePopAccount,"\n\n Creating a Pop account");
+00032 _LIT(KCreateSmtpAccount,"\n\n Creating an Smtp account");
+00033 _LIT(KSmtpServerAddress, "ban-sindhub01.intra");
+00034 _LIT(KEmailAlias, "Messaging example");
+00035 _LIT(KSmtpEmailAddress, "ban-sindhub01@ban-sindhub01.intra");
+00036 _LIT8(KFrom, "ban-sindhub01@ban-sindhub01.intra");
+00037 _LIT(KTo, "ban-sindhub01@ban-sindhub01.intra");
+00038 _LIT(KSubject, "SimpleEmail");
+00039 _LIT(KBodyContents, "This is a very simple mail");
+00040 _LIT(KSendMail,"\n\n Sending the mail... please wait");
+00041 _LIT(KPopServer, "ban-sindhub01.intra");        
+00042 _LIT8(KPopPassword,"ban-sindhub01");
+00043 _LIT8(KPopLoginName,"ban-sindhub01");
+00044 _LIT(KWait,"\n\n Connecting to pop3 server");
+00045 _LIT(KDownload,"\n\n Downloading mail from the pop3 server");
+00046 _LIT(KDisconnect,"\n\n Press any key to disconnect");
+00047         
+00052 CPop3Example* CPop3Example::NewL()
+00053         {
+00054         CPop3Example* self = new (ELeave) CPop3Example();
+00055         CleanupStack::PushL(self);
+00056         self->ConstructL();
+00057         CleanupStack::Pop();
+00058         return self;
+00059         }
+00063 CPop3Example::CPop3Example()
+00064         {
+00065 
+00066         }
+00067         
+00068 void CPop3Example::ConstructL()
+00069         {
+00070         iConsole = Console::NewL(KTitle,TSize(KConsFullScreen,KConsFullScreen));
+00071         iConsole->Printf ( KTextPressAKey );
+00072         iConsole->Getch ();
+00073         }
+00074 
+00078 CPop3Example::~CPop3Example()
+00079         {
+00080         iConsole->Printf(KExit);
+00081         iConsole->Getch();
+00082         delete iMtm;
+00083         iMtm = NULL;
+00084         delete iClientRegistry;
+00085         iClientRegistry  = NULL;
+00086         delete iSession;                
+00087         iSession = NULL;
+00088         delete iConsole;
+00089         }
+00090 
+00096 void CPop3Example::CreateClientRegistryL()
+00097         {
+00098         // Create a message server session.
+00099         iSession = CMsvSession::OpenSyncL(*this);
+00100         CleanupStack::PushL(iSession);
+00101 
+00102         // Create a client-side MTM registry.
+00103         iClientRegistry = CClientMtmRegistry::NewL(*iSession,KMsvDefaultTimeoutMicroSeconds32);
+00104         if (iClientRegistry != NULL && iClientRegistry->IsPresent(KUidMsgTypePOP3))
+00105                 {
+00106                 CleanupStack::PushL(iClientRegistry);
+00107                 }
+00108         // Create a Client-side MTM object for the specified MTM UID.
+00109         iMtm = (CPop3ClientMtm*)iClientRegistry->NewMtmL(KUidMsgTypePOP3); 
+00110         iConsole->Printf(KCreateMtm);
+00111         iConsole->Printf(KKeyPress);
+00112         iConsole->Getch();
+00113         CleanupStack::Pop(2,iSession); //iClientRegistry,iSession
+00114          
+00115     }
+00116 
+00122 void CPop3Example::CreatePopAndSmtpAccountL()
+00123         {
+00124         CEmailAccounts* emailAccounts = CEmailAccounts::NewLC();
+00125         CImPop3Settings* settings = new(ELeave) CImPop3Settings();
+00126         CleanupStack::PushL(settings);
+00127         
+00128         CImIAPPreferences* popIAP = CImIAPPreferences::NewLC();
+00129         
+00130         // Set the server address to system address
+00131         settings->SetServerAddressL(KPopServer);
+00132         settings->SetLoginNameL(KPopLoginName); 
+00133         settings->SetPasswordL(KPopPassword);
+00134         settings->SetPort(110);
+00135         
+00136         // Create a Pop account
+00137         iConsole->Printf(KCreatePopAccount);
+00138         iPopAccount = emailAccounts->CreatePopAccountL(KTxtAccountName, *settings,*popIAP,EFalse);
+00139         
+00140         CImSmtpSettings *smtpSettings = new (ELeave) CImSmtpSettings();
+00141         CleanupStack::PushL(smtpSettings);
+00142 
+00143         emailAccounts->PopulateDefaultSmtpSettingsL(*smtpSettings, *popIAP);
+00144         
+00145         // Create an Smtp acoount
+00146         iConsole->Printf(KCreateSmtpAccount);
+00147         iSmtpAccount = emailAccounts->CreateSmtpAccountL(iPopAccount, *smtpSettings, *popIAP, EFalse);
+00148         
+00149         emailAccounts->SetDefaultSmtpAccountL(iSmtpAccount);
+00150 
+00151         smtpSettings->SetServerAddressL(KSmtpServerAddress);
+00152         smtpSettings->SetEmailAliasL(KEmailAlias);
+00153         smtpSettings->SetEmailAddressL(KSmtpEmailAddress);
+00154         smtpSettings->SetReplyToAddressL(KSmtpEmailAddress);
+00155         smtpSettings->SetReceiptAddressL(KSmtpEmailAddress);
+00156         smtpSettings->SetPort(25);
+00157                 
+00158         // Add IAP to the IAP preferences
+00159         CImIAPPreferences* prefs = CImIAPPreferences::NewLC();
+00160         TImIAPChoice iap;
+00161         TInt iapID = 0;
+00162         CMDBSession* dbSession = CMDBSession::NewL(KCDVersion1_1);
+00163         CleanupStack::PushL(dbSession);
+00164         CCDConnectionPrefsRecord *connPrefRecord = static_cast<CCDConnectionPrefsRecord*>(CCDRecordBase::RecordFactoryL(KCDTIdConnectionPrefsRecord));
+00165         CleanupStack::PushL(connPrefRecord);
+00166         
+00167         // Set the direction of connection
+00168         connPrefRecord->iDirection = ECommDbConnectionDirectionOutgoing;
+00169         connPrefRecord->iRanking = 1;
+00170         if(!connPrefRecord->FindL(*dbSession))
+00171                 {
+00172                 User::Leave(KErrNotFound);              
+00173                 }                       
+00174         iapID = connPrefRecord->iDefaultIAP;
+00175         iap.iIAP = iapID;
+00176         iap.iDialogPref = ECommDbDialogPrefDoNotPrompt;
+00177         prefs->AddIAPL(iap);
+00178         
+00179         emailAccounts->GetSmtpAccountL(iSmtpAccount.iSmtpService, iSmtpAccount);
+00180         emailAccounts->SaveSmtpSettingsL(iSmtpAccount,*smtpSettings);
+00181         emailAccounts->SaveSmtpIapSettingsL(iSmtpAccount, *prefs);
+00182         
+00183         CleanupStack::PopAndDestroy(7,emailAccounts); //connPrefRecord,dbSession,prefs,smtpSettings,popIAP,settings,emailAccounts
+00184         }
+00185         
+00190 void CPop3Example::CreateSmtpMessageL() 
+00191         {
+00192          
+00193         TMsvId outboxId = KMsvGlobalOutBoxIndexEntryId; 
+00194                 
+00195         // Set the context to the folder in which message has to be created
+00196         CMsvEntry*      entry = CMsvEntry::NewL(*iSession,outboxId,TMsvSelectionOrdering());
+00197         CleanupStack::PushL(entry);
+00198         entry->SetEntryL(outboxId);
+00199         
+00200         CMessAsyncWaiter* waiter = CMessAsyncWaiter::NewL();
+00201         CleanupStack::PushL(waiter);
+00202         
+00203         TMsvEmailTypeList msvEmailTypeList = 0;
+00204         TMsvPartList partList = (KMsvMessagePartBody | KMsvMessagePartAttachments);
+00205         
+00206         CImEmailOperation* emailOperation = CImEmailOperation::CreateNewL(waiter->iStatus, *iSession,KMsvGlobalOutBoxIndexEntryId, partList, msvEmailTypeList, KUidMsgTypeSMTP);
+00207         CleanupStack::PushL(emailOperation);
+00208         waiter->StartAndWait();
+00209         
+00210         TMsvId temp;
+00211         TPckgC<TMsvId> paramPack(temp);
+00212         const TDesC8& progBuf = emailOperation->ProgressL();
+00213         paramPack.Set(progBuf);
+00214         TMsvId newMessageId;
+00215         newMessageId = paramPack();
+00216 
+00217         entry->SetEntryL(newMessageId);
+00218 
+00219         CMsvStore* store = entry->EditStoreL();
+00220         CleanupStack::PushL(store);
+00221         CImHeader* emailEntry = CImHeader::NewLC();
+00222         emailEntry->RestoreL(*store);
+00223         emailEntry->SetFromL((TDesC8&)KFrom);
+00224         emailEntry->SetSubjectL((TDesC&)KSubject);
+00225         emailEntry->ToRecipients().AppendL((TDesC&)KTo);
+00226         
+00227         // Paragraph format layer for the rich text object 
+00228         CParaFormatLayer* paraFormatLayer = CParaFormatLayer::NewL();
+00229         CleanupStack::PushL(paraFormatLayer);
+00230         // Character format layer for the rich text object
+00231         CCharFormatLayer* charFormatLayer = CCharFormatLayer::NewL(); 
+00232         CleanupStack::PushL(charFormatLayer);
+00233 
+00234         CRichText* bodyText = CRichText::NewL(paraFormatLayer, charFormatLayer, CEditableText::EFlatStorage, 256);
+00235         CleanupStack::PushL(bodyText);
+00236 
+00237         // Inserts the contents of a buffer into the document at specified position
+00238         bodyText->InsertL(0, KBodyContents);
+00239         store->StoreBodyTextL(*bodyText);
+00240         emailEntry->StoreL(*store);
+00241         // Store the changes permanently
+00242         store->CommitL();
+00243         
+00244         CleanupStack::PopAndDestroy(8,entry); // bodyText,charFormatLayer,paraFormatLayer,emailEntry,store,emailOperation,waiter,entry
+00245 
+00246         }
+00247         
+00254 void CPop3Example::SendSMTPMessageL()
+00255         {
+00256         CMsvEntry* entry = iSession->GetEntryL(iSmtpAccount.iSmtpService);
+00257         CleanupStack::PushL(entry);
+00258 
+00259         TMsvId outboxId = KMsvGlobalOutBoxIndexEntryId;
+00260         
+00261         // creating ChildrenSelection
+00262         TMsvSelectionOrdering order;
+00263         order.SetShowInvisibleEntries(ETrue);
+00264         entry->SetSortTypeL(order);
+00265         
+00266         entry->SetEntryL(outboxId);
+00267         // Get the  selection containing the IDs of all the context children
+00268         CMsvEntrySelection* selection = entry->ChildrenL();
+00269 
+00270         // Fetch the Id of the first entry
+00271         TMsvId entryId = KMsvNullIndexEntryId;
+00272         entryId = (*selection)[0];
+00273         
+00274         delete selection;
+00275 
+00276         CMessAsyncWaiter* waiter = CMessAsyncWaiter::NewL();
+00277         CleanupStack::PushL(waiter);
+00278         
+00279         iConsole->Printf(KSendMail);
+00280         // Create asynchronously, copies of children of the context 
+00281         // as new entries of targetId (smtpServiceId)
+00282         CMsvOperation*  operation = entry->CopyL(entryId,iSmtpAccount.iSmtpService, waiter->iStatus);
+00283 
+00284         CleanupStack::PushL(operation);
+00285         waiter->StartAndWait();
+00286         User::LeaveIfError(waiter->Result());
+00287 
+00288         CleanupStack::PopAndDestroy(3, entry); //operation,waiter,entry
+00289         }
+00290         
+00297 void CPop3Example::ConnectDownloadAndDisconnectPOPServerL()
+00298         {
+00299         
+00300         // Change the current context
+00301         iMtm->SwitchCurrentEntryL(iPopAccount.iPopService);
+00302 
+00303         CMsvEntrySelection* selection = new (ELeave) CMsvEntrySelection;
+00304         CleanupStack::PushL(selection);
+00305         
+00306         // Append the popServiceId onto the end of the array
+00307         selection->AppendL(iPopAccount.iPopService);
+00308                 
+00309         TBuf8<1> param;
+00310         
+00311         CMessAsyncWaiter* waiter = CMessAsyncWaiter::NewL();
+00312         CleanupStack::PushL(waiter);
+00313         
+00314         // Attempts to connect to the Pop3 Service 
+00315         iConsole->Printf(KWait);
+00316         CMsvOperation* connectOperation = iMtm->InvokeAsyncFunctionL(KPOP3MTMConnect,*selection,param, waiter->iStatus);
+00317         CleanupStack::PushL(connectOperation);
+00318         
+00319         // Wait on the status
+00320         waiter->StartAndWait();
+00321         User::LeaveIfError(waiter->Result());
+00322         
+00323         // Populate the message
+00324         iConsole->Printf(KDownload);
+00325         CMsvOperation* operationPop = iMtm->InvokeAsyncFunctionL(KPOP3MTMPopulate,*selection,param, waiter->iStatus);
+00326         CleanupStack::PushL(operationPop);
+00327         
+00328         // Wait on the status
+00329         waiter->StartAndWait();
+00330         User::LeaveIfError(waiter->Result());
+00331         
+00332         // Now disconnect from Pop3 service
+00333         CMsvOperation* operationDis = iMtm->InvokeAsyncFunctionL(KPOP3MTMDisconnect,*selection,param, waiter->iStatus);
+00334         CleanupStack::PushL(operationDis);
+00335         
+00336         // Wait on the status
+00337         waiter->StartAndWait();
+00338         User::LeaveIfError(waiter->Result());
+00339         iConsole->Printf(KDisconnect);
+00340         iConsole->Getch ();
+00341         
+00342         CleanupStack::PopAndDestroy(5,selection); //operationDis,operationPop,connectOperation,waiter,selection
+00343         }
+00344         
+00354 void CPop3Example::HandleSessionEventL(TMsvSessionEvent /*aEvent*/, TAny* /*aArg1*/, TAny* /*aArg2*/, TAny* /*aArg3*/)
+00355         {}
+00356 
+00357 LOCAL_C void MainL()
+00358         {
+00359 
+00360         // Create an Active Scheduler to handle asychronous calls
+00361         CActiveScheduler* scheduler = new (ELeave) CActiveScheduler;
+00362         /*
+00363         If Active Scheduler has been created, install it .
+00364         As it is an asychronous call we need to install it explicitly.
+00365         */
+00366         CActiveScheduler::Install( scheduler );
+00367 
+00368         CPop3Example* app = CPop3Example::NewL();
+00369         CleanupStack::PushL(app);
+00370         
+00371         // Create a client registry.
+00372         app->CreateClientRegistryL();
+00373         
+00374         // Create pop and smtp accounts.
+00375         app->CreatePopAndSmtpAccountL();
+00376 
+00377         // Load the physical and logical device drivers.
+00378         // Symbian platform will automatically append .PDD and .LDD
+00379         
+00380         TInt err;
+00381         err=User::LoadPhysicalDevice(PDD_NAME);
+00382         if (err!=KErrNone && err!=KErrAlreadyExists)
+00383                 User::Leave(err);
+00384         err=User::LoadLogicalDevice(LDD_NAME);
+00385         if (err!=KErrNone && err!=KErrAlreadyExists)
+00386                 User::Leave(err);
+00387                 
+00388         // Create an Smtp mail to be sent.      
+00389         app->CreateSmtpMessageL();
+00390          
+00391         // Send the mail created.
+00392         app->SendSMTPMessageL();
+00393         
+00394         // Connect to the pop3 server for downloading the mail and disconnect.
+00395         app->ConnectDownloadAndDisconnectPOPServerL();
+00396         
+00397         CleanupStack::PopAndDestroy(app);
+00398 
+00399         delete scheduler;
+00400 
+00401         }
+00402 
+00403 GLDEF_C TInt E32Main()
+00404         {
+00405     __UHEAP_MARK;
+00406     CTrapCleanup* cleanup = CTrapCleanup::New();
+00407     if(cleanup == NULL)
+00408         {
+00409         return KErrNoMemory;
+00410         }
+00411     TRAPD(err, MainL());
+00412         
+00413         if(err != KErrNone)
+00414                 {
+00415                 _LIT(KUserPanic,"Failed to complete");  
+00416                 User::Panic(KUserPanic, err);
+00417                 }
+00418 
+00419     delete cleanup;
+00420     __UHEAP_MARKEND;
+00421     return KErrNone;
+00422         }
+

Generated on Thu Jan 21 10:32:58 2010 for TB10.1 Example Applications by  + +doxygen 1.5.3
+ +