1

Using asp.net core 3.1 I can send email using this code:

Code snippet 1 using System.Net.Mail.SmtpClient

using (System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient())
{
    smtp.Host = "mail.my-real-domain-name.com";                    
    smtp.EnableSsl = false;
    NetworkCredential credential = new NetworkCredential(emailModel.From, 
    emailModel.Password);
    smtp.UseDefaultCredentials = false;
    smtp.Credentials = credential;
    smtp.Send(mm);
}

but when I try to send email using MailKit it doesn't connect to mailserver

Code snippet 2 in MailKit documentation

using (var client = new MailKit.Net.Smtp.SmtpClient())
{              
    client.Connect("mail.my-real-domain-name.com",0,false);
    client.Authenticate(emailModel.From, emailModel.Password);
    client.Send(message);
    client.Disconnect(true);
}

What's the equal of Code snippet 1 using MailKit?

None of below lines worked so I want to know how to send email without specifying port number using MailKit. It doesn't throw any error, just doesn't connect, I mean it stays in client.Connect line.

// client.Connect("mail.my-real-domain-name.com", 465);
// client.Connect("mail.my-real-domain-name.com", 465, true);
// client.Connect("mail.my-real-domain-name.com", 25, false);
// client.Connect("mail.my-real-domain-name.com", 2525, false);
// client.Connect("mail.my-real-domain-name.com", 587,MailKit.Security.SecureSocketOptions.StartTls);

MailKit

Nishan
  • 3,644
  • 1
  • 32
  • 41
  • Does your `my-real-domain-name.com` mail server allow SSL connections? – Nishan Jan 26 '22 at 13:46
  • Why do you care so much about whether you specify a port number or not? There's no built in logic to scan all the ports and figure out which one it should connect to. Instead, when you don't specify a port number, it's going to assume a default value. I haven't worked with MailKit much, but if requires a port number, it really shouldn't be a big deal. Just figure out what port your SMTP server listens on, figure out whether it uses SSL, and figure out how to authenticate to it. That's all you need. The default SMTP port is usually 25. – mason Jan 26 '22 at 13:57
  • @Nishan, I checked Mail section in plesk and it says "To send messages via SMTP securely, use port 465.". but I think there's something wrong with mail server. – Amaryllis Lavender Jan 26 '22 at 13:58
  • @mason, because when I don't specify port number it works with System.Net.Mail, but I want to do the same with MailKit. also as I said I test is with port 25 and it didn't connect. – Amaryllis Lavender Jan 26 '22 at 14:00
  • 1
    @AmaryllisLavender `client.Connect("mail.my-real-domain-name.com",0,true);` should work, because when you say `0` it takes the default value. If it still doesn't work I think the mail server is blocking your connection. Check this for all overloads http://www.mimekit.net/docs/html/Overload_MailKit_Net_Smtp_SmtpClient_Connect.htm – Nishan Jan 26 '22 at 14:01
  • @AmaryllisLavender You're making assumptions about how it works without truly understanding. The key of the built in SmtpClient isn't that you aren't specifying a port number. It's something else. You could verify: try specifying port 25 for the built in SmtpClient and you'll see that works too. Specification of a port vs lack of providing one isn't the problem here, so long as you're connecting to the correct port. – mason Jan 26 '22 at 14:04
  • @AmaryllisLavender Following filddle works with Gmail smtp. https://dotnetfiddle.net/LEk8QU, also lokk at these issues too (if you're using gmail). https://stackoverflow.com/a/25238515/5260872 – Nishan Jan 26 '22 at 14:06
  • @nishan no I don't work with Gmail smtp, but I test it before with Gmail smtp and both mailkit and system.net.mail working fine with Gmail smtp – Amaryllis Lavender Jan 26 '22 at 14:09
  • If you know that, then why are you specifically asking in your question "I want to know how to send email without specifying port number using MailKit"? You're asking about a specific solution you think might resolve it, instead of asking the proper thing which is "why can't I connect to my mail server?". That's a classic [XY Problem](https://xyproblem.info/). Granted in this case, it's not terribly difficult to discern what you really want, but you should be explicit about it. I suggest you edit your post to ask the proper question. – mason Jan 26 '22 at 14:15
  • @ason I don't understand you, I want to know how don't specify port when connect using mailkit and Nishan answerd me in comment, client.Connect("mail.my-real-domain-name.com",0,true); should work, so I think there's something wrong with my mail server – Amaryllis Lavender Jan 26 '22 at 14:23
  • My point is that you truly don't want to know how to not specify a port. What you *really* want to know is how to connect to the mail server successfully. And that you should focus your post on asking that instead. Do you understand the difference between those two questions? It's the same as if I took my car to a mechanic and asked why my car uses synthetic fuel when what I really want is to know why my car isn't working. Ask about the problem, don't ask about your theory for fixing it. It's an important distinction that you need to make for future questions. – mason Jan 26 '22 at 15:04
  • 1
    The reason you tave to specify a port is that each major server handles things differently. The default port for SMTP in general is 25, and so that's what you should expect to happen if you don't put a value; but port 25 is blocked virtually everywhere on the public internet because of spam. Port 465 was an unofficial hack which however lingers on in many places; the modern default for SMTP submission is 587, but some sites don't support it, and you still have to know what the server requires by way of encryption and authentication, so the port number is a very minor detail in the whole mess. – tripleee Jan 26 '22 at 17:05
  • Yes, you absolutely want to specify the port and encryption method. Some networks (clouds) block access to port 25 which is what you might be experiencing. Use SSL if available otherwise use STARTTLS. Not all mail providers support both, some only support STARTTLS. Your goal of not caring is a security risk. Always specify the configuration. – John Hanley Feb 01 '22 at 06:25

1 Answers1

1

If you take a look at the SmtpClient.cs reference source, in the Initialize() method:

So to get the same behavior as System.Net.Mail, use port 25.

MailKit will also accept port = 0 in the Connect() call and, depending on what the SecureSocketOptions argument is, it will choose an appropriate default port in the ComputeDefaultValues() method: https://github.com/jstedfast/MailKit/blob/master/MailKit/Net/Smtp/SmtpClient.cs#L1253

If the port == 0, then if useSsl=true or socketOptions=SslOnConnect, it will choose port 465, otherwise it will choose port 25.

jstedfast
  • 35,744
  • 5
  • 97
  • 110