-3

I would like for C# to detect the variables in my mailbody and replace them with the values each of them already has in my method. I'm saving my mails in a TEXT datatype in mysql This is what i save into my Text:

<html>
    <h1>
        <span style='text-decoration: underline;'>
            <strong>Solicitud</strong>
        </span>
    </h1>
    <br/>" + "<body>Blabla" + user.Nombre + " " + user.Apellido + " p<br/>La razón a que: <br />"+ razon+ ".</body>
</html>

I want it to replace user.Nombre user.Apellido and razon which are variables already available in the method

For example if my method has:

  • user.Nombre = "MATT";
  • user.Apellido ="CASA";
  • razon = "why not?";

Then detecting the variables i would have:

    <html>
    <h1>
        <span style='text-decoration: underline;'>
            <strong>Solicitud</strong>
        </span>
    </h1>
    <br/>" + "<body>Blabla" + "MATT"+ " " + "CASA" + " p<br/>La razón a que: <br />"+ "Why not?"+ ".</body>
</html>

The problem is making C# detect that the string already has variables inside of it.

Rodrigo Chapeta
  • 119
  • 1
  • 10

2 Answers2

1

If I understand your question correctly, which is pretty hard given then English you have used.

You want to replace a parts of a string you retrieved from a database?

You can perform string replacements like the below example.

string email = emailAsString.Replace("+ user.Nombre +", variable1);

You can read more on this here, if you so wish.

String Replace - https://www.dotnetperls.com/replace

Dan Cundy
  • 2,649
  • 2
  • 38
  • 65
  • Sory, now i have edited it, my problem is making C# detect that the string already has code inside of it , not a block of characters – Rodrigo Chapeta Nov 09 '17 at 16:59
0

Well searched far and wide in the web, there is a method to compile manually but its highly dangerous so I decided to use String.Format , and the templates of my mails swap the "+ variable +" to {#} Interpolating a string stored in a database

After retrieving use: var formatedString= String.Format(string , variables);

Rodrigo Chapeta
  • 119
  • 1
  • 10