2013-05-30 103 views
0

我們正在將我們的電子郵件更改爲Office 365,當我從.NET發送電子郵件時,強制使用EnableSSL。由於我們的Intranet Web應用程序運行在.net 3.5下,它看起來像我不能enable SSL for SmtpClient in Web.config。所以我這樣做:如何爲健康監測通知電子郵件啓用SSL?

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在此提供程序上。

我應該怎麼做才能解決這個問題?

回答

0

我發現this article顯示如何強制EnableSsl進入MailWebEventProvider

我修改了它,所以它使用SimpleMailWebEventProvider,而不是TemplatedMailWebEventProvider,用我AppSettingsweb.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")); 
    } 

}