public virtual EhloSmtpResponse Ehlo(String localHostName) 
		{
			EhloSmtpResponse ehloResponse=new EhloSmtpResponse();
			ehloResponse.AddAvailableAuthType("login");			
			ehloResponse.Message="OK";
			ehloResponse.ResponseCode=250;
			return ehloResponse;
		}
Example #2
0
        /// <summary>
        /// Parse the response from the EHLO into an
        /// EhloSmtpResponse.  Returns 250 "OK" if successful.
        /// </summary>
        /// <returns></returns>
        private EhloSmtpResponse ReadEhloSmtpResponse()
        {
            //log.Debug("READ THE RESPONSE");
            EhloSmtpResponse ehloResponse      = new EhloSmtpResponse();
            String           multiLineResponse = ReadResponse();
            StringReader     sr = new StringReader(multiLineResponse);

            String line = null;

            //log.Debug("READING...");
            while ((line = sr.ReadLine()) != null)
            {
                try
                {
                    String responseMessage = String.Empty;
                    if (line.Length > 4)
                    {
                        responseMessage = line.Substring(4).Trim();
                    }

                    //log.Debug("Reading "+line);
                    int responseCode = Convert.ToInt32(line.Substring(0, 3));
                    if (responseCode == 250)
                    {
                        if (responseMessage.ToLower().IndexOf("auth") == 0)
                        {
                            // parse the auth types from the response
                            if (responseMessage.Length > 4)
                            {
                                // RFC 2554 SMTP Authentication:
                                // (3) The AUTH EHLO keyword contains as a parameter a space separated
                                // list of the names of supported SASL mechanisms.
                                foreach (String authtype in responseMessage.Substring(5).Split(' '))
                                {
                                    ehloResponse.AddAvailableAuthType(authtype.ToLower());
                                }
                            }
                            // return new SmtpResponse(responseCode, responseMessage);
                        }
                    }
                    else
                    {
                        ehloResponse.ResponseCode = responseCode;
                        ehloResponse.Message      = responseMessage;
                        return(ehloResponse);
                    }
                }
                catch
                {
                    throw new MailException("Could not understand response from server: " + multiLineResponse);
                }
            }
            ehloResponse.ResponseCode = 250;
            ehloResponse.Message      = "OK";
            return(ehloResponse);
        }
		public void TestSimpleSmtpNegotiationWithAuth()
		{


			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(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(smtpProxy);
			
			try 
			{
				emailMessage.Send(smtpserver);
			}
			catch (SmtpException ex)
			{
				log.Debug("Exception was "+ex.Message);
				log.Debug(ex.StackTrace);
				throw ex;
			}

		}
Example #4
0
        /// <summary>
        /// Send the email.
        /// </summary>
        /// <param name="emailMessage">The completed message</param>
        /// <param name="rcpttocollection">A list of email addresses which
        /// are to be used in the RCPT TO SMTP communication</param>
        /// <param name="mailfrom">An email address for the MAIL FROM
        /// part of the SMTP protocol.</param>
        /// <exception cref="SmtpException">throws an SmtpException if there
        /// is an unexpected SMTP error number sent from the server.</exception>
        /// <exception cref="MailException">throws a MailException if there
        /// is a network problem, connection problem, or other issue.</exception>
        /// <returns></returns>
        internal bool Send(ISendableMessage emailMessage, EmailAddressCollection rcpttocollection, EmailAddress mailfrom)
        {
            //ISmtpNegotiator negotiator=_smtpserver.GetSmtpNegotiator();
            ISmtpProxy smtpproxy = GetSmtpProxy();
            //smtpproxy.CaptureSmtpConversation=CaptureSmtpConversation;
            SmtpResponse smtpResponse = smtpproxy.Open();

            try
            {
                #region Connect
                if (smtpResponse.ResponseCode != 220)
                {
                    throw smtpResponse.GetException();
                }
                #endregion

                #region HELO / EHLO
                if (UseEhlo())
                {
                    EhloSmtpResponse esmtpResponse = smtpproxy.Ehlo(GetHeloHost());
                    if (esmtpResponse.ResponseCode != 250)
                    {
                        // TODO: FIX THIS
                        throw new SmtpException(esmtpResponse.ResponseCode, esmtpResponse.Message);
                    }

                    // do SMTP AUTH
                    if (this._authToken != null)
                    {
                        smtpResponse = _authToken.Negotiate(smtpproxy, esmtpResponse.GetAvailableAuthTypes());
                        if (smtpResponse.ResponseCode != 235)
                        {
                            throw smtpResponse.GetException();
                        }
                    }
                }
                else
                {
                    smtpResponse = smtpproxy.Helo(GetHeloHost());
                    if (smtpResponse.ResponseCode != 250)
                    {
                        throw smtpResponse.GetException();
                    }
                }
                #endregion

                #region MAIL FROM
                smtpResponse = smtpproxy.MailFrom(mailfrom);
                if (smtpResponse.ResponseCode != 250)
                {
                    throw smtpResponse.GetException();
                }
                #endregion

                #region RCPT TO

                foreach (EmailAddress rcpttoaddress in rcpttocollection)
                {
                    smtpResponse = smtpproxy.RcptTo(rcpttoaddress);
                    if (smtpResponse.ResponseCode != 250)
                    {
                        throw smtpResponse.GetException();
                    }
                }
                #endregion

                #region DATA

                smtpResponse = smtpproxy.Data();
                if (smtpResponse.ResponseCode != 354)
                {
                    throw smtpResponse.GetException();
                }
                //smtpResponse=negotiator.WriteData();

                String message = emailMessage.ToDataString();
                if (message == null)
                {
                    throw new MailException("The message content is null");
                }

                /*
                 * // START Test With Domain Keys
                 * // (this would appear as an event callback if it works)
                 * MailSigner mailsigner=new MailSigner();
                 * bool signed = mailsigner.signMail(message);
                 * if (!signed)
                 * {
                 *      throw new MailException("Error creating DomainKeys signature.");
                 * }
                 * message=mailsigner.signedHeader + message;
                 * // END Test With Domain Keys
                 */


                // Send the data
                smtpResponse = smtpproxy.WriteData(message);
                if (smtpResponse.ResponseCode != 250)
                {
                    throw smtpResponse.GetException();
                }
                #endregion

                #region QUIT
                // QUIT
                smtpResponse = smtpproxy.Quit();
                if (smtpResponse.ResponseCode != 221)
                {
                    throw smtpResponse.GetException();
                }
                #endregion
            }
            finally
            {
                smtpproxy.Close();
                OnLogSmtpCompleted(this, "Connection Closed");
            }
            return(true);
        }
		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);
			}



		}
