00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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
00099 iSession = CMsvSession::OpenSyncL(*this);
00100 CleanupStack::PushL(iSession);
00101
00102
00103 iClientRegistry = CClientMtmRegistry::NewL(*iSession,KMsvDefaultTimeoutMicroSeconds32);
00104 if (iClientRegistry != NULL && iClientRegistry->IsPresent(KUidMsgTypePOP3))
00105 {
00106 CleanupStack::PushL(iClientRegistry);
00107 }
00108
00109 iMtm = (CPop3ClientMtm*)iClientRegistry->NewMtmL(KUidMsgTypePOP3);
00110 iConsole->Printf(KCreateMtm);
00111 iConsole->Printf(KKeyPress);
00112 iConsole->Getch();
00113 CleanupStack::Pop(2,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
00131 settings->SetServerAddressL(KPopServer);
00132 settings->SetLoginNameL(KPopLoginName);
00133 settings->SetPasswordL(KPopPassword);
00134 settings->SetPort(110);
00135
00136
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
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
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
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);
00184 }
00185
00190 void CPop3Example::CreateSmtpMessageL()
00191 {
00192
00193 TMsvId outboxId = KMsvGlobalOutBoxIndexEntryId;
00194
00195
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
00228 CParaFormatLayer* paraFormatLayer = CParaFormatLayer::NewL();
00229 CleanupStack::PushL(paraFormatLayer);
00230
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
00238 bodyText->InsertL(0, KBodyContents);
00239 store->StoreBodyTextL(*bodyText);
00240 emailEntry->StoreL(*store);
00241
00242 store->CommitL();
00243
00244 CleanupStack::PopAndDestroy(8,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
00262 TMsvSelectionOrdering order;
00263 order.SetShowInvisibleEntries(ETrue);
00264 entry->SetSortTypeL(order);
00265
00266 entry->SetEntryL(outboxId);
00267
00268 CMsvEntrySelection* selection = entry->ChildrenL();
00269
00270
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
00281
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);
00289 }
00290
00297 void CPop3Example::ConnectDownloadAndDisconnectPOPServerL()
00298 {
00299
00300
00301 iMtm->SwitchCurrentEntryL(iPopAccount.iPopService);
00302
00303 CMsvEntrySelection* selection = new (ELeave) CMsvEntrySelection;
00304 CleanupStack::PushL(selection);
00305
00306
00307 selection->AppendL(iPopAccount.iPopService);
00308
00309 TBuf8<1> param;
00310
00311 CMessAsyncWaiter* waiter = CMessAsyncWaiter::NewL();
00312 CleanupStack::PushL(waiter);
00313
00314
00315 iConsole->Printf(KWait);
00316 CMsvOperation* connectOperation = iMtm->InvokeAsyncFunctionL(KPOP3MTMConnect,*selection,param, waiter->iStatus);
00317 CleanupStack::PushL(connectOperation);
00318
00319
00320 waiter->StartAndWait();
00321 User::LeaveIfError(waiter->Result());
00322
00323
00324 iConsole->Printf(KDownload);
00325 CMsvOperation* operationPop = iMtm->InvokeAsyncFunctionL(KPOP3MTMPopulate,*selection,param, waiter->iStatus);
00326 CleanupStack::PushL(operationPop);
00327
00328
00329 waiter->StartAndWait();
00330 User::LeaveIfError(waiter->Result());
00331
00332
00333 CMsvOperation* operationDis = iMtm->InvokeAsyncFunctionL(KPOP3MTMDisconnect,*selection,param, waiter->iStatus);
00334 CleanupStack::PushL(operationDis);
00335
00336
00337 waiter->StartAndWait();
00338 User::LeaveIfError(waiter->Result());
00339 iConsole->Printf(KDisconnect);
00340 iConsole->Getch ();
00341
00342 CleanupStack::PopAndDestroy(5,selection);
00343 }
00344
00354 void CPop3Example::HandleSessionEventL(TMsvSessionEvent , TAny* , TAny* , TAny* )
00355 {}
00356
00357 LOCAL_C void MainL()
00358 {
00359
00360
00361 CActiveScheduler* scheduler = new (ELeave) CActiveScheduler;
00362
00363
00364
00365
00366 CActiveScheduler::Install( scheduler );
00367
00368 CPop3Example* app = CPop3Example::NewL();
00369 CleanupStack::PushL(app);
00370
00371
00372 app->CreateClientRegistryL();
00373
00374
00375 app->CreatePopAndSmtpAccountL();
00376
00377
00378
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
00389 app->CreateSmtpMessageL();
00390
00391
00392 app->SendSMTPMessageL();
00393
00394
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 }