【问题标题】:MailKit Identify that message is html or plain textMailKit 识别消息是 html 还是纯文本
【发布时间】:2020-05-05 09:32:43
【问题描述】:

我是 C# 的新手。关于使用 Mailkit 库发送邮件,我有一个非常基本的问题。 我有一个发送电子邮件的方法非常好。请在下面查看我的方法。

public void SendMessage(string from, string to, string subject, string body)

现在我的问题是我需要识别即将到来的正文是纯文本、富文本还是 html 文本。

之前我以纯文本的形式完成,所以对我来说很容易,如下所示

 var bodyMessage = new TextPart("plain")
            {
                Text = body
            };
            message.Body = bodyMessage;

但是我们如何识别即将到来的正文消息是普通的、丰富的还是 html 呢? 或者我需要在 if 和 else 条件下创建 Text 部分。 一个简短的例子将非常有帮助。 提前致谢。

【问题讨论】:

  • 谁调用了SendMessage 方法?绝对正确地检测 html 并非易事。
  • 在我知道的大多数 Mailclients 中,没有我知道的自动检测正文类型。我有一个新消息的“默认”设置,也许还有一个“覆盖”来切换单个消息的类型。

标签: c# email mailkit document-body mimekit


【解决方案1】:

把你的方法改成这样:

public enum TextFormat {
    PlainText,
    Html,
    RichText
}

public void SendMessage(string from, string to, string subject, string body, TextFormat format)

现在您可以告诉该方法它是什么类型的文本,这样您就不必猜测了。

【讨论】:

  • @All :感谢您的回复。我已经编写了自己的方法来检测纯文本或 HTML。所以我将此标记为解决。
猜你喜欢
  • 1970-01-01
  • 2012-04-23
  • 2011-01-24
  • 1970-01-01
  • 1970-01-01
  • 2010-09-25
  • 1970-01-01
  • 1970-01-01
  • 2015-07-28
相关资源
最近更新 更多