public void TestDeliverToServerNotSupportingEHLORequiredConnectionSecurity()
        {
            Assert.AreEqual(0, _status.UndeliveredMessages.Length);

            var deliveryResults = new Dictionary <string, int>()
            {
                { "*****@*****.**", 250 }
            };

            int smtpServerPort = TestSetup.GetNextFreePort();

            using (var server = new SmtpServerSimulator(1, smtpServerPort))
            {
                server.ServerSupportsEhlo = false;
                server.AddRecipientResult(deliveryResults);
                server.StartListen();

                // Add a route so we can conenct to localhost.
                TestSetup.AddRoutePointingAtLocalhost(1, smtpServerPort, false, eConnectionSecurity.eCSSTARTTLSRequired);

                // Send message to this route.

                SmtpClientSimulator.StaticSend("*****@*****.**", "*****@*****.**", "Test", "Test message");

                // Wait for the client to disconnect.
                server.WaitForCompletion();

                CustomAsserts.AssertRecipientsInDeliveryQueue(0, true);

                var msg = Pop3ClientSimulator.AssertGetFirstMessageText("*****@*****.**", "test");
                Assert.IsTrue(msg.Contains("Server does not support EHLO command."));
            }
        }
Exemple #2
0
        public void SmtpServerNOTSupportingStartTls_StartTlsRequired()
        {
            Account account = SingletonProvider <TestSetup> .Instance.AddAccount(_domain, "*****@*****.**", "test");

            // Set up a server listening on port 250 which accepts email for [email protected]
            var deliveryResults = new Dictionary <string, int>();

            deliveryResults["*****@*****.**"] = 250;

            int smtpServerPort = TestSetup.GetNextFreePort();

            using (var server = new SmtpServerSimulator(1, smtpServerPort, eConnectionSecurity.eCSNone))
            {
                server.SetCertificate(SslSetup.GetCertificate());
                server.AddRecipientResult(deliveryResults);
                server.StartListen();

                Route route = TestSetup.AddRoutePointingAtLocalhost(1, smtpServerPort, true, eConnectionSecurity.eCSSTARTTLSRequired);

                var smtpClient = new SmtpClientSimulator();
                smtpClient.Send(account.Address, "*****@*****.**", "Test", "Test message");

                CustomAsserts.AssertRecipientsInDeliveryQueue(0);

                // This should now be processed via the rule -> route -> external server we've set up.
                server.WaitForCompletion();

                var msg = Pop3ClientSimulator.AssertGetFirstMessageText("*****@*****.**", "test");

                Assert.IsTrue(msg.Contains("Server does not support STARTTLS"));
            }
        }
Exemple #3
0
        public void TestOnDeliveryFailedVBScript()
        {
            // First set up a script
            string script = "Sub OnDeliveryFailed(message, sRecipient, sErrorMessage)" + Environment.NewLine +
                            " EventLog.Write(\"File: \" & message.FileName) " + Environment.NewLine +
                            " EventLog.Write(\"Recipient: \" & sRecipient) " + Environment.NewLine +
                            " EventLog.Write(\"Error: \" & sErrorMessage) " + Environment.NewLine +
                            " End Sub";

            Scripting scripting = _settings.Scripting;
            string    file      = scripting.CurrentScriptFile;

            File.WriteAllText(file, script);
            scripting.Enabled = true;
            scripting.Reload();

            // Add an account and send a message to it.
            Account account1 = SingletonProvider <TestSetup> .Instance.AddAccount(_domain, "*****@*****.**", "test");

            SmtpClientSimulator.StaticSend(account1.Address, "*****@*****.**", "Test", "SampleBody");

            // Make sure that the message is deliverd and bounced.
            CustomAsserts.AssertRecipientsInDeliveryQueue(0);

            string eventLogText = TestSetup.ReadExistingTextFile(LogHandler.GetEventLogFileName());

            Assert.IsTrue(eventLogText.Contains("File: "));
            Assert.IsTrue(eventLogText.Contains("Recipient: [email protected]"));
            Assert.IsTrue(eventLogText.Contains("No mail servers appear to exists"));
        }
