Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send HTML message via Mimekit/Mailkit

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

like image 671
astropringles Avatar asked Dec 15 '16 09:12

astropringles


People also ask

What is MailKit and how to use it?

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.

How do I install MailKit in ASP NET?

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.

How do I create a text message in MimeKit?

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.

What is mimemessage object in SMTP MailKit?

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.


2 Answers

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); 
like image 65
jstedfast Avatar answered Sep 17 '22 16:09

jstedfast


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

like image 42
Stephen C Avatar answered Sep 20 '22 16:09

Stephen C