简体   繁体   English

Asp.net电子邮件发送已发布但未开发

[英]Asp.net email sending on publish but not development

I am developing an application with asp.net mvc. 我正在使用asp.net mvc开发应用程序。 And I need to send email when an action populated. 我需要在执行操作时发送电子邮件。

public class MyController : Controller{

   public ActionResult Create(string name){

        // create and save to database

        // if success 
        // select admin emails from database.
        // create email content and subject.
        // send email to admins.
   }
}

But I want to automatically activate sending mechanism after published my project. 但是我想在发布项目后自动激活发送机制。 In development, I do not need to send email on Create action. 在开发中,我不需要发送有关“创建操作”的电子邮件。

Is there any setting to do this? 有什么设置吗?

You can use the HttpRequest.IsLocal Property. 您可以使用HttpRequest.IsLocal属性。

public class MyController : Controller
{
   public ActionResult Create(string name)
   {
       if (HttpContext.Current.Request.IsLocal) { ... }
       else { ... }
   }
}

To handle those kind of scenarios you should use Dependency Injection (I personally used it in 99.9% of the projects because this is the only way you can unit test it). 要处理这种情况,您应该使用依赖注入(我个人在99.9%的项目中都使用了依赖注入,因为这是可以对其进行单元测试的唯一方法)。 Dependency Injection library ( Autofac , Ninject , Castle Windsor , Simple Injector and others) allows you to resolve dependencies in run time base on some configuration. 依赖注入库( AutofacNinjectCastle WindsorSimple Injector等)允许您基于某些配置在运行时解析依赖。 For example you you have a communication service that is responsible to send emails: 例如,您有一个负责发送电子邮件的通讯服务:

public interface ICommuniucationService
{
    void SendEmail(....);
}

public class CommunicationService : ICommuniucationService
{
    public void SendEmail(...)
    {
        //real implementation of sending email
    }
}

public class FakeCommunicationService : ICommuniucationService
{
    public void SendEmail(...)
    { 
       //do nothing.
       return;
    }
}

My controller is going to have a private property of type ICommuniucationService that is going to be instantiated by the dependency injection library via constructor injection: 我的控制器将具有ICommuniucationService类型的私有属性,该属性将由依赖项注入库通过构造函数注入实例化:

public class MyController : Controller{

   //this will be resolved in runtime(either CommuniucationService or FakeCommunicationService )
   private readonly ICommuniucationService EmailSvc;

   // Use constructor injection for the dependencies
   public MyController(ICommuniucationService svc) {
       this.EmailSvc= svc;       
   }

   public ActionResult Create(string name){

        // create and save to database

        // if success 
        // select admin emails from database.
        // create email content and subject.

        this.EmailSvc.SendEmail(...)

        //The action code doesn't change neither for 
   }
}

When configuring dependency injection container you could do something like this ( Simple injector example similar to this ): 在配置依赖项注入容器时,您可以执行以下操作(类似于以下示例的简单注入器示例 ):

protected void Application_Start(object sender, EventArgs e) {       
    var container = new Container();  
#if DEBUG
   container.Register<ICommuniucationService, FakeCommuniucationService>(Lifestyle.Singleton); 
#else
   container.Register<ICommuniucationService, CommuniucationService>(Lifestyle.Singleton); 
#endif         

    container.Verify();
    DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));
}

So with this configuration when your project runs with DEBUG mode you are registering FakeCommuniucationService which doesn't send emails, while when you run in RELEASE mode (that you should use when you publish your application) real CommuniucationService is registered 因此,使用此配置,当您的项目以DEBUG模式运行时,您将注册不发送电子邮件的FakeCommuniucationService ;而当您以RELEASE模式运行时(发布应用程序时应使用该模式),则注册了真正的CommuniucationService

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM