如何在 Web.config 中为 SmtpClient 启用 SSL

发布于 2024-07-12 22:29:00 字数 110 浏览 4 评论 0原文

有没有办法从 web.config 设置 EnableSSL?

我可以在代码中设置此属性,但这对于简单邮件 Web 事件和使用默认 Smtp 服务器的其他类不起作用。 有任何想法吗?

Is there a way to set the EnableSSL from the web.config?

I could set this property in code, but that wouldn't work for the Simple Mail Web Event and other classes that uses the default Smtp Server. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

李不 2024-07-19 22:29:00

对于 .NET 3 及更早版本:不能。 你必须手动管理它。

有关详细信息,您可以参阅 https://blogs.msdn.microsoft.com/vikas/2008/04/29/bug-asp-net-2-0-密码恢复-Web-控制-无法将电子邮件发送到-ssl-启用-smtp-服务器/

对于.NET 4:可以。

请参阅 http://theoldsewingfactory.com/2011 /01/06/enable-ssl-in-web-config-for-smtpclient/

<configuration>
    <system.net>
        <mailSettings>
            <smtp deliveryMethod=”network”>
                <network host="localhost"
                         port="25"
                         enableSsl="true"
                         defaultCredentials="true" />
            </smtp>
        </mailSettings>
    </system.net>
</configuration>

For .NET 3 and earlier: You can't. You have to manage it by hand.

For more information you can see https://blogs.msdn.microsoft.com/vikas/2008/04/29/bug-asp-net-2-0-passwordrecovery-web-control-cannot-send-emails-to-ssl-enabled-smtp-servers/.

For .NET 4: You can.

See http://theoldsewingfactory.com/2011/01/06/enable-ssl-in-web-config-for-smtpclient/

<configuration>
    <system.net>
        <mailSettings>
            <smtp deliveryMethod=”network”>
                <network host="localhost"
                         port="25"
                         enableSsl="true"
                         defaultCredentials="true" />
            </smtp>
        </mailSettings>
    </system.net>
</configuration>
盛夏尉蓝 2024-07-19 22:29:00

我有一个肮脏的解决方法(直到 .NET 4.0 出现)。 它不是更改我的代码,而是依赖使用的端口来确定是否需要 SSL。

var client = new SmtpClient();
client.EnableSsl = client.Port == 587 || client.Port == 465;
// This could also work
//client.EnableSsl = client.Port != 25;

我说这是一个肮脏的黑客行为,但它对于我们遇到的不同配置运行良好。

I have a dirty workaround (until .NET 4.0 comes out). Instead of changin my code it relies on the used port to determine if SSL is required or not.

var client = new SmtpClient();
client.EnableSsl = client.Port == 587 || client.Port == 465;
// This could also work
//client.EnableSsl = client.Port != 25;

I said it was a dirty hack, but it working fine for the different configurations that we encounter.

玩物 2024-07-19 22:29:00

这在 .net 4

E.G. 中对我有用 在 web.config 中

network host="somesmtpserver" userName="[email protected]" 
password="whatever" port="25" enableSsl="true"         

this works for me in .net 4

E.G. in web.config

network host="somesmtpserver" userName="[email protected]" 
password="whatever" port="25" enableSsl="true"         
夜司空 2024-07-19 22:29:00

Giles Roberts Jan 18 '12 at 18:01 说

这对我在 .net 4

E.G. 中有效 web.config 中的

network host="somesmtpserver" userName="[email protected]" 
password="whatever" port="25" enableSsl="true" 

端口 25 不是 SSL 端口。 端口 25 是默认 SMTP 端口。 此外,web.config 代码已部分填写。 代码应该是

    <system.net>
         <mailSettings>
              <smtp deliveryMethod="Network" from="[email protected]">
                     <network host="smtp.gmail.com"
                     userName="[email protected]"
                     password="********"
                     port="587"
                     defaultCredentials="true"
                     enableSsl="true" />
             </smtp>
        </mailSettings>
   </system.net>

