Cannot send even the simplest HTML Email in C# -

I'm struggling, to get HTML Emails to work. I started blaming gmail smtp, since I was using that, while developing, but have now posted code to my webhost, and I still can only send plain text. I'm really sure that it's a really stupid small thing that I'm missing, but I can't cut away more code. I can't make it simpler, and it's still showing up as plain text in my outlook.

Here's my source:

  var EmailMessage = new MailMessage();
  EmailMessage.IsBodyHtml = true;
  EmailMessage.Body = @"test...<br><b>Bold text</b>";

  using (var smtpClient = new SmtpClient("smtp.123.com")) {
    smtpClient.UseDefaultCredentials = true;
    smtpClient.Send("[email protected]", "[email protected]", "Account verification", EmailMessage.Body);
  }

This results in a plain text email with this content: test...<br><b>Bold text</b>

EDIT:
Changed

  var EmailMessage = new MailMessage();
  EmailMessage.IsBodyHtml = true;
  EmailMessage.Body = @"<!DOCTYPE html>
<html>
<head>
    <meta charset=""utf-8"" />
    <title></title>
</head>
<body>
Test<br/>
<b>Bold text</b>
</body>
</html>";

  using (var smtpClient = new SmtpClient("smtp.123.com")) {
    smtpClient.UseDefaultCredentials = true;
    smtpClient.Send("[email protected]", "[email protected]", "Account verification", 
         EmailMessage.Body);
  }

Still ends up as plain text :(

Upvotes: 1

Views: 2536

Answers (1)

I ended up solving it, and it was a stupid small thing. I sent the EmailMessage.Body instead of instead of Just EmailMessage

Here is my code now:

var EmailMessage = new MailMessage();
EmailMessage.IsBodyHtml = true;
EmailMessage.Body = @"test...<br><b>Bold text</b>";

using (var smtpClient = new SmtpClient("smtp.123.com")) {
    smtpClient.UseDefaultCredentials = true;
    smtpClient.Send("[email protected]", "[email protected]", "Account verification", 
         EmailMessage);
}

Upvotes: 2

Related Questions