Generate HTML Email with .NET

Asked by At

I have been struggling for two days to generate a simple HTML email with a .NET application. There are several answered questions on this topic already, and in each case the syntax is relatively simple. It still took me over a day to get it to work. However, the solution raises more questions. I have looked to the Microsoft documentation and found nothing there.

This syntax works and will generate an HTML email:

MailMessage message = new MailMessage();
message.Subject = "Test";
message.From = new MailAddress("[email protected]");
message.To.Add("[email protected]");
message.Body = "<strong>This is a test</strong>";
message.IsBodyHtml = true;

However, if I use the Send method of the SmtpClient object that has a signature with four parameters, it will NOT generate an HTML email:

smtpClient.Send("[email protected]", "[email protected]", 
    message.Subject, message.Body);

Can anyone explain why this is happening. Is it documented, or is it a known "issue"? IsBodyHtml was set to true in both cases.

3 Answers

Jack Marchetti On Best Solutions

Because there's no designation of IsBodyHTML set to true when using the Send function the way you've done. I think IsBodyHTML is within the MailMessage object.

I'm not entirely sure what IsBodyHTML sets in the body but it may create fully formed HTML (<html><head><body>etc....) so maybe give that a try.

ADyson On

I think the explanation for this is fairly simple.

In the first case, you explicitly set isBodyHtml to true in your MailMessage object. You then pass the MailMessage object to the Send() method. Therefore it formats the email as HTML, per your instructions.

In the second case, you have no means by which to tell the system what format the email should be. Plain text is generally the default format, and therefore in the absence of any other instructions, I expect that's what it will use.

N.B. You claim isBodyHTML was set to true in both cases, but this doesn't make any sense. In the second case you are not passing a MailMessage object to the method...the IsBodyHtml property belongs to the MailMessage. So how else and where could you set such a value?

In this second scenario the only values the Send() method gets are four strings (from, to, subject and body). It does not receive any other information about the email. The isBodyHTML property is never used because it is part of the un-used MailMessage.

Ralf On

The overload that is not working for you internally looks more or less like this

MailMessage mailMessage = new MailMessage(from, recipients, subject, body);

It creates a MailMessage instance does not set IsBodyHtml and calls the other overload. IsBodyHtml controls the MediaType used for the MimePart of the Body in your Mail and if IsBodyHtml is not set PlainText will be used instead of a Html MimePart. Setting IsBodyHtml does not change the content of the Body Property in any way so you have to use the overload with MailMessage so you can explicitly set IsBodyHtml.