How to send HTML message via Mimekit/Mailkit

随声附和 提交于 2019-12-18 01:32:47

问题


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


回答1:


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);



回答2:


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




回答3:


One other option here if you want to be strict;

msg.Body = new TextPart(MimeKit.Text.TextFormat.Html) { Text = "<b>html content</b>" };



回答4:


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.



来源:https://stackoverflow.com/questions/41160536/how-to-send-html-message-via-mimekit-mailkit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!