0

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

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

但是这个 ASP.net 网站也被配置为发送电子邮件以获取健康监控通知。我使用SimpleMailWebEventProvider来执行此操作,但看起来此提供程序上没有 EnableSsl 选项。

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

4

1 回答 1

0

我发现这篇文章展示了如何强制EnableSsl进入MailWebEventProvider.

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

}
于 2013-05-30T18:26:04.497 回答