I found this article that show how to force EnableSsl into a MailWebEventProvider.
I modified it so it use the SimpleMailWebEventProvider instead of the TemplatedMailWebEventProvider and use my AppSettings from the web.config to toggle the EnableSsl setting without changing the code, so the administrator will be able to switch it at will.
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"));
}
}