Example #6
0
        /// <summary>
        /// Parse the response from the EHLO into an 
        /// EhloSmtpResponse.  Returns 250 "OK" if successful.
        /// </summary>
        /// <returns></returns>
        private EhloSmtpResponse ReadEhloSmtpResponse()
        {
            //log.Debug("READ THE RESPONSE");
            EhloSmtpResponse ehloResponse=new EhloSmtpResponse();
            String multiLineResponse=ReadResponse();
            StringReader sr=new StringReader(multiLineResponse);

            String line=null;
            //log.Debug("READING...");
            while ((line=sr.ReadLine()) !=null)
            {
                try
                {
                    String responseMessage=String.Empty;
                    if (line.Length > 4)
                    {
                        responseMessage=line.Substring(4).Trim();
                    }

                    //log.Debug("Reading "+line);
                    int responseCode=Convert.ToInt32(line.Substring(0, 3));
                    if (responseCode==250)
                    {
                        if (responseMessage.ToLower().IndexOf("auth")==0)
                        {
                            // parse the auth types from the response
                            if (responseMessage.Length > 4)
                            {
                                // RFC 2554 SMTP Authentication:
                                // (3) The AUTH EHLO keyword contains as a parameter a space separated
                                // list of the names of supported SASL mechanisms.
                                foreach (String authtype in responseMessage.Substring(5).Split(' '))
                                {
                                    ehloResponse.AddAvailableAuthType(authtype.ToLower());
                                }
                            }
                            // return new SmtpResponse(responseCode, responseMessage);
                        }
                    }
                    else
                    {
                        ehloResponse.ResponseCode=responseCode;
                        ehloResponse.Message=responseMessage;
                        return ehloResponse;
                    }

                }
                catch
                {
                    throw new MailException("Could not understand response from server: "+multiLineResponse);
                }
            }
            ehloResponse.ResponseCode=250;
            ehloResponse.Message="OK";
            return ehloResponse;
        }