Пример #1
0
        /// <summary>
        /// Is called when SMTP server greeting reading has completed.
        /// </summary>
        /// <param name="op">Asynchronous operation.</param>
        /// <param name="connectCallback">Callback to be called to complete connect operation.</param>
        private void ReadServerGreetingCompleted(ReadResponseAsyncOP op,CompleteConnectCallback connectCallback)
        {
            Exception error = null;

            try{
                // Greeting reading failed, we are done.
                if(op.Error != null){
                    error = op.Error;
                }
                // Greeting reading succeded.
                else{
                    /* RFC 5321 4.2.
                        Greeting = ( "220 " (Domain / address-literal) [ SP textstring ] CRLF ) /
                                   ( "220-" (Domain / address-literal) [ SP textstring ] CRLF
                                  *( "220-" [ textstring ] CRLF )
                                     "220" [ SP textstring ] CRLF )

                    */

                    // SMTP server accepted connection, get greeting text.
                    if(op.ReplyLines[0].ReplyCode == 220){
                        StringBuilder greetingText = new StringBuilder();
                        foreach(SMTP_t_ReplyLine line in op.ReplyLines){
                            greetingText.AppendLine(line.Text);
                        }

                        m_GreetingText = greetingText.ToString();
                        m_pEsmtpFeatures = new List<string>();
                        m_pRecipients = new List<string>();
                    }
                    // SMTP server rejected connection.
                    else{
                        error = new SMTP_ClientException(op.ReplyLines);
                    }
                }
            }
            catch(Exception x){
                error = x;
            }

            // Complete TCP_Client connect operation.
            connectCallback(error);
        }
Пример #2
0
            /// <summary>
            /// Is called when SMTP server response reading has completed.
            /// </summary>
            /// <param name="op">Asynchronous operation.</param>
            private void AuthReadResponseCompleted(ReadResponseAsyncOP op)
            {
                try{
                    // Continue authenticating.
                    if(op.ReplyLines[0].ReplyCode == 334){
                        // 334 base64Data, we need to decode it.
                        byte[] serverResponse = Convert.FromBase64String(op.ReplyLines[0].Text);

                        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_pSmtpClient.LogAddWrite(buffer.Length,Convert.ToBase64String(clientResponse));

                        // Start auth-data sending.
                        m_pSmtpClient.TcpStream.BeginWrite(buffer,0,buffer.Length,this.AuthCommandSendingCompleted,null);
                    }
                    // Authentication suceeded.
                    else if(op.ReplyLines[0].ReplyCode == 235){
                        m_pSmtpClient.m_pAuthdUserIdentity = new GenericIdentity(m_pSASL.UserName,m_pSASL.Name);

                        SetState(AsyncOP_State.Completed);
                    }
                    // Authentication rejected.
                    else{
                        m_pException = new SMTP_ClientException(op.ReplyLines);
                        SetState(AsyncOP_State.Completed);
                    }
                }
                catch(Exception x){
                    m_pException = x;
                    m_pSmtpClient.LogAddException("Exception: " + x.Message,x);
                    SetState(AsyncOP_State.Completed);
                }
            }