简体   繁体   English

如何通过Mimekit / Mailkit发送HTML消息

[英]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.. 我有一个例外,如果正文是空的,它会抛出一个参数。

Using a BodyBuilder like you are doing is probably the easiest way. 像你一样使用BodyBuilder可能是最简单的方法。

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 MimeKit文档 - 创建消息

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 ." “TextPart是具有文本媒体类型的叶节点MIME部分.TextPart构造函数的第一个参数指定媒体子类型: plainhtmlenrichedrtfxml 。”

如果你想严格要求,另外一个选择;

msg.Body = new TextPart(MimeKit.Text.TextFormat.Html) { Text = "<b>html content</b>" };
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. 在某些邮件ISP中,您应始终按值设置bodyBuilder.TextBody。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM