Ejemplo n.º 1
0
        /*
         * /// <summary>
         * /// Return true if this is among the supported AUTH
         * /// types.
         * /// </summary>
         * /// <param name="authtypes"></param>
         * /// <returns></returns>
         * public bool IsSupported(SmtpAuthType[] authtypes)
         * {
         *      log.Debug("CHECKING SUPPORTED TYPES");
         *      for (int i=0; i<authtypes.Length; i++)
         *      {
         *              log.Debug("CHECKING IF "+authtypes[i]+"="+SmtpAuthType.Login);
         *              if (authtypes[i]==SmtpAuthType.Login)
         *              {
         *                      return true;
         *              }
         *      }
         *      return false;
         * }
         */

        /// <summary>
        /// Return the 235 response code if valid, otherwise
        /// return the error.
        /// </summary>
        /// <param name="smtpProxy"></param>
        /// <param name="supportedAuthTypes">the supported auth types</param>
        /// <returns></returns>
        public SmtpResponse Negotiate(ISmtpProxy smtpProxy, String[] supportedAuthTypes)
        {
            SmtpResponse response = smtpProxy.Auth("login");

            //log.Debug("RESPONSE WAS "+response.ResponseCode+" "+response.Message);
            if (response.ResponseCode != 334)
            {
                return(response);
            }
            Base64Encoder encoder = Base64Encoder.GetInstance();

            response = smtpProxy.SendString(encoder.EncodeString(this.UserName, this._charEncoding));
            if (response.ResponseCode != 334)
            {
                return(response);
            }
            response = smtpProxy.SendString(encoder.EncodeString(this.Password, this._charEncoding));
            if (response.ResponseCode != 334)
            {
                // here it's an error
                return(response);
            }
            else
            {
                // here it's ok.
                return(response);
            }
        }
Ejemplo n.º 2
0
        public void TestFailedSmtpNegotiationWithAuth()
        {
            SmtpServer smtpserver = new SmtpServer("localhost");

            smtpserver.SmtpAuthToken = new SmtpAuthToken("test", "test");


            Base64Encoder encoder        = Base64Encoder.GetInstance();
            String        base64Username = encoder.EncodeString(smtpserver.SmtpAuthToken.UserName, System.Text.Encoding.ASCII);
            String        base64Password = encoder.EncodeString(smtpserver.SmtpAuthToken.Password, System.Text.Encoding.ASCII);

            EmailMessage emailMessage = GetTestHtmlAndTextMessage();

            IMock mockSmtpProxy = new DynamicMock(typeof(ISmtpProxy));

            mockSmtpProxy.ExpectAndReturn("Open", new SmtpResponse(220, "welcome to the mock object server"), null);

            EhloSmtpResponse ehloResponse = new EhloSmtpResponse();

            ehloResponse.AddAvailableAuthType("login");

            ehloResponse.Message      = "OK";
            ehloResponse.ResponseCode = 250;
            mockSmtpProxy.ExpectAndReturn("Ehlo", ehloResponse, Dns.GetHostName());
            mockSmtpProxy.ExpectAndReturn("Auth", new SmtpResponse(554, "Unrecognized auth type"), "login");

            ISmtpProxy smtpProxy = (ISmtpProxy)mockSmtpProxy.MockInstance;

            smtpserver.OverrideSmtpProxy(smtpProxy);

            /*
             * mockSmtpProxy.ExpectAndReturn("SendString", new SmtpResponse(554, "Invalid UserName"), base64Username);
             * mockSmtpProxy.ExpectAndReturn("SendString", new SmtpResponse(554, "Invalid Password"), base64Password);
             * mockSmtpProxy.ExpectAndReturn("MailFrom", new SmtpResponse(250, "mail from"), emailMessage.FromAddress);
             * foreach (EmailAddress rcpttoaddr in emailMessage.ToAddresses)
             * {
             *      mockSmtpProxy.ExpectAndReturn("RcptTo", new SmtpResponse(250, "receipt to"), rcpttoaddr);
             * }
             * mockSmtpProxy.ExpectAndReturn("Data", new SmtpResponse(354, "data open"), null);
             * mockSmtpProxy.ExpectAndReturn("WriteData", new SmtpResponse(250, "data"), emailMessage.ToDataString());
             *
             * mockSmtpProxy.ExpectAndReturn("Quit", new SmtpResponse(221, "quit"), null);
             * mockSmtpProxy.Expect("Close", null);
             */

            try
            {
                emailMessage.Send(smtpserver);
                Assert.Fail("The auth type is wrong");
            }
            catch (SmtpException ex)
            {
                log.Debug("ERROR CODE IS " + 554);
                Assert.AreEqual(554, ex.ErrorCode);
            }
        }
