web.config から EnableSSL を設定する方法はありますか?
このプロパティをコードで設定することもできますが、Simple Mail Web Event や、既定の Smtp サーバーを使用するその他のクラスでは機能しません。何か案は?
web.config から EnableSSL を設定する方法はありますか?
このプロパティをコードで設定することもできますが、Simple Mail Web Event や、既定の Smtp サーバーを使用するその他のクラスでは機能しません。何か案は?
.NET 3 以前の場合: できません。手作業で管理する必要があります。
.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>
汚い回避策があります(.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;
汚いハックだと言いましたが、遭遇するさまざまな構成では問題なく機能します。
これは.net 4で機能します
web.config の EG
network host="somesmtpserver" userName="do_not_reply@yourserver.com"
password="whatever" port="25" enableSsl="true"
ああ、.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
ライアン
にバグがあると思います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
を実行してデバッグすると、値は表示されますが、ExtensionMethod がないためにコードをコンパイルできないため、Network の下にプロパティとして存在しないようです。
クラスが封印されているようですので、手動で拡張しました。ここで他の人に提供すると思いました。それが他の人に役立つことを願っています。
/// <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);
クラスを拡張して EnableSsl = true を設定し、そのクラスを使用するだけです。