Search code examples
c#emailmailkitdocument-bodymimekit

MailKit Identify that message is html or plain text


I am newbie in C#. I have a very basic question regarding the sending mail using Mailkit library. I have a method which send the email that is perfectly fine.Please have a look on my method below.

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

Now my problem is I need to identify that body which are coming is plain text,rich text or html text.

Previously I have done as a plain text so it was easy for me like below you can see

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

but how we identify that coming body message is plain,rich or html ? or I need to create Text part in if and else condition. A short example will be really helpful. Thanks in Advance.


Solution

  • Change your method to this:

    public enum TextFormat {
        PlainText,
        Html,
        RichText
    }
    
    public void SendMessage(string from, string to, string subject, string body, TextFormat format)
    

    Now you can tell the method what kind of text it is so you don't have to guess.