smtp - SMTPServer - How to support StartTLS? - Stack Overflow

I have an SMTPClient, using MailKit, that sends a message with the follow code.using (var client = new

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?

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?

Share Improve this question edited Mar 7 at 17:23 Don Chambers asked Mar 7 at 15:58 Don ChambersDon Chambers 4,31710 gold badges35 silver badges87 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

The problem was this line:

builder.Port(587, true)

Is should be:

builder.Port(587, false)

Setting this to true means the endpoint is secure by default and in which case it will try to update the connection to SSL when the client connects rather than waiting for the client to issue a STARTTLS command.

Change:

client.Connect("localhost", 587, SecureSocketOptions.None);

to

client.Connect("localhost", 587, SecureSocketOptions.StartTLS);

If that doesn't work, can you connect to localhost:587 using telnet?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744919335a4601034.html

相关推荐

  • smtp - SMTPServer - How to support StartTLS? - Stack Overflow

    I have an SMTPClient, using MailKit, that sends a message with the follow code.using (var client = new

    1天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信