Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending email to a user

Tags:

c#

asp.net

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?

like image 861
Serenity Avatar asked Nov 27 '25 15:11

Serenity


2 Answers

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.

like image 177
Jon Skeet Avatar answered Nov 29 '25 05:11

Jon Skeet


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.

like image 44
Dan Dumitru Avatar answered Nov 29 '25 03:11

Dan Dumitru



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!