0

I need to parse html code into string, because i'm later using it as my body content of email message:

Is there a way to parse html code like this:

<div class="alert alert-success" role="alert">
    <h4 class="alert-heading">Well done!</h4>
    <p>You have successfully subscribed!</p>
    <hr>
    <p class="mb-0">We will be sending you newsletter on weekly basis.</p>
</div>

into string in a clean way, without doing it like this:

string body = @"<div class=""alert alert-success"" role=""alert"">" + "</div>"

so i could pass it to mail like this:

MailMessage mailMessage = new MailMessage();
mailMessage.Body = body;

instead of having a whole series of html code inside string there.

Is there maybe any other "way" to make "design" for email messages?

PS: i also do not want to load html from external file

I would like to use html code to have a decent design for subscribing to newsletter message on email.

Thank you!

aiden87
  • 929
  • 8
  • 25
  • 52
  • Since you want it in a E-Mail, why dont you allow html content in your E-Mail? https://stackoverflow.com/questions/8628683/how-to-send-html-formatted-email – Rand Random Sep 14 '17 at 14:05
  • Not totally clear what you are asking here. How about making an html file as an embedded resource in your project? Or do you want the html to be input by the user? – stephen.vakil Sep 14 '17 at 14:12
  • i want my html code to be the display of how mail looks like. for example if id put a button in it, uses who receives email could actually click on that button and get redirected somwhere @stephen.vakil – aiden87 Sep 14 '17 at 14:24

2 Answers2

1

Since you are using C#, you can use String interpolation. http://www.informit.com/articles/article.aspx?p=2422807

You can create a template object were you can pass all the necessary variables for your message.

e.g

class WelcomeEmail {
     public WelcomeEmail(String message, String title) {

     }
     public override string ToString() {

     }
}
johndavedecano
  • 522
  • 5
  • 11
1

/* If they are static and don't need to be read from a file, as you've indicated, I would create a project->property->resource string for each message. You can paste the email body into a string variable and then just access it like this: */

string body = MyNamespace.Properties.Resources.emailBody1;

/* the string will be formatted with all the quotes and \r\n just as it was from what you pasted in. */

// you could even add some substitution to customize the email

body.Replace("{EmailRecipient}", strEmailRecipient);

wdomains
  • 86
  • 5