I am using html code inside a .cs file to design a template for sending emails. I have added style tag to the <a href>
tag, but it does not take effect when the email is actually sent.
Here is the code:
public static void Email()
{
StringBuilder sb= new StringBuilder();
sb.Append("<a href='#' style='font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;font-size:14px;color:#fff;text-decoration:none;line-height:2em;font-weight:bold;text-align:center;display:inline-block;border-radius:5px;text-transform:capitalize;background-color:#208ed5;margin:0;border-color:#208ed5;border-style:solid;border-width:10px 20px' target='_blank'>");
sb.Append("link</a>");
}
Escape the quotes with backslash:
style=\"font-family:\'Helvetica Neue\',Helvetica,Arial,sans-serif;font-size:14px;color:#fff;text-decoration:none;line-height:2em;font-weight:bold;text-align:center;display:inline-block;border-radius:5px;text-transform:capitalize;background-color:#208ed5;margin:0;border-color:#208ed5;border-style:solid;border-width:10px 20px\"
The problem is with the way you have constructed your HTML string...
In this code
"<a href='#' style='font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;font-size:14px;color:#fff;text-decoration:none;line-height:2em;font-weight:bold;text-align:center;display:inline-block;border-radius:5px;text-transform:capitalize;background-color:#208ed5;margin:0;border-color:#208ed5;border-style:solid;border-width:10px 20px' target='_blank'>"
The style
attribute will stop here style='font-family:'
since the single quotes which opens the style rules is closed... Hence your other rules are not even considered into this style arrtibute.
Solution: You really dont need to wrap the font-family values in quotes.. you can just say style='font-family:Helvetica,Arial,sans-serif;
If at all you have a font-family name with spaces in it then you have to wrap it in quotes.
Edit 1: If at your font-family name contains space you need to wrap them in quotes.. So you have to wrap the Helvetica Neue
in quotes. Add a double quote and escape its meaning using backslash.. Like below..
style='font-family:\"Helvetica Neue\",Helvetica,Arial,sans-serif;
Use double quotes but with escaped characters, like so:
sb.Append("<a href=\"#\" style=\"font-family:\"Helvetica Neue\",Helvetica,Arial,sans-serif;font-size:14px;color:#fff;text-decoration:none;line-height:2em;font-weight:bold;text-align:center;display:inline-block;border-radius:5px;text-transform:capitalize;background-color:#208ed5;margin:0;border-color:#208ed5;border-style:solid;border-width:10px 20px\" target=\"_blank\">");
sb.Append("link</a>");
Or you can always use the old way of concatenating:
string sb = "<a href=\"#\" style=\"font-family:\"Helvetica Neue\",Helvetica,Arial,sans-serif;font-size:14px;color:#fff;text-decoration:none;line-height:2em;font-weight:bold;text-align:center;display:inline-block;border-radius:5px;text-transform:capitalize;background-color:#208ed5;margin:0;border-color:#208ed5;border-style:solid;border-width:10px 20px\" target=\"_blank\">";
sb += "link</a>";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With