Flag environment when sending email to user

Asked

Viewed 37 times

-1

Guys, I need your help. Each form that is registered in my system it sends in e-mail to some people warning that it has been registered, except that I have two environments, one of homologation and one of production. I need that 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);
        }
  • In another deleted comment you said something like going through connectionstring, I didn’t understand what you were thinking

  • @Gabrielcoletta type, do some checking pulling on connectionstring, because I have 2 environments, I have 2 connectionstring in the system, then in this case, check which one is being used, or check the url understand? i have an example here that checks Pea URL, but I’m not able to adapt it to use in my Mailservice. --- var isHomolog = Request.url.Absoluteuri.Contains("Homolog") || Request.url.Absoluteuri.Contains("localhost") ? true : false;

  • What I can recommend to you is to tag the web.config saying if it is development or production, in the case of the system Deploy it would change the web.config correctly.

  • Can you give me an example?

1 answer

1

You could flag 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 the web.Release.config would use "Produção", so when you do deploy it would transform your web.config with the right environment.

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

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        //DESENVOLVIMENTO.
    }
}
  • I will try to use here Gabriel, thank you very much

Browser other questions tagged

You are not signed in. Login or sign up in order to post.