Exemple #4
0
        private string SendMessage(string body)
        {
            SingletonProvider <TestSetup> .Instance.AddAccount(_domain, "*****@*****.**", "test");

            var deliveryResults = new Dictionary <string, int>();

            deliveryResults["*****@*****.**"] = 250;

            int port = TestSetup.GetNextFreePort();

            using (var server = new SmtpServerSimulator(1, port))
            {
                server.SecondsToWaitBeforeTerminate = 60;
                server.AddRecipientResult(deliveryResults);
                server.StartListen();

                // Add a route so we can connect to localhost.
                AddRoutePointingAtLocalhost(5, port);

                // Send message to this route.
                var smtp       = new SmtpClientSimulator();
                var recipients = new List <string>();
                recipients.Add("*****@*****.**");
                smtp.Send("*****@*****.**", recipients, "Test", body);

                // Wait for the client to disconnect.
                server.WaitForCompletion();
                string messageData = server.MessageData;

                CustomAsserts.AssertRecipientsInDeliveryQueue(0);

                return(messageData);
            }
        }
        public void TestDeliverToMyselfOnLocalPort()
        {
            Assert.AreEqual(0, _status.UndeliveredMessages.Length);

            // Add a route so we can conenct to localhost.
            TestSetup.AddRoutePointingAtLocalhost(1, 25, false);

            // Send message to this route.
            SmtpClientSimulator.StaticSend("*****@*****.**", "*****@*****.**", "subject", "body");

            for (int i = 0; i < 40; i++)
            {
                string s = _status.UndeliveredMessages;
                if (s.Contains("\t\[email protected]"))
                {
                    break;
                }

                Thread.Sleep(250);
            }

            // Wait for the bounce message to be delivered.
            CustomAsserts.AssertRecipientsInDeliveryQueue(0, true);

            string message = Pop3ClientSimulator.AssertGetFirstMessageText("*****@*****.**", "test");

            Assert.IsTrue(message.Contains("this would mean connecting to myself."));
        }
        public void TestDeliverySuccessSingleRecipientMissingQuitResponse()
        {
            Assert.AreEqual(0, _status.UndeliveredMessages.Length);

            // No valid recipients...
            var deliveryResults = new Dictionary <string, int>();

            deliveryResults["*****@*****.**"] = 250;

            int smtpServerPort = TestSetup.GetNextFreePort();

            using (var server = new SmtpServerSimulator(1, smtpServerPort))
            {
                server.AddRecipientResult(deliveryResults);
                server.StartListen();
                server.SimulatedError = SimulatedErrorType.DisconnectWithoutReplyOnQuit;

                // Add a route so we can connect to localhost.
                TestSetup.AddRoutePointingAtLocalhost(5, smtpServerPort, false);

                // Send message to this route.
                var smtp       = new SmtpClientSimulator();
                var recipients = new List <string>();
                recipients.Add("*****@*****.**");
                smtp.Send("*****@*****.**", recipients, "Test", "DeliverySuccessNoQuitResponse");

                // Wait for the client to disconnect.
                server.WaitForCompletion();

                CustomAsserts.AssertRecipientsInDeliveryQueue(0, false);

                Assert.IsTrue(server.MessageData.Contains("DeliverySuccessNoQuitResponse"));
            }
        }
Exemple #7
0
        public void TestDeliverToExternalMimeRecipientsEnabledRouteAsExternal()
        {
            var messages = new List <string>();

            string message = "From: [email protected]\r\n" +
                             "To: \"Test\" <*****@*****.**>, \"ExternalGuy\" <*****@*****.**>\r\n" +
                             "Subject: Test\r\n" +
                             "\r\n" +
                             "Hello!";

            messages.Add(message);

            int port = TestSetup.GetNextFreePort();

            using (var pop3Server = new Pop3ServerSimulator(1, port, messages))
            {
                pop3Server.StartListen();

                // Add a route so we can connect to localhost.
                //Route route = SMTPClientTests.AddRoutePointingAtLocalhost(5, 250, false);
                //route.TreatSecurityAsLocalDomain = false;
                //route.Save();

                Account userAccount = SingletonProvider <TestSetup> .Instance.AddAccount(_domain, "*****@*****.**", "test");

                Account recipientAccount1 = SingletonProvider <TestSetup> .Instance.AddAccount(_domain, "*****@*****.**",
                                                                                               "test");

                Account catchallAccount = SingletonProvider <TestSetup> .Instance.AddAccount(_domain, "*****@*****.**",
                                                                                             "test");

                _domain.Postmaster = catchallAccount.Address;
                _domain.Save();

                FetchAccount fa = userAccount.FetchAccounts.Add();

                fa.Enabled             = true;
                fa.MinutesBetweenFetch = 10;
                fa.Name                  = "Test";
                fa.Username              = "******";
                fa.Password              = "******";
                fa.UseSSL                = false;
                fa.ServerAddress         = "localhost";
                fa.Port                  = port;
                fa.ProcessMIMERecipients = true;
                fa.EnableRouteRecipients = true;
                fa.Save();

                fa.DownloadNow();

                pop3Server.WaitForCompletion();

                fa.Delete();

                string downloadedMessage1 = Pop3ClientSimulator.AssertGetFirstMessageText(recipientAccount1.Address, "test");
                Assert.IsTrue(downloadedMessage1.Contains(message), downloadedMessage1);

                CustomAsserts.AssertRecipientsInDeliveryQueue(0, false);
            }
        }
Exemple #8
0
        public void TestBounceMessageSyntax()
        {
            Account senderAccount = SingletonProvider <TestSetup> .Instance.AddAccount(_domain, "*****@*****.**", "test");

            Account recipientAccount = SingletonProvider <TestSetup> .Instance.AddAccount(_domain, "*****@*****.**",
                                                                                          "test");

            recipientAccount.MaxSize = 1;
            recipientAccount.Save();

            // Build a 2MB string.
            var builder = new StringBuilder();

            for (int i = 0; i < 11000; i++)
            {
                builder.Append(
                    "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\r\n");
            }

            string text = builder.ToString();

            SmtpClientSimulator.StaticSend(senderAccount.Address, recipientAccount.Address, "", text);

            // Make sure the recipient did not receive it.
            CustomAsserts.AssertRecipientsInDeliveryQueue(0);

            // Check the syntax in the bounce message.
            string content = Pop3ClientSimulator.AssertGetFirstMessageText(senderAccount.Address, "test");

            // The bounce message should contain the MIME-version.
            Assert.IsTrue(content.Contains("MIME-Version: 1.0"));

            // Lines may not end with space if they are QP-encoded.
            Assert.IsTrue(content.Contains("  Subject:=20"));
        }
