We're developing an application which uses SMTP; the host is configured in our DB and we want allow an optional port to be specified (so that SSL can be used in future).
Easiest seems to be to set port==0
when reading from the DB but I need to confirm that .NET will treat this exactly the same as not specifying a port at all?
https://msdn.microsoft.com/en-us/library/67w4as51(v=vs.110).aspx
According to the SmtpClient(string, int)
constructor documentation:
If port is zero, Port is initialized using the settings in the application or machine configuration files.
As you can see from the SmtpNetworkElement
configuration element documentation, this defaults to port 25, exactly the same as when you call the contructor with only a host
parameter.
If you don't want that, it's as simple as:
SmtpClient smtpClient;
if (yourConfiguration.SmtpPort.HasValue)
{
smtpClient = new SmtpClient(yourConfiguration.SmtpHost, yourConfiguration.SmtpPort.Value);
}
else
{
smtpClient = new SmtpClient(yourConfiguration.SmtpHost);
}