Ejemplo n.º 3
0
        public void TestSmtpNegotiationWithAuthTimeout()
        {
            SmtpServer smtpserver = new SmtpServer("localhost");

            smtpserver.SmtpAuthToken = new LoginAuthToken("test", "test");


            Base64Encoder encoder        = Base64Encoder.GetInstance();
            String        base64Username = encoder.EncodeString(smtpserver.SmtpAuthToken.UserName, System.Text.Encoding.ASCII);
            String        base64Password = encoder.EncodeString(smtpserver.SmtpAuthToken.Password, System.Text.Encoding.ASCII);

            EmailMessage emailMessage = GetTestHtmlAndTextMessage();

            /*
             * IMock mockSmtpProxy = new DynamicMock(typeof(ISmtpProxy));
             *
             * mockSmtpProxy.ExpectAndReturn("Open", new SmtpResponse(220, "welcome to the mock object server"), null);
             *
             * EhloSmtpResponse ehloResponse=new EhloSmtpResponse();
             * ehloResponse.AddAvailableAuthType("login");
             *
             * ehloResponse.Message="OK";
             * ehloResponse.ResponseCode=250;
             * mockSmtpProxy.ExpectAndReturn("Ehlo",ehloResponse , Dns.GetHostName());
             *
             * mockSmtpProxy.ExpectAndReturn("Auth", new SmtpResponse(334, encoder.EncodeString("Username:"******"login");
             * mockSmtpProxy.ExpectAndReturn("SendString", new SmtpResponse(334, encoder.EncodeString("Password:"******"SendString", new SmtpResponse(235, "Hooray, Authenticated"), base64Password);
             * mockSmtpProxy.ExpectAndReturn("MailFrom", new SmtpResponse(250, "mail from"), emailMessage.FromAddress);
             *
             * foreach (EmailAddress rcpttoaddr in emailMessage.ToAddresses)
             * {
             *      mockSmtpProxy.ExpectAndReturn("RcptTo", new SmtpResponse(250, "receipt to"), rcpttoaddr);
             * }
             * mockSmtpProxy.ExpectAndReturn("Data", new SmtpResponse(354, "data open"), null);
             * mockSmtpProxy.ExpectAndReturn("WriteData", new SmtpResponse(250, "data"), emailMessage.ToDataString());
             *
             * mockSmtpProxy.ExpectAndReturn("Quit", new SmtpResponse(221, "quit"), null);
             * mockSmtpProxy.Expect("Close", null);
             *
             * ISmtpProxy smtpProxy= (ISmtpProxy) mockSmtpProxy.MockInstance;
             */

            smtpserver.OverrideSmtpProxy(new MockedSlowProxy());
            try
            {
                emailMessage.Send(smtpserver);
                Assert.Fail("This should have thrown an SmtpException indicating a timeout.");
            }
            catch (SmtpException ex)
            {
                log.Debug("Exception was " + ex.Message);
                log.Debug(ex.StackTrace);
                //throw ex;
            }
        }
        public void LineLengthTest()
        {
            String        line           = "12345678901234567890123456789012345678901234567890123456789012345678901234567890";
            String        expectedresult = "MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3\r\nODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTA=";
            Base64Encoder base64         = Base64Encoder.GetInstance();
            StringReader  sr             = new StringReader(line);
            StringBuilder sb             = new StringBuilder();
            StringWriter  sw             = new StringWriter(sb);

            base64.Encode(sr, sw, System.Text.Encoding.GetEncoding("iso-8859-1"));
            log.Debug(sb.ToString());
            Assert.AreEqual(expectedresult, sb.ToString());

            Assert.AreEqual(expectedresult, base64.EncodeString(line, System.Text.Encoding.GetEncoding("iso-8859-1")));
        }
Ejemplo n.º 5
0
        public virtual SmtpResponse Auth(String localHostName)
        {
            Base64Encoder encoder = Base64Encoder.GetInstance();

            return(new SmtpResponse(334, encoder.EncodeString("Username:", System.Text.Encoding.ASCII)));
        }