Exemple #9
0
        public void SmtpServerSupportingStartTls_StartTlsOptional()
        {
            Account account = SingletonProvider <TestSetup> .Instance.AddAccount(_domain, "*****@*****.**", "test");

            // Set up a server listening on port 250 which accepts email for [email protected]
            var deliveryResults = new Dictionary <string, int>();

            deliveryResults["*****@*****.**"] = 250;

            int smtpServerPort = TestSetup.GetNextFreePort();

            using (var server = new SmtpServerSimulator(1, smtpServerPort, eConnectionSecurity.eCSSTARTTLSRequired))
            {
                server.SetCertificate(SslSetup.GetCertificate());
                server.AddRecipientResult(deliveryResults);
                server.StartListen();

                Route route = TestSetup.AddRoutePointingAtLocalhost(1, smtpServerPort, true, eConnectionSecurity.eCSSTARTTLSOptional);

                var smtpClient = new SmtpClientSimulator();
                smtpClient.Send(account.Address, "*****@*****.**", "Test", "Test message");

                CustomAsserts.AssertRecipientsInDeliveryQueue(0);

                server.WaitForCompletion();

                Assert.IsTrue(server.MessageData.Contains("Test message"));
            }
        }
        public void DoNotUseStartTlsIfNotEnabledButAvailable()
        {
            // No valid recipients...
            var deliveryResults = new Dictionary <string, int>();

            deliveryResults["*****@*****.**"] = 250;

            int smtpServerPort = TestSetup.GetNextFreePort();

            using (var server = new SmtpServerSimulator(1, smtpServerPort, eConnectionSecurity.eCSSTARTTLSOptional))
            {
                server.AddRecipientResult(deliveryResults);
                server.StartListen();

                Route route = TestSetup.AddRoutePointingAtLocalhost(1, smtpServerPort, false, eConnectionSecurity.eCSNone);

                // Send message to this route.
                var smtp = new SmtpClientSimulator();
                smtp.Send("*****@*****.**", "*****@*****.**", "Test", "Test message");


                // Wait for the client to disconnect.
                server.WaitForCompletion();

                CustomAsserts.AssertRecipientsInDeliveryQueue(0, false);

                Assert.IsNotNullOrEmpty(server.MessageData);

                Assert.IsFalse(LogHandler.DefaultLogContains("220 Ready to start TLS"));
            }
        }
        public void TestDeliverToMyselfOnLocalPortAfterChangedLocalPort()
        {
            TCPIPPorts oPorts = _application.Settings.TCPIPPorts;

            for (int i = 0; i < oPorts.Count; i++)
            {
                TCPIPPort oTestPort = oPorts[i];
                if (oTestPort.Protocol == eSessionType.eSTIMAP)
                {
                    oTestPort.PortNumber = 14300;
                }
                else if (oTestPort.Protocol == eSessionType.eSTSMTP)
                {
                    oTestPort.PortNumber = 11000;
                }
                else if (oTestPort.Protocol == eSessionType.eSTPOP3)
                {
                    oTestPort.PortNumber = 2500;
                }

                oTestPort.Save();
            }

            _application.Stop();
            _application.Start();

            // Set up a server and start listening.
            var deliveryResults = new Dictionary <string, int>();

            deliveryResults["*****@*****.**"] = 250;

            int smtpServerPort = TestSetup.GetNextFreePort();

            using (var server = new SmtpServerSimulator(1, smtpServerPort))
            {
                server.AddRecipientResult(deliveryResults);
                server.StartListen();

                // Add a route so we can connect to localhost.
                TestSetup.AddRoutePointingAtLocalhost(5, smtpServerPort, false);

                // Send message to this route.
                var sim = new SmtpClientSimulator(false, 11000);
                sim.Send("*****@*****.**", "*****@*****.**", "Test", "Test message");


                // Wait for the client to disconnect.
                server.WaitForCompletion();

                CustomAsserts.AssertRecipientsInDeliveryQueue(0, false);

                Assert.IsTrue(server.MessageData.Contains("Test message"));
            }

            oPorts.SetDefault();
            _application.Stop();
            _application.Start();
        }
        public void TestFatalDeliveryFailure()
        {
            _application.Settings.Logging.Enabled  = true;
            _application.Settings.Logging.LogSMTP  = true;
            _application.Settings.Logging.LogTCPIP = true;
            _application.Settings.Logging.LogDebug = true;

            Assert.AreEqual(0, _status.UndeliveredMessages.Length);

            // No valid recipients...
            var deliveryResults = new Dictionary <string, int>();

            deliveryResults["*****@*****.**"] = 550;

            int smtpServerPort = TestSetup.GetNextFreePort();

            using (var server = new SmtpServerSimulator(1, smtpServerPort))
            {
                server.AddRecipientResult(deliveryResults);
                server.StartListen();

                // Add a route so we can conenct to localhost.
                TestSetup.AddRoutePointingAtLocalhost(0, smtpServerPort, false);

                // Send message to this route.
                var smtp       = new SmtpClientSimulator();
                var recipients = new List <string>();
                recipients.Add("*****@*****.**");
                smtp.Send("*****@*****.**", recipients, "Test", "Test message");

                string undeliveredMessages = _status.UndeliveredMessages;

                // Wait for the client to disconnect.
                server.WaitForCompletion();

                // wait for the message to be delivered.
                for (int i = 1; i <= 40; i++)
                {
                    Assert.IsFalse(i == 40);

                    if (_status.UndeliveredMessages.Contains("\[email protected]") || _status.UndeliveredMessages.Length == 0)
                    {
                        break;
                    }

                    Thread.Sleep(250);
                }

                CustomAsserts.AssertRecipientsInDeliveryQueue(0, true);

                string bounceMessage = Pop3ClientSimulator.AssertGetFirstMessageText(_account.Address, "test");

                Assert.IsTrue(bounceMessage.Contains("Remote server (127.0.0.1) issued an error."));
                Assert.IsTrue(bounceMessage.Contains("550 [email protected]"));
            }
        }
        public void TestMultipleHostsHalfDeliveryOnFirstPermanentOnSecond()
        {
            Assert.AreEqual(0, _status.UndeliveredMessages.Length);

            // No valid recipients...
            var deliveryResults = new Dictionary <string, int>();

            deliveryResults["*****@*****.**"] = 250;
            deliveryResults["*****@*****.**"] = 250;
            deliveryResults["*****@*****.**"] = 450;

            var deliveryResultsSecond = new Dictionary <string, int>();

            deliveryResultsSecond["*****@*****.**"] = 550;
            deliveryResultsSecond["*****@*****.**"] = 550;
            deliveryResultsSecond["*****@*****.**"] = 500;

            int smtpServerPort = TestSetup.GetNextFreePort();

            using (var server = new SmtpServerSimulator(2, smtpServerPort))
            {
                server.AddRecipientResult(deliveryResults);
                server.AddRecipientResult(deliveryResultsSecond);
                server.StartListen();


                // Add a route so we can connect to localhost.
                TestSetup.AddRoutePointingAtLocalhostMultipleHosts(1, smtpServerPort);

                // Send message to this route.
                var smtp       = new SmtpClientSimulator();
                var recipients = new List <string>();

                recipients.Add("*****@*****.**");
                recipients.Add("*****@*****.**");
                recipients.Add("*****@*****.**");

                smtp.Send("*****@*****.**", recipients, "Test", "Accepted message");

                // Wait for the client to disconnect.
                server.WaitForCompletion();

                // Trigger a sending of the bounce message.
                CustomAsserts.AssertRecipientsInDeliveryQueue(0, true);

                string bounceMessage = Pop3ClientSimulator.AssertGetFirstMessageText(_account.Address, "test");

                Assert.IsFalse(bounceMessage.Contains("RCPT TO:<*****@*****.**>"));
                Assert.IsFalse(bounceMessage.Contains("RCPT TO:<*****@*****.**>"));
                Assert.IsTrue(bounceMessage.Contains("RCPT TO:<*****@*****.**>"));
                Assert.IsTrue(bounceMessage.Contains("500 [email protected]"));
            }
        }
        public void TestTemporaryFailure()
        {
            Assert.AreEqual(0, _status.UndeliveredMessages.Length);

            var deliveryResults = new Dictionary <string, int>();

            deliveryResults["*****@*****.**"] = 452;

            int smtpServerPort = TestSetup.GetNextFreePort();

            using (var server = new SmtpServerSimulator(1, smtpServerPort))
            {
                server.AddRecipientResult(deliveryResults);
                server.StartListen();

                // Add a route so we can connect to localhost.
                TestSetup.AddRoutePointingAtLocalhost(2, smtpServerPort, false);

                // Send message to this route.
                var smtp       = new SmtpClientSimulator();
                var recipients = new List <string>();
                recipients.Add("*****@*****.**");
                smtp.Send("*****@*****.**", recipients, "Test", "Test message");

                // Wait for the client to disconnect.
                server.WaitForCompletion();

                Assert.AreNotEqual(0, _status.UndeliveredMessages.Length);
            }

            using (var server = new SmtpServerSimulator(1, smtpServerPort))// Start to listen again.
            {
                server.AddRecipientResult(deliveryResults);
                server.StartListen();

                // Deliver the message to the server and then deliver the bounce message.
                CustomAsserts.AssertRecipientsInDeliveryQueue(0, true);

                // Wait for the client to disconnect.
                server.WaitForCompletion();

                Assert.IsFalse(server.Conversation.Contains("\r\nDATA\r\n"));
            }

            // Now the message has bounced.
            string message = Pop3ClientSimulator.AssertGetFirstMessageText(_account.Address, "test");

            Assert.IsTrue(message.Contains("452 [email protected]"));
            Assert.IsTrue(message.Contains("Tried 2 time(s)"));
        }