上面的设置比原始的 web.config 代码更准确。 我不知道巫婆的方法更好。 使用 web.config 或使用代码隐藏页面发送电子邮件。 无论使用哪种方法,都必须修改代码隐藏文件。 我这样说是因为您必须连接发件人、主题和正文文本框。 我想当然地认为最终结果是你想通过aspx网页发送消息

Giles Roberts Jan 18 '12 at 18:01 said

this works for me in .net 4

E.G. in web.config

network host="somesmtpserver" userName="[email protected]" 
password="whatever" port="25" enableSsl="true" 

Port 25 is not a SSL port. Port 25 is the default SMTP port. Furthermore the web.config code is partly filled out. The code should be

    <system.net>
         <mailSettings>
              <smtp deliveryMethod="Network" from="[email protected]">
                     <network host="smtp.gmail.com"
                     userName="[email protected]"
                     password="********"
                     port="587"
                     defaultCredentials="true"
                     enableSsl="true" />
             </smtp>
        </mailSettings>
   </system.net>

This settings above is more accurate then the original web.config code. I don't know witch method is better. Using web.config or using the code-behind page to send the e-mail. No matter witch method you use the code-behind file has to be modified. I say this because you have to wire up From, Subject, and Body text boxes. I'm taking it for granted that the end results that you want to send a message through an aspx web page

奶茶白久 2024-07-19 22:29:00

啊,有一种方法可以解决 .net 登录控件中内置的“忘记密码”问题。

请参阅 http://blogs.msdn.com/vikas/archive/2008/04/29/bug-asp-net-2-0-passwordrecovery-web -control-cannot-send-emails-to-ssl-enabled-smtp-servers.aspx

Ryan

Ah, there is a way to do it for the 'forgot password' built in .net login controls though.

See http://blogs.msdn.com/vikas/archive/2008/04/29/bug-asp-net-2-0-passwordrecovery-web-control-cannot-send-emails-to-ssl-enabled-smtp-servers.aspx

Ryan

浸婚纱 2024-07-19 22:29:00

这是我在 web.config 文件中使用的代码。在此处输入图像描述

This is the code I use in my web.config file.enter image description here

臻嫒无言 2024-07-19 22:29:00

我似乎班级已密封,所以我进行了手动扩展。 我想我会在这里为其他人提供它。 希望它对其他人有用。

/// <summary>
/// OldSchool extension of SmtpNetWorkElement, since it's sealed.
/// </summary>
public class SmtpNetworkElementEx
{
    private readonly SmtpNetworkElement m_SmtpNetWorkElement;

    /// <summary>
    /// Initializes a new instance of the <see cref="SmtpNetworkElementEx"/> class.
    /// </summary>
    public SmtpNetworkElementEx()
    {
        Configuration configurationFile = WebConfigurationManager.OpenWebConfiguration("~/web.config");
        var mailSettings = configurationFile.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;

        if (mailSettings == null)
            return;

        m_SmtpNetWorkElement = mailSettings.Smtp.Network;
    }

    public string Host { get { return m_SmtpNetWorkElement.Host; } }
    public bool DefaultCredentials { get { return m_SmtpNetWorkElement.DefaultCredentials; } }
    public string ClientDomain { get { return m_SmtpNetWorkElement.ClientDomain; } }
    public string TargetName { get { return m_SmtpNetWorkElement.TargetName; } }
    public int Port { get { return m_SmtpNetWorkElement.Port; } }
    public string UserName { get { return m_SmtpNetWorkElement.UserName; } }
    public string Password { get { return m_SmtpNetWorkElement.Password; } }
    public bool EnableSsl { get { return Convert.ToBoolean(m_SmtpNetWorkElement.ElementInformation.Properties["enableSsl"].Value); } }
}

使用这种方式:

var smtpSettings = new SmtpNetworkElementEx();

_smtpClient.Host = smtpSettings.Host;
_smtpClient.Port = smtpSettings.Port;
_smtpClient.EnableSsl = smtpSettings.EnableSsl;
_smtpClient.Credentials = new System.Net.NetworkCredential(smtpSettings.UserName, smtpSettings.Password);

