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..
MailKit is an Open Source cross-platform .NET mail-client library that is based on MimeKit. It is optimized for mobile devices and supports most of the modern protocols. I was looking to build send email functionality as a Service using C# .NET Core 3.1 recently using MailKit.
Installing MailKit The first thing we need to do is to install the MailKit NuGet package, which can be done in the following ways: Using the .NET Core CLI, with the dotnet add package MailKit console command from the ASP.NET Core project's root folder.
33 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.
A message structure consists of header fields and a body. The body of the message can be plain text or a tree of MIME entities. Let’s now define the SMTP MailKit client. We shall pass the MimeMessage object to the Send () API as below, Complete API as an example demonstrating the usage as below.
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);
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."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With