Exemple #15
0
        public void MailFromShouldNotUpdatedLastLogonTime()
        {
            Account account = SingletonProvider <TestSetup> .Instance.AddAccount(_domain, "us'*****@*****.**", "test");

            DateTime lastLogonTimeBefore = Convert.ToDateTime(account.LastLogonTime);

            Thread.Sleep(1000);
            SmtpClientSimulator.StaticSend("*****@*****.**", "us'*****@*****.**", "Test", "Test");

            CustomAsserts.AssertRecipientsInDeliveryQueue(0);
            DateTime lastLogonTimeAfter =
                Convert.ToDateTime(SingletonProvider <TestSetup> .Instance.GetApp().Domains[0].Accounts[0].LastLogonTime);

            Assert.AreEqual(lastLogonTimeBefore, lastLogonTimeAfter);
        }
Exemple #16
0
        public void RoutesShouldConsolidateRecipients()
        {
            // Set up a server listening on port 250 which accepts email for [email protected]
            var deliveryResults = new Dictionary <string, int>();

            deliveryResults["*****@*****.**"] = 250;
            deliveryResults["*****@*****.**"] = 250;
            deliveryResults["*****@*****.**"] = 250;
            deliveryResults["*****@*****.**"] = 250;

            int smtpServerPort = TestSetup.GetNextFreePort();

            using (var server = new SmtpServerSimulator(1, smtpServerPort))
            {
                server.AddRecipientResult(deliveryResults);
                server.StartListen();

                // Add a route pointing at localhost
                Route route = _settings.Routes.Add();
                route.DomainName                  = "test.com";
                route.TargetSMTPHost              = "localhost";
                route.TargetSMTPPort              = smtpServerPort;
                route.NumberOfTries               = 1;
                route.MinutesBetweenTry           = 5;
                route.TreatRecipientAsLocalDomain = true;
                route.TreatSenderAsLocalDomain    = true;
                route.AllAddresses                = true;
                route.Save();

                var smtpClient = new SmtpClientSimulator();

                var recipients = new List <string>()
                {
                    "*****@*****.**",
                    "*****@*****.**",
                    "*****@*****.**",
                    "*****@*****.**"
                };

                smtpClient.Send("*****@*****.**", recipients, "Test", "Test message");
                CustomAsserts.AssertRecipientsInDeliveryQueue(0);

                server.WaitForCompletion();

                Assert.IsTrue(server.MessageData.Contains("Test message"));
                Assert.AreEqual(deliveryResults.Count, server.RcptTosReceived);
            }
        }
