/// <summary> /// Is called when incoming SMTP message reading has completed. /// </summary> /// <param name="op">Asynchronous operation.</param> private void MessageReadingCompleted(SmartStream.ReadPeriodTerminatedAsyncOP op) { try{ if(op.Error != null){ if(op.Error is LineSizeExceededException){ SendFinalResponse(new SMTP_t_ReplyLine(503,"500 Line too long.",true)); } else if(op.Error is DataSizeExceededException){ SendFinalResponse(new SMTP_t_ReplyLine(503,"552 Too much mail data.",true)); } else{ m_pException = op.Error; } m_pSession.OnMessageStoringCanceled(); } else{ // Log. m_pSession.LogAddRead(op.BytesStored,"Readed " + op.BytesStored + " message bytes."); SMTP_Reply reply = new SMTP_Reply(250,"DATA completed in " + (DateTime.Now - m_StartTime).TotalSeconds.ToString("f2") + " seconds."); reply = m_pSession.OnMessageStoringCompleted(reply); SendFinalResponse(SMTP_t_ReplyLine.Parse(reply.ReplyCode + " " + reply.ReplyLines[0])); } } catch(Exception x){ m_pException = x; } // We got some unknown error, we are done. if(m_pException != null){ SetState(AsyncOP_State.Completed); } op.Dispose(); }
/// <summary> /// Is called when DATA command message sending has completed. /// </summary> /// <param name="op">Asynchronous operation.</param> /// <exception cref="ArgumentNullException">Is raised when <b>op</b> is null reference.</exception> private void DataMsgSendingCompleted(SmartStream.WritePeriodTerminatedAsyncOP op) { if(op == null){ throw new ArgumentNullException("op"); } try{ if(op.Error != null){ m_pException = op.Error; m_pSmtpClient.LogAddException("Exception: " + m_pException.Message,m_pException); SetState(AsyncOP_State.Completed); } else{ // Log m_pSmtpClient.LogAddWrite(op.BytesWritten,"Sent message " + op.BytesWritten + " bytes."); // Read DATA command final response. ReadResponseAsyncOP readResponseOP = new ReadResponseAsyncOP(); readResponseOP.CompletedAsync += delegate(object s,EventArgs<ReadResponseAsyncOP> e){ DataReadFinalResponseCompleted(readResponseOP); }; if(!m_pSmtpClient.ReadResponseAsync(readResponseOP)){ DataReadFinalResponseCompleted(readResponseOP); } } } catch(Exception x){ m_pException = x; m_pSmtpClient.LogAddException("Exception: " + m_pException.Message,m_pException); SetState(AsyncOP_State.Completed); } op.Dispose(); }
/// <summary> /// Is called when POP3 server RETR response reading has completed. /// </summary> /// <param name="op">Asynchronous operation.</param> private void RetrReadResponseCompleted(SmartStream.ReadLineAsyncOP op) { try{ // Operation failed. if(op.Error != null){ m_pException = op.Error; m_pPop3Client.LogAddException("Exception: " + op.Error.Message,op.Error); SetState(AsyncOP_State.Completed); } // Operation succeeded. else{ // Log m_pPop3Client.LogAddRead(op.BytesInBuffer,op.LineUtf8); // Server returned success response. if(string.Equals(op.LineUtf8.Split(new char[]{' '},2)[0],"+OK",StringComparison.InvariantCultureIgnoreCase)){ SmartStream.ReadPeriodTerminatedAsyncOP readMsgOP = new SmartStream.ReadPeriodTerminatedAsyncOP(m_pStream,long.MaxValue,SizeExceededAction.ThrowException); readMsgOP.Completed += delegate(object sender,EventArgs<SmartStream.ReadPeriodTerminatedAsyncOP> e){ MessageReadingCompleted(readMsgOP); }; if(m_pPop3Client.TcpStream.ReadPeriodTerminated(readMsgOP,true)){ MessageReadingCompleted(readMsgOP); } } // Server returned error response. else{ m_pException = new POP3_ClientException(op.LineUtf8); SetState(AsyncOP_State.Completed); } } } catch(Exception x){ m_pException = x; m_pPop3Client.LogAddException("Exception: " + x.Message,x); SetState(AsyncOP_State.Completed); } op.Dispose(); }
/// <summary> /// Is called when POP3 server DELE response reading has completed. /// </summary> /// <param name="op">Asynchronous operation.</param> private void DeleReadResponseCompleted(SmartStream.ReadLineAsyncOP op) { try{ // Operation failed. if(op.Error != null){ m_pException = op.Error; m_pPop3Client.LogAddException("Exception: " + op.Error.Message,op.Error); SetState(AsyncOP_State.Completed); } // Operation succeeded. else{ // Log m_pPop3Client.LogAddRead(op.BytesInBuffer,op.LineUtf8); // Server returned success response. if(string.Equals(op.LineUtf8.Split(new char[]{' '},2)[0],"+OK",StringComparison.InvariantCultureIgnoreCase)){ m_pOwner.m_IsMarkedForDeletion = true; SetState(AsyncOP_State.Completed); } // Server returned error response. else{ m_pException = new POP3_ClientException(op.LineUtf8); SetState(AsyncOP_State.Completed); } } } catch(Exception x){ m_pException = x; m_pPop3Client.LogAddException("Exception: " + x.Message,x); SetState(AsyncOP_State.Completed); } op.Dispose(); }
/// <summary> /// Is called when POP3 server response reading has completed. /// </summary> /// <param name="op">Asynchronous operation.</param> private void AuthReadResponseCompleted(SmartStream.ReadLineAsyncOP op) { try{ // Operation failed. if(op.Error != null){ m_pException = op.Error; m_pPop3Client.LogAddException("Exception: " + op.Error.Message,op.Error); SetState(AsyncOP_State.Completed); } // Operation succeeded. else{ // Log m_pPop3Client.LogAddRead(op.BytesInBuffer,op.LineUtf8); // Authentication succeeded. if(string.Equals(op.LineUtf8.Split(new char[]{' '},2)[0],"+OK",StringComparison.InvariantCultureIgnoreCase)){ // Start filling messages info. POP3_Client.FillMessagesAsyncOP fillOP = new FillMessagesAsyncOP(); fillOP.CompletedAsync += delegate(object sender,EventArgs<FillMessagesAsyncOP> e){ FillMessagesCompleted(fillOP); }; if(!m_pPop3Client.FillMessagesAsync(fillOP)){ FillMessagesCompleted(fillOP); } } // Continue authenticating. else if(op.LineUtf8.StartsWith("+")){ // + base64Data, we need to decode it. byte[] serverResponse = Convert.FromBase64String(op.LineUtf8.Split(new char[]{' '},2)[1]); byte[] clientResponse = m_pSASL.Continue(serverResponse); // We need just send SASL returned auth-response as base64. byte[] buffer = Encoding.UTF8.GetBytes(Convert.ToBase64String(clientResponse) + "\r\n"); // Log m_pPop3Client.LogAddWrite(buffer.Length,Convert.ToBase64String(clientResponse)); // Start auth-data sending. m_pPop3Client.TcpStream.BeginWrite(buffer,0,buffer.Length,this.AuthCommandSendingCompleted,null); } // Authentication rejected. else{ m_pException = new POP3_ClientException(op.LineUtf8); SetState(AsyncOP_State.Completed); } } } catch(Exception x){ m_pException = x; m_pPop3Client.LogAddException("Exception: " + x.Message,x); SetState(AsyncOP_State.Completed); } op.Dispose(); }
/// <summary> /// Is called when POP3 server STLS response reading has completed. /// </summary> /// <param name="op">Asynchronous operation.</param> private void StlsReadResponseCompleted(SmartStream.ReadLineAsyncOP op) { try{ // Operation failed. if(op.Error != null){ m_pException = op.Error; m_pPop3Client.LogAddException("Exception: " + op.Error.Message,op.Error); SetState(AsyncOP_State.Completed); } // Operation succeeded. else{ // Log m_pPop3Client.LogAddRead(op.BytesInBuffer,op.LineUtf8); // Server returned success response. if(string.Equals(op.LineUtf8.Split(new char[]{' '},2)[0],"+OK",StringComparison.InvariantCultureIgnoreCase)){ // Log m_pPop3Client.LogAddText("Starting TLS handshake."); SwitchToSecureAsyncOP switchSecureOP = new SwitchToSecureAsyncOP(m_pCertCallback); switchSecureOP.CompletedAsync += delegate(object s,EventArgs<SwitchToSecureAsyncOP> e){ SwitchToSecureCompleted(switchSecureOP); }; if(!m_pPop3Client.SwitchToSecureAsync(switchSecureOP)){ SwitchToSecureCompleted(switchSecureOP); } } // Server returned error response. else{ m_pException = new POP3_ClientException(op.LineUtf8); SetState(AsyncOP_State.Completed); } } } catch(Exception x){ m_pException = x; m_pPop3Client.LogAddException("Exception: " + x.Message,x); SetState(AsyncOP_State.Completed); } op.Dispose(); }
/// <summary> /// Is called when POP3 server CAPA response reading has completed. /// </summary> /// <param name="op">Asynchronous operation.</param> private void CapaReadResponseCompleted(SmartStream.ReadLineAsyncOP op) { try{ // Operation failed. if(op.Error != null){ m_pException = op.Error; m_pPop3Client.LogAddException("Exception: " + op.Error.Message,op.Error); SetState(AsyncOP_State.Completed); } // Operation succeeded. else{ // Log m_pPop3Client.LogAddRead(op.BytesInBuffer,op.LineUtf8); // Server returned success response. if(string.Equals(op.LineUtf8.Split(new char[]{' '},2)[0],"+OK",StringComparison.InvariantCultureIgnoreCase)){ // Read capa-list. SmartStream.ReadLineAsyncOP readLineOP = new SmartStream.ReadLineAsyncOP(new byte[8000],SizeExceededAction.JunkAndThrowException); readLineOP.Completed += delegate(object s,EventArgs<SmartStream.ReadLineAsyncOP> e){ try{ ReadMultiLineResponseLineCompleted(readLineOP); // Read response lines while we get terminator(.). while(this.State == AsyncOP_State.Active && m_pPop3Client.TcpStream.ReadLine(readLineOP,true)){ ReadMultiLineResponseLineCompleted(readLineOP); } } catch(Exception x){ m_pException = x; m_pPop3Client.LogAddException("Exception: " + x.Message,x); SetState(AsyncOP_State.Completed); } }; // Read response lines while we get terminator(.). while(this.State == AsyncOP_State.Active && m_pPop3Client.TcpStream.ReadLine(readLineOP,true)){ ReadMultiLineResponseLineCompleted(readLineOP); } } // Server returned error response. else{ m_pException = new POP3_ClientException(op.LineUtf8); SetState(AsyncOP_State.Completed); } } } catch(Exception x){ m_pException = x; m_pPop3Client.LogAddException("Exception: " + x.Message,x); SetState(AsyncOP_State.Completed); } op.Dispose(); }
/// <summary> /// Is called when POP3 server PASS response reading has completed. /// </summary> /// <param name="op">Asynchronous operation.</param> private void PassReadResponseCompleted(SmartStream.ReadLineAsyncOP op) { try{ // Operation failed. if(op.Error != null){ m_pException = op.Error; m_pPop3Client.LogAddException("Exception: " + op.Error.Message,op.Error); SetState(AsyncOP_State.Completed); } // Operation succeeded. else{ // Log m_pPop3Client.LogAddRead(op.BytesInBuffer,op.LineUtf8); // Server returned success response. if(string.Equals(op.LineUtf8.Split(new char[]{' '},2)[0],"+OK",StringComparison.InvariantCultureIgnoreCase)){ // Start filling messages info. POP3_Client.FillMessagesAsyncOP fillOP = new FillMessagesAsyncOP(); fillOP.CompletedAsync += delegate(object sender,EventArgs<FillMessagesAsyncOP> e){ FillMessagesCompleted(fillOP); }; if(!m_pPop3Client.FillMessagesAsync(fillOP)){ FillMessagesCompleted(fillOP); } } // Server returned error response. else{ m_pException = new POP3_ClientException(op.LineUtf8); SetState(AsyncOP_State.Completed); } } } catch(Exception x){ m_pException = x; m_pPop3Client.LogAddException("Exception: " + x.Message,x); SetState(AsyncOP_State.Completed); } op.Dispose(); }
/// <summary> /// Is called when POP3 server USER response reading has completed. /// </summary> /// <param name="op">Asynchronous operation.</param> private void UserReadResponseCompleted(SmartStream.ReadLineAsyncOP op) { try{ // Operation failed. if(op.Error != null){ m_pException = op.Error; m_pPop3Client.LogAddException("Exception: " + op.Error.Message,op.Error); SetState(AsyncOP_State.Completed); } // Operation succeeded. else{ // Log m_pPop3Client.LogAddRead(op.BytesInBuffer,op.LineUtf8); // Server returned success response. if(string.Equals(op.LineUtf8.Split(new char[]{' '},2)[0],"+OK",StringComparison.InvariantCultureIgnoreCase)){ byte[] buffer = Encoding.UTF8.GetBytes("PASS " + m_Password + "\r\n"); // Log m_pPop3Client.LogAddWrite(buffer.Length,"PASS <***REMOVED***>"); // Start command sending. m_pPop3Client.TcpStream.BeginWrite(buffer,0,buffer.Length,this.PassCommandSendingCompleted,null); } // Server returned error response. else{ m_pException = new POP3_ClientException(op.LineUtf8); SetState(AsyncOP_State.Completed); } } } catch(Exception x){ m_pException = x; m_pPop3Client.LogAddException("Exception: " + x.Message,x); SetState(AsyncOP_State.Completed); } op.Dispose(); }