Flag environment when sending email to user

-1

Personal I need your help. Every form that is registered in my system it sends in e-mail to some people warning that it was registered, only that I have two environments, one of homologation and one of production. I need when I send an email, it signals the place that was registered

public void NovoEventoAdverso(int patientId, int hospitalId, int usuarioId)
        {
            var cadastroRepository = new CadastroRepository();
            var investigadorRepository = new UsuarioRepository();
            var randomizacaoRepository = new RandomizacaoRepository();

            var paciente = cadastroRepository.GetByPatientId(patientId, hospitalId);
            var investigador = investigadorRepository.GetbyId(usuarioId);
            var randomizacao = randomizacaoRepository.GetByPatientId(patientId, hospitalId);

            MailMessage objEmail = FactoryMailMessage();

            objEmail.To.Add("Nome Teste <[email protected]>");

            objEmail.Subject = $"Novo Evento Adverso - {paciente.inpac}";

            var conteudo = "Novo Evento Adverso cadastrado:<br />";
            conteudo += $"Iniciais do Paciente: {paciente.inpac}<br />";
            conteudo += $"Nome do Investigador: {investigador.Nome}<br />";
            conteudo += $"Data da Randomização: {randomizacao.RandomizacaoData.Value.ToString("dd/MM/yyyy")}<br />";
            conteudo += $"Data do Preenchimento: {DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss")}";

            objEmail.Body = conteudo;

            EnviarEmail(objEmail);
        }

private static void EnviarEmail(MailMessage objEmail)
        {
            var objSmtp = new SmtpClient
            {
                Host = "smtp.mandrillapp.com",
                EnableSsl = true,
                Port = 587
            };

            const string user = "####@#####.com.br";
            const string senha = "######";

            objSmtp.Credentials = new NetworkCredential(user, senha);
            objSmtp.Send(objEmail);
        }
    
asked by anonymous 19.01.2018 / 12:33

1 answer

1

You could put a flag in your web.config, for example:

<appSettings>
    <add key="ambiente" value="Produção" />
</appSettings>

And to access it, you would use:

var ambiente = ConfigurationManager.AppSettings["ambiente"];

In the case of web.Debug.config you would use as "Desenvolvimento" , and in web.Release.config would use "Produção" , so when you do deploy it would make your web.config with the correct environment.

If the environment is Asp.Net Core, the API itself has a flag indicating whether you are developing:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        //DESENVOLVIMENTO.
    }
}
    
19.01.2018 / 15:22