Exemple #17
0
        public static void AssertMessageCount(string accountName, string accountPassword, string folderName,
                                              int expectedCount)
        {
            if (expectedCount == 0)
            {
                // make sure that we aren't currently delivering messages.
                CustomAsserts.AssertRecipientsInDeliveryQueue(0);
            }

            var imap = new ImapClientSimulator();

            Assert.IsTrue(imap.ConnectAndLogon(accountName, accountPassword));

            if (expectedCount != 0)
            {
                imap.AssertFolderExists(folderName);
            }

            int currentCount = 0;
            int timeout      = 1000; // 1000 * 25 = 25 seconds.

            while (timeout > 0)
            {
                currentCount = imap.GetMessageCount(folderName);

                if (currentCount > expectedCount)
                {
                    break;
                }

                if (currentCount == expectedCount)
                {
                    imap.Disconnect();
                    return;
                }

                timeout--;
                Thread.Sleep(25);
            }

            imap.Disconnect();

            string error = "Wrong number of messages in mailbox " + folderName + " in account " + accountName +
                           " Actual: " + currentCount.ToString() + " Expected: " + expectedCount.ToString();

            Assert.Fail(error);
        }
Exemple #18
0
        public void DomainAliasesShouldNotRewriteRecipientList()
        {
            DomainAlias oDA = _domain.DomainAliases.Add();

            oDA.AliasName = "dummy-example.com";
            oDA.Save();

            Account account = SingletonProvider <TestSetup> .Instance.AddAccount(_domain, "*****@*****.**", "test");

            // Set up a server listening on port 250 which accepts email for [email protected]
            var deliveryResults = new Dictionary <string, int>();

            deliveryResults["*****@*****.**"] = 250;

            int smtpServerPort = TestSetup.GetNextFreePort();

            using (var server = new SmtpServerSimulator(1, smtpServerPort))
            {
                server.AddRecipientResult(deliveryResults);
                server.StartListen();


                Route route = TestSetup.AddRoutePointingAtLocalhost(1, smtpServerPort, true, eConnectionSecurity.eCSNone);

                // Now send a message from [email protected] to [email protected].
                // Error scenario:
                // In the recipientlist, otherdomain.com gets rewritten to test.com
                // since there's a domain alias named otherdomain.co on the domain
                // test.com.
                //
                // This should not happen. Otherdomain.com is an alias for test.com,
                // but we shouldn't actually modify the recipient address just because
                // of this.
                var smtpClient = new SmtpClientSimulator();
                smtpClient.Send(account.Address, "*****@*****.**", "Test", "Test message");

                CustomAsserts.AssertRecipientsInDeliveryQueue(0);

                Pop3ClientSimulator.AssertMessageCount(account.Address, "test", 0);

                // This should now be processed via the rule -> route -> external server we've set up.
                server.WaitForCompletion();

                Assert.IsTrue(server.MessageData.Contains("Test message"));
            }
        }
