email/email/smtp/src/T_UtilsSendEmail.cpp
branchSymbian2
changeset 1 8758140453c0
child 6 c108117318cb
equal deleted inserted replaced
0:e8c1ea2c6496 1:8758140453c0
       
     1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of the License "Symbian Foundation License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.symbianfoundation.org/legal/sfl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // This is the header file for CT_MsgUtilsSendEmail
       
    15 // 
       
    16 //
       
    17 
       
    18 
       
    19 
       
    20 /**
       
    21  @file
       
    22 */
       
    23 
       
    24 
       
    25 // User includes
       
    26 #include "T_UtilsSendEmail.h"
       
    27 
       
    28 
       
    29 // epoc includes
       
    30 #include <iapprefs.h>
       
    31 
       
    32 
       
    33 // Literals for the Smtp commands
       
    34 _LIT8(KHeloString, "HELO\r\n");
       
    35 _LIT8(KMailFrom, "MAIL FROM:");
       
    36 _LIT8(KRcptTo, "RCPT TO:");
       
    37 _LIT8(KDataString, "DATA\r\n");
       
    38 _LIT8(KEndData, "\r\n.\r\n");
       
    39 _LIT8(KQuitString, "QUIT\r\n");
       
    40 _LIT8(KCrlfString, "\r\n");
       
    41 
       
    42 
       
    43 /**
       
    44 NewL()
       
    45 Static factory constructor. Uses two phase 
       
    46 construction.
       
    47 
       
    48 @param aTestStep
       
    49 @leave KErrNoMemory
       
    50 @return
       
    51 A pointer to the newly created CT_MsgUtilsSendEmail object.
       
    52 */
       
    53 CT_MsgUtilsSendEmail* CT_MsgUtilsSendEmail::NewL(CTestStep& aTestStep)
       
    54    {
       
    55 	CT_MsgUtilsSendEmail* self = new (ELeave) CT_MsgUtilsSendEmail(aTestStep);
       
    56 	return self;
       
    57 	}
       
    58 
       
    59 
       
    60 /**
       
    61 CT_MsgUtilsSendEmail()
       
    62 Constructor
       
    63 
       
    64 @param aTestStep
       
    65 */
       
    66 CT_MsgUtilsSendEmail::CT_MsgUtilsSendEmail(CTestStep& aTestStep)
       
    67 :	CActive(EPriorityStandard),iTestStep(aTestStep)
       
    68 	{
       
    69 	CActiveScheduler::Add(this);
       
    70 	}
       
    71 
       
    72 
       
    73 /**
       
    74 ~CT_MsgUtilsSendEmail()
       
    75 Destructor
       
    76 */
       
    77 CT_MsgUtilsSendEmail::~CT_MsgUtilsSendEmail()
       
    78 	{
       
    79 	iFile.Close();
       
    80 	iFs.Close();
       
    81 	delete iImSocket;
       
    82 	iImSocket=NULL;
       
    83 	delete iIapPrefs;
       
    84 	iIapPrefs=NULL;
       
    85 	}
       
    86 
       
    87 
       
    88 /**
       
    89 StartL()
       
    90 Deletes the children entries of the specified parent
       
    91 
       
    92 @param aSender
       
    93 @param aReceipient
       
    94 @param aMailFile
       
    95 @param aServerIP
       
    96 @param aStatus
       
    97 */
       
    98 void CT_MsgUtilsSendEmail::StartL(TPtrC aSender,TPtrC aReceipient,TPtrC aMailFile,
       
    99 											TPtrC aServerIP,TRequestStatus& aStatus)
       
   100 	{
       
   101 	iRequestStatus = &aStatus;
       
   102 	aStatus = KRequestPending;
       
   103 	
       
   104 	iSender.Set(aSender);
       
   105 	iReceipient.Set(aReceipient);
       
   106 	
       
   107 	// Connects the client to the file server.
       
   108 	iFs.Connect();
       
   109 
       
   110 	// Opens an existing file reading and a share mode 
       
   111 	// If the file does not already exist, an error is returned.
       
   112 	iFile.Open(iFs, (TDesC&)aMailFile, EFileShareAny|EFileRead);
       
   113 
       
   114 	// Uses two phase construction and leaves nothing on the CleanupStack
       
   115 	iImSocket = CImTextServerSession::NewL();
       
   116 
       
   117 	iIapPrefs = CImIAPPreferences::NewLC();
       
   118 	CleanupStack::Pop();
       
   119 
       
   120 	// Queue a connect assuming the socket is successfully opened.
       
   121 	iImSocket->QueueConnectL(iStatus, (TDesC&)aServerIP, 25, *iIapPrefs, ETrue);
       
   122 	
       
   123 	iSendState = ESmtpConnecting;
       
   124 	SetActive();
       
   125 	}
       
   126 
       
   127 	
       
   128 /**
       
   129 DoCancel()
       
   130 CActive derived function to handle Cancel requests
       
   131 */
       
   132 void CT_MsgUtilsSendEmail::DoCancel()
       
   133 	{
       
   134 	iImSocket->Cancel();
       
   135 	}
       
   136 
       
   137 
       
   138 /**
       
   139 RunL()
       
   140 CActive derived function to handle the state machine
       
   141 
       
   142 @panic Unkown receive state!
       
   143 @panic Unkown send state!
       
   144 */
       
   145 void CT_MsgUtilsSendEmail::RunL()
       
   146 	{
       
   147 	// handle send state
       
   148 	switch (iSendState)
       
   149 		{
       
   150 		case ESmtpNotConnected:
       
   151 			{
       
   152 			// Send completed.
       
   153 			User::RequestComplete(iRequestStatus,KErrNone);
       
   154 			break;
       
   155 			}
       
   156 		case ESmtpConnecting:
       
   157 			{	
       
   158 			// Connection attempt completed.
       
   159 			if (iStatus != KErrNone)
       
   160 				{
       
   161 				// error
       
   162 				iTestStep.ERR_PRINTF2(_L("CT_MsgUtilsSendEmail: failed to connect: [%d]."), iStatus.Int());
       
   163 				iTestStep.SetTestStepResult(EFail);
       
   164 				User::Leave(iStatus.Int());
       
   165 				}
       
   166 			
       
   167 				// ok successful connect, next send HELO
       
   168 				iSendState = ESmtpSendHELO;
       
   169 				iImSocket->Send(iStatus, KHeloString());
       
   170 				SetActive();
       
   171 				break;
       
   172 			}
       
   173 		case ESmtpSendHELO:
       
   174 			{	
       
   175 			// HELO was sent
       
   176 			if (iStatus != KErrNone)
       
   177 				{
       
   178 				// error
       
   179 				iTestStep.ERR_PRINTF2(_L("CT_MsgUtilsSendEmail: failed to send HELO: [%d]."), iStatus.Int());
       
   180 				iTestStep.SetTestStepResult(EFail);
       
   181 				User::Leave(iStatus.Int());
       
   182 				}
       
   183 			
       
   184 				// ok we sent HELO, now receive reply
       
   185 				iSendState = ESmtpReceiveReply;
       
   186 				iRecvState = ESmtpHeloReply;
       
   187 				iImSocket->QueueReceiveNextTextLine(iStatus);
       
   188 				SetActive();
       
   189 				break;
       
   190 			}
       
   191 		case ESmtpSendMailFrom:
       
   192 			{	
       
   193 			// MAIL FROM was sent.
       
   194 			if (iStatus != KErrNone)
       
   195 				{
       
   196 				// error
       
   197 				iTestStep.ERR_PRINTF2(_L("CT_MsgUtilsSendEmail: failed to send MAIL FROM: [%d]."), iStatus.Int());
       
   198 				iTestStep.SetTestStepResult(EFail);
       
   199 				User::Leave(iStatus.Int());
       
   200 				}
       
   201 			
       
   202 				// ok we sent MAIL FROM, now receive reply
       
   203 				iSendState = ESmtpReceiveReply;
       
   204 				iRecvState = ESmtpFromReply;
       
   205 				iImSocket->QueueReceiveNextTextLine(iStatus);
       
   206 				SetActive();
       
   207 				break;
       
   208 			}
       
   209 		case ESmtpSendRcptTo:
       
   210 			{	
       
   211 			// RCPT TO was sent.
       
   212 			if (iStatus != KErrNone)
       
   213 				{
       
   214 				// error
       
   215 				iTestStep.ERR_PRINTF2(_L("CT_MsgUtilsSendEmail: failed to send RCPT TO: [%d]."), iStatus.Int());
       
   216 				iTestStep.SetTestStepResult(EFail);
       
   217 				User::Leave(iStatus.Int());
       
   218 				}
       
   219 			
       
   220 				// ok we sent RCPT TO, now receive reply
       
   221 				iSendState = ESmtpReceiveReply;
       
   222 				iRecvState = ESmtpRcptReply;
       
   223 				iImSocket->QueueReceiveNextTextLine(iStatus);
       
   224 				SetActive();
       
   225 				break;
       
   226 			}
       
   227 		case ESmtpSendData:
       
   228 			{	
       
   229 			// DATA was sent.
       
   230 			if (iStatus != KErrNone)
       
   231 				{
       
   232 				// error
       
   233 				iTestStep.ERR_PRINTF2(_L("CT_MsgUtilsSendEmail: failed to send DATA: [%d]."), iStatus.Int());
       
   234 				iTestStep.SetTestStepResult(EFail);
       
   235 				User::Leave(iStatus.Int());
       
   236 				}
       
   237 			
       
   238 				// ok we sent DATA, now receive reply
       
   239 				iSendState = ESmtpReceiveReply;
       
   240 				iRecvState = ESmtpDataReply;
       
   241 				iImSocket->QueueReceiveNextTextLine(iStatus);
       
   242 				SetActive();
       
   243 				break;
       
   244 			}
       
   245 		case ESmtpSendContent:
       
   246 			{	
       
   247 			// Content was sent.
       
   248 			// Read and send file. Send file in chunks of 100 bytes.
       
   249 			TBuf8<100> buffer;
       
   250 			User::LeaveIfError(iFile.Read(buffer, 100));
       
   251 			if (buffer.Length() == 0)
       
   252 				{
       
   253 				// ok, end of file. send end of email.
       
   254 				iSendState = ESmtpSendEndData;
       
   255 				iImSocket->Send(iStatus, KEndData());
       
   256 				}
       
   257 			else
       
   258 				{
       
   259 				// we still have data in file. read and send.
       
   260 				iSendState = ESmtpSendContent;
       
   261 				iImSocket->Send(iStatus, buffer);
       
   262 				}
       
   263 				SetActive();
       
   264 				break;
       
   265 			}
       
   266 		case ESmtpSendEndData:
       
   267 			{	
       
   268 			// end of data was sent.
       
   269 			if (iStatus != KErrNone)
       
   270 				{
       
   271 				// error
       
   272 				iTestStep.ERR_PRINTF2(_L("CT_MsgUtilsSendEmail: failed to send end-of-data marker: [%d]."), iStatus.Int());
       
   273 				iTestStep.SetTestStepResult(EFail);
       
   274 				User::Leave(iStatus.Int());
       
   275 				}
       
   276 			
       
   277 			// ok we sent end-of-data, now receive reply
       
   278 			iSendState = ESmtpReceiveReply;
       
   279 			iRecvState = ESmtpContentReply;
       
   280 			iImSocket->QueueReceiveNextTextLine(iStatus);
       
   281 			SetActive();
       
   282 			break;
       
   283 			}
       
   284 		case ESmtpSendLogout:
       
   285 			{
       
   286 			iSendState = ESmtpReceiveReply;
       
   287 			iRecvState = ESmtpLogoutReply;
       
   288 			iImSocket->QueueReceiveNextTextLine(iStatus);
       
   289 			SetActive();
       
   290 			break;
       
   291 			}
       
   292 		case ESmtpReceiveReply:
       
   293 			{
       
   294 			if (iStatus != KErrNone)
       
   295 				{
       
   296 				// error
       
   297 				iTestStep.ERR_PRINTF3(_L("CT_MsgUtilsSendEmail: failed to receive reply: error [%d] recvstate [%d]."), iStatus.Int(), iRecvState);
       
   298 				iTestStep.SetTestStepResult(EFail);
       
   299 				User::Leave(iStatus.Int());
       
   300 				}
       
   301 			
       
   302 			// ok, read and print out the reply
       
   303 			TBuf8<50> buffer;
       
   304 			TBuf<50> buffer16;
       
   305 			TImLineType replyLine = iImSocket->GetCurrentTextLine(buffer);
       
   306 			buffer16.Copy(buffer);
       
   307 			iTestStep.INFO_PRINTF1(buffer16);
       
   308 			
       
   309 			if (replyLine == EBufferTooSmall)
       
   310 				{
       
   311 				while (replyLine != ECRLFTerminated)
       
   312 					{
       
   313 					replyLine = iImSocket->GetCurrentTextLine(buffer);
       
   314 					buffer16.Copy(buffer);
       
   315 					iTestStep.INFO_PRINTF1(buffer16);
       
   316 					}
       
   317 				}
       
   318 		
       
   319 			// handle reply state
       
   320 			switch (iRecvState)
       
   321 				{
       
   322 				case ESmtpHeloReply:
       
   323 					{
       
   324 					// send MAIL FROM
       
   325 					iSendState = ESmtpSendMailFrom;
       
   326 					TBuf8<100> from;
       
   327 					from.Append(KMailFrom);
       
   328 					from.Append(iSender);
       
   329 					from.Append(KCrlfString);
       
   330 					iImSocket->Send(iStatus, from);
       
   331 					break;
       
   332 					}
       
   333 				case ESmtpFromReply:
       
   334 					{
       
   335 					// send RCPT TO
       
   336 					iSendState = ESmtpSendRcptTo;
       
   337 					TBuf8<100> to;
       
   338 					to.Append(KRcptTo);
       
   339 					to.Append(iReceipient);
       
   340 					to.Append(KCrlfString);
       
   341 					iImSocket->Send(iStatus, to);
       
   342 					break;
       
   343 					}
       
   344 				case ESmtpRcptReply:
       
   345 					{
       
   346 						// send DATA
       
   347 					iSendState = ESmtpSendData;
       
   348 					iImSocket->Send(iStatus, KDataString());
       
   349 					break;
       
   350 					}
       
   351 				case ESmtpDataReply:
       
   352 					{
       
   353 					// send content
       
   354 					iSendState = ESmtpSendContent;
       
   355 					TBuf8<100> buffer;
       
   356 					User::LeaveIfError(iFile.Read(buffer, 100));
       
   357 					if (buffer.Length() == 0)
       
   358 						{
       
   359 						// ok, end of file. since this is first read it would mean the file
       
   360 						// is empty! we should maybe leave here or??
       
   361 						iSendState = ESmtpSendEndData;
       
   362 						iImSocket->Send(iStatus, KEndData());
       
   363 						}
       
   364 					else
       
   365 						{
       
   366 						// we still have data in file
       
   367 						iSendState = ESmtpSendContent;
       
   368 						iImSocket->Send(iStatus, buffer);
       
   369 						}
       
   370 						break;
       
   371 					}
       
   372 				case ESmtpContentReply:
       
   373 					{
       
   374 					// send QUIT
       
   375 					iSendState = ESmtpSendLogout;
       
   376 					iImSocket->Send(iStatus, KQuitString());
       
   377 					break;
       
   378 					}
       
   379 				case ESmtpLogoutReply:
       
   380 					{
       
   381 					//iTestStep.INFO_PRINTF1(_L("LOGOUT"));
       
   382 					iSendState = ESmtpNotConnected;
       
   383 					iImSocket->Disconnect(iStatus);
       
   384 					break;
       
   385 					}
       
   386 				default:
       
   387 					{
       
   388 					User::Panic(_L("Unkown receive state!"), iRecvState);
       
   389 					break;
       
   390 					}
       
   391 				}// End switch (iRecvState)
       
   392 			
       
   393 				SetActive();
       
   394 				break;
       
   395 			}
       
   396 		default:
       
   397 			{
       
   398 			User::Panic(_L("Unkown send state!"), iSendState);
       
   399 			break;
       
   400 			}
       
   401 		} // End switch (iSendState)
       
   402 	}