Search code examples
c#.netemailhtml-emailsmtpclient

Generate HTML Email with .NET


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;
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("[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.


Solution

  • 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.