6

I have an console application and I installed mailkit package for messaging purposes.

I have code in the main method to test mailkit smtp client. I have smtp4dev dummy server running and the client code is the example code of mailkit in github with the authentication part commented, the host is localhost and the port 26, matching the smtp4dev configuration.

When the client code is executed the smtp4dev stop running and an unhandled exception occurrs, IOException: Unable to read data from the transport connection: an existing connection was forcibly closed by the remote host.

How can I configure smtp4dev to receive message from mailkit client?

Dalsier
  • 377
  • 3
  • 14

2 Answers2

12

After some trial and error, I was able to have success with the following arrangement. smtp4dev options

My code is similar to https://github.com/jstedfast/MailKit#sending-messages:

public void DoMail()
{
    var message = new MimeMessage();
    message.From.Add(new MailboxAddress("Joey", "joey@friends.com"));
    message.To.Add(new MailboxAddress("Alice", "alice@wonderland.com"));
    message.Subject = "How you doin?";

    message.Body = new TextPart("plain")
    {
        Text = @"Hey Alice,

What are you up to this weekend? Monica is throwing one of her parties on
Saturday and I was hoping you could make it.

Will you be my +1?

-- Joey
"
    };

    using (var client = new SmtpClient())
    {
        // For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS)
        client.ServerCertificateValidationCallback = (s, c, h, e) => true;

        client.Connect("localhost", 25, false);

        // Note: since we don't have an OAuth2 token, disable
        // the XOAUTH2 authentication mechanism.
        client.AuthenticationMechanisms.Remove("XOAUTH2");

        // Note: only needed if the SMTP server requires authentication
        //client.Authenticate("joey", "password");

        client.Send(message);
        client.Disconnect(true);
    }
}

For those that cannot access imgur:
Domain Name: localhost
Listen Interface: 0.0.0.0
Port Number: 25 (Though, in Dalsier's case, Dalsier would use 26)
Extensions:

  • [ ] Implicit SSL/TLS
  • [x] 8BITMIME
  • [ ] STARTTLS
  • [ ] AUTH
  • [x] SIZE

SSL/TLS Certificate: None
SSL/TLS Certificate Password: None
Max Message Size (bytes): 0
Receive timeout (ms): 30000
Options:

  • [ ] Require authentication
  • [ ] Require secure connection
  • [ ] Only allow clear text authentication over secure connection
Onosa
  • 1,275
  • 1
  • 12
  • 28
  • 1
    this line made a difference: client.ServerCertificateValidationCallback = (s, c, h, e) => true; Even with all the configuration above, without this still fails. – jpgrassi May 17 '17 at 13:37
  • 1
    This worked for me, and yes what @jpgrassi said too. – Zorkind Aug 25 '18 at 17:24
0

Do you have the client.Disconnect(true);? The error message suggests that you do not.

jstedfast
  • 35,744
  • 5
  • 97
  • 110
  • 1
    Yes I do. The main problem is that smtp4dev is listening at port 26 and when the client code runs the smtp4dev stops. It seems an exception were thrown inside the smpt4dev code. – Dalsier Jan 11 '16 at 21:43
  • I am having the same issue, with the old version of smtp4dev and the .netCore one, no luck at all :-( – Zorkind Aug 25 '18 at 17:20
  • to answer your assumption @jstedfast this issue happens on the very FIRST call, so it is not a disconnect issue since the connection was never made before. – Zorkind Aug 25 '18 at 17:22