如何启用SWS健康监控通知电子邮件?

时间:2013-05-30 16:03:02

标签: asp.net web-config asp.net-3.5 smtpclient health-monitoring

我们正在将电子邮件更改为Office 365,当我从.NET发送电子邮件时,这会强制我使用EnableSSL。由于我们的Intranet Web应用程序在.net 3.5下运行,看起来我不能enable SSL for SmtpClient in Web.config。所以我做了这样的事情:

Dim Smtp As New System.Net.Mail.SmtpClient
Smtp.EnableSsl = Boolean.Parse(System.Configuration.ConfigurationManager.AppSettings("SmtpEnableSsl"))
Smtp.Send(eMail)

但是这个ASP.net网站也配置为Send E-mail for Health Monitoring Notifications。我使用SimpleMailWebEventProvider来执行此操作,但看起来此提供程序上没有EnableSsl选项。

我该怎么做才能解决这个问题?

1 个答案:

答案 0 :(得分:0)

我发现this article显示了如何强制EnableSsl加入MailWebEventProvider

我对其进行了修改,因此使用SimpleMailWebEventProvider代替TemplatedMailWebEventProvider并使用AppSettings中的web.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"));
    }

}