2

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("user@athisaddress.com");
message.To.Add("me@myorg.com");
message.Body = "<strong>This is a test</strong>";
message.IsBodyHtml = true;
smtpClient.Send(message);

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("user@athisaddress.com", "me@myorg.com", 
    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.

IrishChieftain
  • 15,108
  • 7
  • 50
  • 91
  • 1
    N.B. A minor bit of pedantry: None of this has anything to do with ASP.NET or WebForms specifically...the smtp mail libraries are available to any kind of .NET application – ADyson Apr 25 '19 at 15:56
  • I had that set in both case, I will include it in the question. You can test this yourself - it's baffling! – IrishChieftain Apr 25 '19 at 15:57
  • ADyson, good point. If can want please adjust the tags. – IrishChieftain Apr 25 '19 at 15:58
  • 1
    I have adjusted the tags. You might want to adjust the wording in the title and body too – ADyson Apr 25 '19 at 15:59
  • Anyway...I don't find this scenario especially baffling. In the first case, you explicitly set "isBodyHtml" to true. 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. – ADyson Apr 25 '19 at 16:01
  • I'll update the wording. In both cases IsBodyHtml was set to true. – IrishChieftain Apr 25 '19 at 16:02
  • 1
    How? 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 did you set such a value? In this 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. – ADyson Apr 25 '19 at 16:02
  • Have you tried sending plain text with the second method? It might not be possible to send html using it as there is no way to set `IsBodyHtml` in this case. – colinD Apr 25 '19 at 16:08
  • Have you tried sending fully formed HTML? var htmlMessage = "Testing"; smtpClient.Send("user@athisaddress.com", "me@myorg.com", "Subject", htmlMessage); – Jack Marchetti Apr 25 '19 at 16:20
  • No Jack, but I tried "This is a test" as the 4th param and it sent as plain text. I think you nailed it in your answer. – IrishChieftain Apr 25 '19 at 16:24

3 Answers3

3

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.

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • I've updated the question. The version of the code I posted was for simplicity. In actuality, I passed message object properties for the 3rd and 4th params. – IrishChieftain Apr 25 '19 at 16:17
  • 1
    But you did not pass the message object. You only passed the message's subject and body, both strings. The rest of the message object is in no way made available to the Send method, by you passing specific members of that object. Your edit did nothing to pass to the Send method the .isBodyHTML member of the message object. –  Apr 25 '19 at 16:22
  • Thanks for your help, upvoted. I gave the answer to Jack as he was first in ;-) – IrishChieftain Apr 25 '19 at 16:28
2

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.

Jack Marchetti
  • 15,536
  • 14
  • 81
  • 117
  • 1
    I think you nailed it. I thought by using message properties for the subject and body params that .NET would be aware of the other properties of the message object. Obviously this is not so. – IrishChieftain Apr 25 '19 at 16:26
  • 1
    If passing members of an object gave access to the object, that would violate basic principles of object oriented programming languages. –  Apr 25 '19 at 16:29
  • AgapwIesu, or maybe more of a scope issue since I created and own the object in the calling environment? – IrishChieftain Apr 25 '19 at 16:32
  • You can set IsBodyHtml = True many times, and write it in Bold, but afterwards you never use it. You are just sending 4 strings to the constructor, and none of them is the IsBodyHtml property. As ADyson mentioned in other comments, the default behavior for the constructor which receives 4 strings, is to build a plain text email message and send it. – Jortx Apr 26 '19 at 08:13
1

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

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

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.

Ralf
  • 1,216
  • 10
  • 20