Exemple #19
0
        public void RoutesShouldHaveHigherPrioThanSMTPRelay()
        {
            // Set up a server listening on port 250 which accepts email for [email protected]
            var deliveryResults = new Dictionary <string, int>();

            deliveryResults["*****@*****.**"] = 250;

            // We set the SMTP relayer here, but this should be ignored since the recipient's
            // address matches a route set up (test.com).
            _application.Settings.SMTPRelayer = "example.com";

            int smtpServerPort = TestSetup.GetNextFreePort();

            using (var server = new SmtpServerSimulator(1, smtpServerPort))
            {
                server.AddRecipientResult(deliveryResults);
                server.StartListen();

                // Add a route pointing at localhost
                Route route = _settings.Routes.Add();
                route.DomainName                  = "test.com";
                route.TargetSMTPHost              = "localhost";
                route.TargetSMTPPort              = smtpServerPort;
                route.NumberOfTries               = 1;
                route.MinutesBetweenTry           = 5;
                route.TreatRecipientAsLocalDomain = true;
                route.TreatSenderAsLocalDomain    = true;
                route.AllAddresses                = false;
                route.Save();

                // Make sure only the specific user is valid.
                RouteAddress routeAddress = route.Addresses.Add();
                routeAddress.Address = "user@" + _domain.Name;
                routeAddress.Save();

                SingletonProvider <TestSetup> .Instance.AddAlias(_domain, "*****@*****.**", "*****@*****.**");

                var smtpClient = new SmtpClientSimulator();
                smtpClient.Send("*****@*****.**", "*****@*****.**", "Test", "Test message");
                CustomAsserts.AssertRecipientsInDeliveryQueue(0);

                server.WaitForCompletion();

                Assert.IsTrue(server.MessageData.Contains("Test message"));
            }
        }
        public static void AssertMessageCount(string accountName, string accountPassword, int expectedCount, TimeSpan timeout)
        {
            var timeoutTime = DateTime.UtcNow + timeout;

            if (expectedCount == 0)
            {
                // just in case.
                CustomAsserts.AssertRecipientsInDeliveryQueue(0);
            }

            int actualCount = 0;

            DateTime lastDebugEntryTimestamp = DateTime.UtcNow;


            while (DateTime.UtcNow < timeoutTime)
            {
                var pop3ClientSimulator = new Pop3ClientSimulator();

                actualCount = pop3ClientSimulator.GetMessageCount(accountName, accountPassword);
                if (actualCount == expectedCount)
                {
                    return;
                }

                if (actualCount > expectedCount)
                {
                    Assert.Fail(
                        string.Format(
                            "Actual count exceeds expected count. Account name: {2}, Actual: {0}, Expected: {1}.",
                            actualCount, expectedCount, accountName));
                }

                if (DateTime.UtcNow - lastDebugEntryTimestamp > TimeSpan.FromSeconds(10))
                {
                    Console.WriteLine("Waiting for messages to appear in account. Actual: {0}, Expected: {1}", actualCount, expectedCount);

                    lastDebugEntryTimestamp = DateTime.UtcNow;
                }

                Thread.Sleep(50);
            }

            Assert.Fail(string.Format("Wrong number of messages in inbox for {0}. Actual: {1}, Expected: {2}",
                                      accountName, actualCount, expectedCount));
        }
Exemple #21
0
        public void BounceMessageShouldContainSubjectAndDate()
        {
            Account senderAccount = SingletonProvider <TestSetup> .Instance.AddAccount(_domain, "*****@*****.**", "test");

            Account recipientAccount = SingletonProvider <TestSetup> .Instance.AddAccount(_domain, "*****@*****.**",
                                                                                          "test");

            recipientAccount.MaxSize = 1;
            recipientAccount.Save();

            // Make sure that no bounce is sent.
            SmtpClientSimulator.StaticSend(senderAccount.Address, recipientAccount.Address, "MySubject", "Test");
            Pop3ClientSimulator.AssertGetFirstMessageText(recipientAccount.Address, "test");

            // Build a 2MB string.
            var builder = new StringBuilder();

            for (int i = 0; i < 11000; i++)
            {
                builder.Append(
                    "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\r\n");
            }
            string text = builder.ToString();

            SmtpClientSimulator.StaticSend(senderAccount.Address, recipientAccount.Address, "Test subject", text);

            // Make sure the recipient did not receive it.
            CustomAsserts.AssertRecipientsInDeliveryQueue(0);
            Pop3ClientSimulator.AssertMessageCount(recipientAccount.Address, "test", 0);
            CustomAsserts.AssertFilesInUserDirectory(recipientAccount, 0);

            // Make sure it bounced.
            string content = Pop3ClientSimulator.AssertGetFirstMessageText(senderAccount.Address, "test");

            Assert.IsTrue(content.Contains("Inbox is full"));
            Assert.IsTrue(content.Contains("Subject: Test subject"));

            // Make sure body contains year.
            int bodyStartPos = content.IndexOf("\r\n\r\n");
            int yearPos      = content.IndexOf(DateTime.Now.Year.ToString(), bodyStartPos);

            Assert.IsTrue(yearPos >= 0);
        }
        public void TestDeliverySuccess250Recipients()
        {
            Assert.AreEqual(0, _status.UndeliveredMessages.Length);

            // No valid recipients...
            var deliveryResults = new Dictionary <string, int>();

            for (int i = 0; i < 250; i++)
            {
                deliveryResults["user" + i.ToString() + "@dummy-example.com"] = 250;
            }

            int smtpServerPort = TestSetup.GetNextFreePort();

            using (var server = new SmtpServerSimulator(3, smtpServerPort))
            {
                server.AddRecipientResult(deliveryResults);
                server.AddRecipientResult(deliveryResults);
                server.AddRecipientResult(deliveryResults);
                server.StartListen();

                // Add a route so we can connect to localhost.
                TestSetup.AddRoutePointingAtLocalhost(5, smtpServerPort, false);

                // Send message to this route.
                var smtp       = new SmtpClientSimulator();
                var recipients = new List <string>();

                for (int i = 0; i < 250; i++)
                {
                    recipients.Add("user" + i.ToString() + "@dummy-example.com");
                }

                smtp.Send("*****@*****.**", recipients, "Test", "Test message");

                // Wait for the client to disconnect.
                server.WaitForCompletion();

                CustomAsserts.AssertRecipientsInDeliveryQueue(0, false);

                Assert.IsTrue(server.MessageData.Contains("Test message"));
            }
        }
        public void TestSMTPAuthExternalToLocal()
        {
            SecurityRange range =
                SingletonProvider <TestSetup> .Instance.GetApp().Settings.SecurityRanges.get_ItemByName("My computer");

            range.RequireSMTPAuthExternalToLocal = true;
            range.Save();

            Account account1 = SingletonProvider <TestSetup> .Instance.AddAccount(_domain, "*****@*****.**", "test");

            var smtpClientSimulator = new SmtpClientSimulator();

            CustomAsserts.Throws <DeliveryFailedException>(() => smtpClientSimulator.Send("*****@*****.**", account1.Address, "Mail 1", "Mail 1"));

            range.RequireSMTPAuthExternalToLocal = false;
            range.Save();

            smtpClientSimulator.Send("*****@*****.**", account1.Address, "Mail 1", "Mail 1");
            CustomAsserts.AssertRecipientsInDeliveryQueue(0);
        }
