1

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 :(

8
  • Your body string is not valid html. HTML need proper tags and does not start with "test..." Commented Apr 13, 2020 at 16:18
  • I have tried <html>test...<br><b>Bold text</b></html> <html><body>test...<br><b>Bold text</b></body></html> <html><head></head><body>test...<br><b>Bold text</b></body></html> Commented Apr 13, 2020 at 16:24
  • Still not valid. From VS menu : Project : Add New Item : HTML Page. Then paste your html and get the errors out before using. Commented Apr 13, 2020 at 16:33
  • Still the same, only plain text, with all html tags visible. Commented Apr 13, 2020 at 19:31
  • Are you getting exactly what you sent? If so then it just how you are viewing the results. HTML is text. So you just need to put text into a viewer that can display html. Commented Apr 13, 2020 at 21:01

1 Answer 1

2

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I was doing the same thing! Haha what a ridiculous bug.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.