I appears the class is sealed, so i made a manual extension. I thought i'd provide it for others here. Hope it can be of use to others.

/// <summary>
/// OldSchool extension of SmtpNetWorkElement, since it's sealed.
/// </summary>
public class SmtpNetworkElementEx
{
    private readonly SmtpNetworkElement m_SmtpNetWorkElement;

    /// <summary>
    /// Initializes a new instance of the <see cref="SmtpNetworkElementEx"/> class.
    /// </summary>
    public SmtpNetworkElementEx()
    {
        Configuration configurationFile = WebConfigurationManager.OpenWebConfiguration("~/web.config");
        var mailSettings = configurationFile.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;

        if (mailSettings == null)
            return;

        m_SmtpNetWorkElement = mailSettings.Smtp.Network;
    }

    public string Host { get { return m_SmtpNetWorkElement.Host; } }
    public bool DefaultCredentials { get { return m_SmtpNetWorkElement.DefaultCredentials; } }
    public string ClientDomain { get { return m_SmtpNetWorkElement.ClientDomain; } }
    public string TargetName { get { return m_SmtpNetWorkElement.TargetName; } }
    public int Port { get { return m_SmtpNetWorkElement.Port; } }
    public string UserName { get { return m_SmtpNetWorkElement.UserName; } }
    public string Password { get { return m_SmtpNetWorkElement.Password; } }
    public bool EnableSsl { get { return Convert.ToBoolean(m_SmtpNetWorkElement.ElementInformation.Properties["enableSsl"].Value); } }
}

Use this way:

var smtpSettings = new SmtpNetworkElementEx();

_smtpClient.Host = smtpSettings.Host;
_smtpClient.Port = smtpSettings.Port;
_smtpClient.EnableSsl = smtpSettings.EnableSsl;
_smtpClient.Credentials = new System.Net.NetworkCredential(smtpSettings.UserName, smtpSettings.Password);
苏大泽ㄣ 2024-07-19 22:29:00

我几乎到处寻找这个。

但似乎我们无法在 web.config 中配置 EnableSsl 属性。

看看这个

I have searched almost everywhere for this.

But it seems there is no way we can configure EnableSsl Property in web.config.

Have a look at this

彡翼 2024-07-19 22:29:00

我认为 MailSettingsSectionGroup 中存在错误。 请参阅下面的代码:

Configuration configurationFile = WebConfigurationManager.OpenWebConfiguration("~/web.config");
var mailSettings = configurationFile.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;

_smtpClient.Host = mailSettings.Smtp.Network.Host;
_smtpClient.Port = mailSettings.Smtp.Network.Port;
_smtpClient.EnableSsl = mailSettings.Smtp.Network.**EnableSsl**;
_smtpClient.Credentials = new System.Net.NetworkCredential(mailSettings.Smtp.Network.UserName, mailSettings.Smtp.Network.Password);
_smtpClient.UseDefaultCredentials = false;

似乎 EnableSsl 不作为 Network 下的属性存在,因为当我运行和调试它时,我可以看到该值,但由于缺少 ExtensionMethod 而无法编译代码。

I think there's a bug in the MailSettingsSectionGroup. See below code:

Configuration configurationFile = WebConfigurationManager.OpenWebConfiguration("~/web.config");
var mailSettings = configurationFile.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;

_smtpClient.Host = mailSettings.Smtp.Network.Host;
_smtpClient.Port = mailSettings.Smtp.Network.Port;
_smtpClient.EnableSsl = mailSettings.Smtp.Network.**EnableSsl**;
_smtpClient.Credentials = new System.Net.NetworkCredential(mailSettings.Smtp.Network.UserName, mailSettings.Smtp.Network.Password);
_smtpClient.UseDefaultCredentials = false;

It seems that EnableSsl does not exist as a property under Network because when I run and debug this, I can see the value, but can't compile the code due to missing ExtensionMethod.

不知所踪 2024-07-19 22:29:00

只需扩展该类并设置 EnableSsl = true 并使用该类即可。

Just extend the class and set EnableSsl = true and use that class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文