Exemple #24
0
        public void SmtpRelayShouldConsolidateRecipients()
        {
            // Set up a server listening on port 250 which accepts email for [email protected]
            var deliveryResults = new Dictionary <string, int>();

            deliveryResults["*****@*****.**"] = 250;
            deliveryResults["*****@*****.**"] = 250;
            deliveryResults["*****@*****.**"] = 250;
            deliveryResults["*****@*****.**"] = 250;

            int smtpServerPort = TestSetup.GetNextFreePort();

            using (var server = new SmtpServerSimulator(1, smtpServerPort))
            {
                _application.Settings.SMTPRelayer     = "localhost";
                _application.Settings.SMTPRelayerPort = smtpServerPort;

                server.AddRecipientResult(deliveryResults);
                server.StartListen();

                var smtpClient = new SmtpClientSimulator();

                var recipients = new List <string>()
                {
                    "*****@*****.**",
                    "*****@*****.**",
                    "*****@*****.**",
                    "*****@*****.**"
                };

                Account senderAccount = SingletonProvider <TestSetup> .Instance.AddAccount(_domain, "*****@*****.**", "test");

                smtpClient.Send(senderAccount.Address, recipients, "Test", "Test message");
                CustomAsserts.AssertRecipientsInDeliveryQueue(0);

                server.WaitForCompletion();

                Assert.IsTrue(server.MessageData.Contains("Test message"));
                Assert.AreEqual(deliveryResults.Count, server.RcptTosReceived);
            }
        }
        public void TestFailureAfterReceivedHelloBanner()
        {
            _application.Settings.SMTPNoOfTries         = 3;
            _application.Settings.SMTPMinutesBetweenTry = 60;

            Assert.AreEqual(0, _status.UndeliveredMessages.Length);
            // No valid recipients...
            var deliveryResults = new Dictionary <string, int>();

            deliveryResults["*****@*****.**"] = 250;

            int smtpServerPort = TestSetup.GetNextFreePort();

            using (var server = new SmtpServerSimulator(1, smtpServerPort))
            {
                server.AddRecipientResult(deliveryResults);
                server.SimulatedError = SimulatedErrorType.DisconnectAfterSessionStart;
                server.StartListen();

                // Add a route so we can connect to localhost.
                TestSetup.AddRoutePointingAtLocalhost(1, smtpServerPort, false);

                // Send message to this route.
                var smtp       = new SmtpClientSimulator();
                var recipients = new List <string>();
                recipients.Add("*****@*****.**");
                smtp.Send(_account.Address, recipients, "Test", "Test message");

                // Wait for the client to disconnect.
                server.WaitForCompletion();

                // Force the message to be bounced.
                CustomAsserts.AssertRecipientsInDeliveryQueue(0, true);

                string bounce = Pop3ClientSimulator.AssertGetFirstMessageText(_account.Address, "test");

                Assert.IsTrue(bounce.Contains("*****@*****.**"));
                Assert.IsTrue(bounce.Contains("Remote server closed connection."));
                Assert.IsTrue(bounce.Contains("Tried 1 time(s)"));
            }
        }
Exemple #26
0
        public void TestWithVirus()
        {
            _antiVirus.ClamAVEnabled = true;
            LogHandler.DeleteCurrentDefaultLog();

            Account account1 = SingletonProvider <TestSetup> .Instance.AddAccount(_domain, "*****@*****.**", "test");

            string firstPart  = @"X5O!P%@AP[4\PZX54(P^)7CC)7}";
            string secondPart = @"$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*";

            SmtpClientSimulator.StaticSend(account1.Address, account1.Address, "Mail 1",
                                           firstPart + secondPart);

            CustomAsserts.AssertRecipientsInDeliveryQueue(0);
            Pop3ClientSimulator.AssertMessageCount(account1.Address, "test", 0);

            string defaultLog = LogHandler.ReadCurrentDefaultLog();

            Assert.IsTrue(defaultLog.Contains("Connecting to ClamAV"));
            Assert.IsTrue(defaultLog.Contains("Message deleted (contained virus Eicar-Test-Signature)"));
        }
