I have an SMTPClient, using MailKit, that sends a message with the follow code.
using (var client = new SmtpClient())
{
client.ServerCertificateValidationCallback = (s, c, h, e) => true;
client.Connect("localhost", 587, SecureSocketOptions.None);
client.Send(message);
client.Disconnect(true);
}
My SMTP server is implemented using cosullican/SmtpServer. I setup the server as follows:
var optionsBuilder = new SmtpServerOptionsBuilder()
.ServerName(serverName)
.Endpoint(builder =>
builder.Port(587, true)
.AllowUnsecureAuthentication(false)
.Certificate(CreateCertificate()));
var options = optionsBuilder.Build();
return new SmtpServer.SmtpServer(options, provider.GetRequiredService<IServiceProvider>());
I can't seem to get this working with StartTLS. I get a timeout when the client attempts to connect.
System.IO.IOException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
It will work when configured to not use TLS. To not use TLS the client uses SecureSocketOptions.None to connect, and for the server I remove the .Certificate line.
I think the client is setup correctly, and I am probably missing something on the server.
Note: My service is using a self-signed cert. I am developing this on my box at the moment.
How do I implement StartTLS?
The problem was this line:
builder.Port(587, true)
Is should be:
builder.Port(587, false)