1

Good Day.

I want to send an email from my code file in ASP.NET. I want to apply styling to my html...How do I do it?

Here is the html part:

var Message = string.Format("<html><head></head><body><div style=""this does not work!!"">{0}<br/>{1}<br />{2}<br />{3}<br />{4}<br />{5}</body></html>", emaildetail.Name, emaildetail.Surname, emaildetail.CardProvider, emaildetail.CardType, emaildetail.CardNo, emaildetail.SuspendGuid); 

SendEmail("my email address", "Suspended Message", Message, true, null, true, 587);

I tried using double quotes, but it does not work.

How can I do this?

Thank you

MattDavey
  • 8,897
  • 3
  • 31
  • 54
DextrousDave
  • 6,603
  • 35
  • 91
  • 134

4 Answers4

2

Either use a @ prefix:

@"Some text ""here""";

or use a backslash to escape the quotes:

"Some text \"here\"";
MattDavey
  • 8,897
  • 3
  • 31
  • 54
Matt
  • 6,787
  • 11
  • 65
  • 112
  • what does the 'some text' refer to? – DextrousDave Mar 07 '13 at 14:12
  • whatever your text is.. in thsi case your html – Matt Mar 07 '13 at 14:13
  • Just stick a @ character in front like this: `string.Format(@"...etc` and see if that works first – Matt Mar 07 '13 at 14:14
  • Both of these are the lesser way, IMO; this scenario is perfect for single quotes. the prefix and escaping just make the string more convoluted. – Grant Thomas Mar 07 '13 at 14:15
  • @GrantThomas I see this as a limitation in C# itself. This kind of task would be a lot nicer if C# supported string interpolation like [Nemerle](http://nemerle.org/wiki/index.php?title=Features#String_interpolation) or [Ruby](http://kconrails.com/2010/12/08/ruby-string-interpolation/) – MattDavey Mar 07 '13 at 15:16
  • @MattDavey How come? There are at least two ways that are _much_ cleaner than either of these. – Grant Thomas Mar 07 '13 at 15:38
  • @GrantThomas in Nemerle I would write `$"
    $emaildetail.Name
    $emaildetail.Surname
    ..."` No need for those nasty placeholders. Granted building up the HTML this way is horrible - far better to use a templating engine.
    – MattDavey Mar 07 '13 at 17:58
1
var Message = string.Format(@"<html><head></head><body><div style=""this does not work!!"">{0}<br/>{1}<br />{2}<br />{3}<br />{4}<br />{5}</body></html>", emaildetail.Name, emaildetail.Surname, emaildetail.CardProvider, emaildetail.CardType, emaildetail.CardNo, emaildetail.SuspendGuid);

You need to escape your string using the @.

See this post

Escape Double Quote

Community
  • 1
  • 1
Sean Barlow
  • 588
  • 6
  • 11
1

Generally in such circumstances it's much less work and more pretty (as much as that can be) to use single quotes within the double quotes of a literal string; of course you can escape the doubles with a backslash, or a prefixed @, but there's very little point, and more typing or confusion:

<html><head></head><body><div style='this will work!!'

However, another crux of your problem might exist even so, as clients may render styles at discretion.

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
  • OH so just single quotes will work just as good? So using single quotes won't make the styling be ignored in any way? – DextrousDave Mar 07 '13 at 14:14
  • Nope, in fact, by the standards most clients go by, unquoted HTML probably still works in most of them. Single quotes will be fine, if a client is going to ignore a style property it's going to do it regardless. – Grant Thomas Mar 07 '13 at 14:16
-1

use \ as an escape character before your quotes...

var Message = string.Format("<html><head></head><body><div style=\"\">") 
Brian P
  • 1,569
  • 13
  • 23