Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recommended way to use smtp4dev as server of mailkit client messages

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?

like image 826
Dalsier Avatar asked Jan 06 '16 16:01

Dalsier


People also ask

How do I use smtp4dev?

Navigate to System administration > Setup > Email parameters and click the Configuration tab, then set Batch email provider to SMTP. Next, click the SMTP settings tab. Here we need to enter parameters for smtp4dev as follows: Set Outgoing mail server to localhost and SMTP port number to 25.

How do I send an email using MailKit?

C# send simple mail with Mailkit Net. Smtp; using MailKit. Security; using MimeKit; var host = "smtp.mailtrap.io"; var port = 2525; var username = "username"; // get from Mailtrap var password = "password"; // get from Mailtrap var message = new MimeMessage(); message.


1 Answers

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", "[email protected]"));
    message.To.Add(new MailboxAddress("Alice", "[email protected]"));
    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
like image 185
Onosa Avatar answered Jan 04 '23 15:01

Onosa