I am using this code to send email
var message = new MailMessage("abc@somedomain.com", "administrator@anotherdomain.com");
message.Subject = "Testing";
message.IsBodyHtml = true;
message.Body = "<html><body>IMAGINE A LOT OF HTML CODING HERE</body></html>";
The problem is I just copied the HTML that I want to send as email and now I have to make the whole HTML code in ONE single line! Otherwise it is saying ";" missing! I mean, now I can't keep on removing spaces and put it ALL in one line! It's too much HTML code that I need to send. What do I do ? :/
[EDIT开发者_JS百科]
Another question: Is there a limit to this message.Body
? Like a limit to how much HTML can be inserted in this?
Dan has given one option - verbatim string literals - but I'd like to suggest that you move the data into a separate HTML file. Embed it as a resource within your assembly, and then you can load it in at execution time.
That way you'll get HTML syntax highlighting, you won't clutter up your code with a lot of data, and you can edit it really easily at any time, without having to worry about things like double quotes (which would need to be doubled within a verbatim string literal, or escaped with a backslash in a regular string literal).
The downside is that it becomes harder to put user data within the HTML - for that, you might want to consider using a templating system; either simply handwritten (html = html.Replace("$user", name)
) or one of the various templating libraries available. Be careful to use HTML escaping where appropriate, of course.
You can use the @
character:
message.Body = @"
<html>
<body>
IMAGINE A LOT OF HTML CODING HERE
</body>
</html>";
This works OK if you have a small HTML markup / want a quick-and-dirty solution. For production code, I recommend you use what Jon Skeet suggests, keeping a separate HTML file.
Answer to your second question :
"The Body property can contain any object whose size does not exceed 4 MB"
From http://msdn.microsoft.com/en-us/library/system.messaging.message.body.aspx
Put the text onto multiple lines?
message.Body = "<html><body>IMAGINE A LOT OF "+
" HTML CODING HERE</body></html>";
精彩评论