检查邮件服务器是否使用SSL而不尝试发送电子邮件

时间:2021-03-11 18:15:36

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

我正在使用C#,在使用.NET Framework上提供的工具处理发送邮件例程时我已经将两种情况相混淆

  • One mail server that requires SSL
  • 一台需要SSL的邮件服务器
  • One mail server that doesn't support SSL
  • 一个不支持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.

因此,当使用通用邮件发送工具时,我注意到我应该询问用户是否应该使用SSL,或者通过编码来计算。

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.

当我尝试通过需要它的邮件服务器发送不使用SSL的电子邮件时,我得到一个例外。当我尝试通过不支持它的邮件服务器使用SSL发送电子邮件时,我得到一个例外。

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 ?

但我想要一个更聪明的方法来做到这一点。我没有找到任何办法这样做。有吗?

3 个解决方案

#1


1  

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.

听起来你有答案。只要您使用的SMTP库在抛出异常时做正确的事情,首先尝试ssl,然后回退到非ssl,并处理该错误情况(即完全失败)(如果发生)。

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.

我想你很担心,因为当抛出异常时你不知道幕后发生了什么,如果是这样的话,我也会有同感。我不知道您的.net工具包是否提供了设施,但您应该能够与较低级别的类型连接对象建立连接,从而可以测试是否可以建立ssl连接并检查返回你尝试的价值,然后决定如何继续进行。

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.

它可能比它的价值更麻烦,因为如果您的SMTP库不支持使用已建立的连接,那么您可能需要将SMTP功能固定到新的连接类型。这是一个很长的路要走,你可能做得很好,抓住异常并再次尝试。

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.

在提到@Mitchel的答案时,我会说如果它是自动的,只要做出正确的通知,当然上下文是正确的,那就完全可以了。 SMTP仍然通常是纯文本,因此对电子邮件加密的任何期望都非常低。登录凭据是另一回事。如果这是最终用户将使用的应用程序,您当然应该确保他们通过SSL连接,如果将交换凭据。

#2


2  

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

就个人而言,我不认为您的代码应该“自动”确定是否应该使用SSL。

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.

你可以通过开箱即用的.NET SMTP看到这种行为,你必须告诉它它是怎么回事。

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.

为了进一步说明,一些服务具有不同的SSL和非SSL URL,并且用户应该使用SSL,但是你可能会让他们意外地发送错误的方式。

#3


2  

bool supportsSSL = true;
Stream stream = new SslStream(client.GetStream(), false);

try
{
   ((SslStream)stream).AuthenticateAsClient("pop3.xyz.com");
}
catch (Exception e)
{
   supportsSSL =false;
}

#1


1  

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.

听起来你有答案。只要您使用的SMTP库在抛出异常时做正确的事情,首先尝试ssl,然后回退到非ssl,并处理该错误情况(即完全失败)(如果发生)。

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.

我想你很担心,因为当抛出异常时你不知道幕后发生了什么,如果是这样的话,我也会有同感。我不知道您的.net工具包是否提供了设施,但您应该能够与较低级别的类型连接对象建立连接,从而可以测试是否可以建立ssl连接并检查返回你尝试的价值,然后决定如何继续进行。

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.

它可能比它的价值更麻烦,因为如果您的SMTP库不支持使用已建立的连接,那么您可能需要将SMTP功能固定到新的连接类型。这是一个很长的路要走,你可能做得很好,抓住异常并再次尝试。

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.

在提到@Mitchel的答案时,我会说如果它是自动的,只要做出正确的通知,当然上下文是正确的,那就完全可以了。 SMTP仍然通常是纯文本,因此对电子邮件加密的任何期望都非常低。登录凭据是另一回事。如果这是最终用户将使用的应用程序,您当然应该确保他们通过SSL连接,如果将交换凭据。

#2


2  

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

就个人而言,我不认为您的代码应该“自动”确定是否应该使用SSL。

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.

你可以通过开箱即用的.NET SMTP看到这种行为,你必须告诉它它是怎么回事。

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.

为了进一步说明,一些服务具有不同的SSL和非SSL URL,并且用户应该使用SSL,但是你可能会让他们意外地发送错误的方式。

#3


2  

bool supportsSSL = true;
Stream stream = new SslStream(client.GetStream(), false);

try
{
   ((SslStream)stream).AuthenticateAsClient("pop3.xyz.com");
}
catch (Exception e)
{
   supportsSSL =false;
}