Exemple #27
0
        public void RoutesShouldSupportWildcardDomain()
        {
            // Set up a server listening on port 250 which accepts email for [email protected]
            var deliveryResults = new Dictionary <string, int>();

            deliveryResults["*****@*****.**"] = 250;

            int smtpServerPort = TestSetup.GetNextFreePort();

            using (var server = new SmtpServerSimulator(1, smtpServerPort))
            {
                server.AddRecipientResult(deliveryResults);
                server.StartListen();

                // Add a route pointing at localhost
                Route route = _settings.Routes.Add();
                route.DomainName                  = "*.example.com";
                route.TargetSMTPHost              = "localhost";
                route.TargetSMTPPort              = smtpServerPort;
                route.NumberOfTries               = 1;
                route.MinutesBetweenTry           = 5;
                route.TreatRecipientAsLocalDomain = true;
                route.TreatSenderAsLocalDomain    = true;
                route.AllAddresses                = true;
                route.Save();

                // Make sure only the specific user is valid.
                RouteAddress routeAddress = route.Addresses.Add();
                routeAddress.Address = "user@" + _domain.Name;
                routeAddress.Save();

                var smtpClient = new SmtpClientSimulator();
                smtpClient.Send("*****@*****.**", "*****@*****.**", "Test", "Test message");
                CustomAsserts.AssertRecipientsInDeliveryQueue(0);

                server.WaitForCompletion();

                Assert.IsTrue(server.MessageData.Contains("Test message"));
            }
        }
        public void TestAuthFailurePasswordInBounce()
        {
            Assert.AreEqual(0, _status.UndeliveredMessages.Length);

            // No valid recipients...
            var deliveryResults = new Dictionary <string, int>();

            deliveryResults["*****@*****.**"] = 250;

            int smtpServerPort = TestSetup.GetNextFreePort();

            using (var server = new SmtpServerSimulator(1, smtpServerPort))
            {
                server.AddRecipientResult(deliveryResults);
                server.SimulatedError = SimulatedErrorType.ForceAuthenticationFailure;
                server.StartListen();

                // Add a route so we can connect to localhost.
                Route route = TestSetup.AddRoutePointingAtLocalhost(5, smtpServerPort, false);
                route.RelayerRequiresAuth = true;
                route.RelayerAuthUsername = "******";
                route.SetRelayerAuthPassword("MySecretPassword");

                // Send message to this route.
                var smtp       = new SmtpClientSimulator();
                var recipients = new List <string>();
                recipients.Add("*****@*****.**");
                smtp.Send("*****@*****.**", recipients, "Test", "Test message");

                // Wait for the client to disconnect.
                server.WaitForCompletion();

                CustomAsserts.AssertRecipientsInDeliveryQueue(0);

                string messageText = Pop3ClientSimulator.AssertGetFirstMessageText(_account.Address, "test");

                Assert.IsFalse(messageText.Contains("MySecretPassword"));
                Assert.IsTrue(messageText.Contains("<Password removed>"));
            }
        }
        public void TestSMTPAuthLocalToLocal()
        {
            SecurityRange range =
                SingletonProvider <TestSetup> .Instance.GetApp().Settings.SecurityRanges.get_ItemByName("My computer");

            range.RequireSMTPAuthLocalToLocal = true;
            range.Save();

            Account account1 = SingletonProvider <TestSetup> .Instance.AddAccount(_domain, "*****@*****.**", "test");

            var    smtpClientSimulator = new SmtpClientSimulator();
            string result = "";

            CustomAsserts.Throws <DeliveryFailedException>(() => smtpClientSimulator.Send(account1.Address, account1.Address, "Mail 1", "Mail 1", out result));
            Assert.IsTrue(result.Contains("SMTP authentication is required."));

            range.RequireSMTPAuthLocalToLocal = false;
            range.Save();

            smtpClientSimulator.Send(account1.Address, account1.Address, "Mail 1", "Mail 1", out result);
            CustomAsserts.AssertRecipientsInDeliveryQueue(0);
        }
Exemple #30
0
        public void TestSMTPClient()
        {
            Account account1 = SingletonProvider <TestSetup> .Instance.AddAccount(_domain, "*****@*****.**", "test");

            Assert.AreEqual(0, _status.UndeliveredMessages.Length);

            // No valid recipients...
            var deliveryResults = new Dictionary <string, int>();

            deliveryResults["*****@*****.**"] = 550;

            int smtpServerPort = TestSetup.GetNextFreePort();

            using (var server = new SmtpServerSimulator(1, smtpServerPort))
            {
                server.AddRecipientResult(deliveryResults);
                server.StartListen();

                // Add a route so we can connect to localhost.
                AddRoutePointingAtLocalhostWithAuth(0, smtpServerPort);

                // Send message to this route.
                var smtp = new SmtpClientSimulator();
                smtp.Send("*****@*****.**", "*****@*****.**", "Test", "Test message");

                CustomAsserts.AssertRecipientsInDeliveryQueue(0);

                string undeliveredMessages = _status.UndeliveredMessages;

                // Wait for the client to disconnect.
                server.WaitForCompletion();

                CustomAsserts.AssertRecipientsInDeliveryQueue(0);

                EnsureNoPassword();
            }
        }