Serenity
Serenity

Reputation: 5098

Sending email to a user

I am using this code to send email

var message = new MailMessage("[email protected]", "[email protected]");
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] Another question: Is there a limit to this message.Body? Like a limit to how much HTML can be inserted in this?

Upvotes: 2

Views: 194

Answers (4)

Wilko de Zeeuw
Wilko de Zeeuw

Reputation: 21

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

Upvotes: 2

Dan Dumitru
Dan Dumitru

Reputation: 5333

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.

Upvotes: 6

Jon Skeet
Jon Skeet

Reputation: 1503599

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.

Upvotes: 10

Brissles
Brissles

Reputation: 3891

Put the text onto multiple lines?

message.Body = "<html><body>IMAGINE A LOT OF "+
                " HTML CODING HERE</body></html>";

Upvotes: 1

Related Questions