tags:

views:

34

answers:

2

I'm using C#, and while working on a sending mail routine using the tools available on the .NET Framework and I've sumbled upon two situations

  • One mail server that requires SSL
  • One mail server that doesn't support SSL

So when using a generic mail sending tool, I've noticed that I should ask the user if SSL should be used, or figure it by coding.

When I try to send an E-Mail not using SSL through a mail server that requires it, I get an exception. When I try to send an E-Mail using SSL through a mail server that doesnt support it, I get an exception.

By doing this, it should be easy to check wether I should use it or not.

But I want a smarter way to do this. I didn't find any way to do so. Is there one ?

+2  A: 

Personally, I do not believe that your code should be "Automagically" determining if SSL should be used.

When the e-mail service is configured by the users, they should be telling you how it works and what is needed.

You can see this behavior by the way the out of the box .NET SMTP which you must tell it how it is.

To further this, some services have a different URL for SSL and non SSL, and maybe users SHOULD be using SSL, but you could let them accidentally send the wrong way.

Mitchel Sellers
+1  A: 

It sounds like you have the answer. As long as the SMTP library you are using does the right thing when throwing the exception, try ssl first, then fallback to non-ssl, and handle that error case (i.e. total failure) if it happens.

I think you are concerned because you don't know what's going on under the covers when the exception is thrown, and if that's so, I feel the same. I don't know if your .net toolkit provides the facilities, but you should be able to make the connection with a lower-level type connection object which allows you to test whether or not it's possible to make an ssl connection and check the return value of your attempt, then decide how to proceed based on that.

It may be more trouble than it's worth though, because you might then need to bolt on SMTP functionality to your new connection type, if your SMTP library doesn't support using an already established connection. Which is the long way around saying, you're probably doing well enough with catching the exception and trying again.

In reference to @Mitchel's answer, I'd say it's perfectly okay if it's automagic as long as the right notification is made, and of course the context is right. SMTP is still pretty often plain text, so any expectation of encryption for email is pretty low. Login credentials are another matter. If this is an app an end user would use, you certainly ought to make sure they're connecting over SSL if credentials will be exchanged.

Aaron H.