.netemailmimekitmime-message

How to exclude the signature and quoted parts from email message using MimeKit?


Is it possible to detect which parts of the email message are signature and previous messages quoted to this message for excluding those parts, using the MimeKit?


Solution

  • You can do something like this:

    static string ExcludeQuotedTextAndSignature (string bodyText)
    {
        using (var writer = new StringWriter ()) {
            using (var reader = new StringReader (bodyText)) {
                string line;
    
                while ((line = reader.ReadLine ()) != null) {
                    if (line.Length > 0 && line[0] == '>') {
                        // This line is a quoted line, ignore it.
                        continue;
                    }
    
                    if (line.Equals ("-- ", StringComparison.Ordinal)) {
                        // This is the start of the signature
                        break;
                    }
    
                    writer.WriteLine (line);
                }
            }
    
            return writer.ToString ();
        }
    }