59
BodyBuilder bodyBuilder = new BodyBuilder();
messageContent.Body = "<b>This is a test mail</b>";
bodyBuilder.HtmlBody = messageContent.Body;

I tried to embed my body to a bodybuilder but when I received the email, it returned an empty body. I have an exception that would throw an argument if the body is empty..

4 Answers 4

96

Using a BodyBuilder like you are doing is probably the easiest way.

var bodyBuilder = new BodyBuilder();
bodyBuilder.HtmlBody = "<b>This is some html text</b>";
bodyBuilder.TextBody = "This is some plain text";

message.Body = bodyBuilder.ToMessageBody();

client.Send(message);
6
  • If you only include HtmlBody and not TextBody, is there a risk that it could be identified as spam for only including one and not both? Aug 3, 2017 at 18:34
  • That probably depends on the recipients mail software
    – jstedfast
    Aug 3, 2017 at 19:00
  • 2
    More real world samples ?
    – Kiquenet
    Feb 6, 2018 at 11:34
  • Also, if the mail still shows in plain text, it could be due to the recipients mail software detects it as spam and converts it in plain text. E.g. Outlook does it.
    – Himanshu
    May 13, 2022 at 11:05
  • @jstedfast How can we add multiple images to the body, not as an attachment
    – Ali
    Jul 31, 2022 at 2:22
36

MimeKit Documentation - Creating Messages

var message = new MimeMessage();    
message.Body = new TextPart ("html") { Text = "<b>Test Message</b>" };

"A TextPart is a leaf-node MIME part with a text media-type. The first argument to the TextPart constructor specifies the media-subtype: plain, html, enriched, rtf, and xml."

1
  • I was having a problem getting other examples to work. This one went perfectly. Thank you!
    – RiverNet
    Jun 10, 2021 at 17:24
22

One other option here if you want to be strict;

msg.Body = new TextPart(MimeKit.Text.TextFormat.Html) { Text = "<b>html content</b>" };
2
var bodyBuilder = new BodyBuilder();
bodyBuilder.HtmlBody = body;
bodyBuilder.TextBody = "-";

message.Body = bodyBuilder.ToMessageBody();

In some mail ISP, you should always set bodyBuilder.TextBody by value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.