我发现这篇文章展示了如何强制EnableSsl
进入MailWebEventProvider
.
我对其进行了修改,因此它使用SimpleMailWebEventProvider
代替TemplatedMailWebEventProvider
并使用 my AppSettings
fromweb.config
来切换EnableSsl
设置而不更改代码,因此管理员将能够随意切换它。
using System;
using System.Collections.Specialized;
using System.Reflection;
using System.Web.Management;
using System.Net.Mail;
using System.Configuration;
/// <summary>
/// Summary description for SimpleMailWithSslWebEventProvider
/// </summary>
public class SimpleMailWithSslWebEventProvider : WebEventProvider
{
private SimpleMailWebEventProvider _SimpleProvider;
public SimpleMailWithSslWebEventProvider()
{
ConstructorInfo constructor = typeof(SimpleMailWebEventProvider)
.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic,
null, new Type[0], null);
_SimpleProvider = (SimpleMailWebEventProvider)constructor
.Invoke(null);
}
public override void Initialize(string name, NameValueCollection config)
{
if (config == null)
{
throw new ArgumentNullException("config");
}
_SimpleProvider.Initialize(name, config);
FieldInfo field = typeof(MailWebEventProvider)
.GetField("_smtpClient",
BindingFlags.Instance | BindingFlags.NonPublic);
field.SetValue(_SimpleProvider, new SmtpClientWithSsl());
}
public override void Flush()
{
_SimpleProvider.Flush();
}
public override void ProcessEvent(WebBaseEvent raisedEvent)
{
_SimpleProvider.ProcessEvent(raisedEvent);
}
public override void Shutdown()
{
_SimpleProvider.Shutdown();
}
}
public class SmtpClientWithSsl : SmtpClient {
public SmtpClientWithSsl() {
base.EnableSsl = Boolean.Parse(ConfigurationManager.AppSettings.Get("SmtpEnableSsl"));
}
}