cosullivan / SmtpServer

A SMTP Server component written in C#
MIT License
676 stars 160 forks source link

SMTP Timeout issue while more than 100 emails #168

Closed vesnaveen closed 9 months ago

vesnaveen commented 3 years ago

We are using this SMTP server for two years. our client now sends more than 100 emails together in one connection and sometimes its works and sometimes its throws a timeout error. Can we have any additional function of this or any solution that allows multiple emails in the same connection?

public void Run()
        {
            try
            {
                _cancellationTokenSource = new CancellationTokenSource();
                var options = new SmtpServerOptionsBuilder()
                    .ServerName(Host)
                    .Port(Port)
                    .MessageStore(new SmtpMessageStore())
                    .MailboxFilter(new SmtpMailboxFilter())
                    .UserAuthenticator(new SmtpUserAuthenticator())
                    .AllowUnsecureAuthentication()
                    .CommandWaitTimeout(TimeSpan.FromMinutes(3))
                    .Build();
                var server = new SmtpServer.SmtpServer(options);
                server.SessionCreated += OnSessionCreated;
                server.SessionCompleted += OnSessionCompleted;
                var serverTask = server.StartAsync(_cancellationTokenSource.Token);
            }
            catch (Exception ex)
            {
                logger.Error("SmtpServer Run: {0}", ex.ToString());
            }
        }
cosullivan commented 3 years ago

Hi @vesnaveen ,

What version are you using? it should support well and truly more than 100 so I suspect there might be another issue at play.

Just looking at your code that you posted, is this the actual code you are using? I notice you are starting the server (StartAsync) but not waiting for that task anywhere. How do you keep the application alive and running?

Do you have the call stack of the exception?

Thanks, Cain.

vesnaveen commented 3 years ago

Hi, I was using the old version and now I have moved this to the latest version SmtpServer -Version 9.0.1. then again it's throwing the same Timeout exception. I have also used the same as https://github.com/cosullivan/SmtpServer example for StartAsync. What should I do to increase my time period?

Also, I know that what is default time of this function CommandWaitTimeout

cosullivan commented 3 years ago

The default ReadTimeout is set to 2 minutes (it can be configured on the Endpoint) however, I don't think that will be causing an issue.

Can you send the Exception stack trace?

vesnaveen commented 3 years ago

You mean I have to send the timeout functionality to the SMTP client side. right? Can I use increased timeout limit from the SMTP server-side also? I have sent the image below for the exception. Please check this. Untitled

cosullivan commented 3 years ago

The default CommandWaitTimeout is 5 minutes, but that is the timeout that is applied to waiting for a single command to be read from the client. So it's unlikely this timeout would be an issue unless the SMTP client is just sitting there and not sending anything.

You could try adjusting the ReadTimeout on the endpoint like

 var options = new SmtpServerOptionsBuilder()
                .ServerName("SmtpServer SampleApp")
                .Endpoint(builder =>
                    builder
                        .Port(9025, true)
                        .ReadTimeout(TimeSpan.FromMinutes(10))
                        .AllowUnsecureAuthentication(false)
                        .Certificate(CreateCertificate()))
                .Build();

But I don't think that would be the issue. It's hard to know with that stack trace whether it is the Server that is causing the timeout or the client.