How to reply with quoted HTML message?

130 Views Asked by At

I'm reading the MailKit FAQ on how to reply a HTML message with the quoted original message.

It doesn't explain how you can set both your message and the ReplyVisitor messages to the body. Any idea?

I ran the example code successfully, but since I don't know where to set my HTML message, the email is blank with only the quoted message.

public static MimeMessage Reply (MimeMessage message, MailboxAddress from, bool replyToAll)
{
    var visitor = new ReplyVisitor ();
    var reply = new MimeMessage ();

    reply.From.Add (from);

    // reply to the sender of the message
    if (message.ReplyTo.Count > 0) {
        reply.To.AddRange (message.ReplyTo);
    } else if (message.From.Count > 0) {
        reply.To.AddRange (message.From);
    } else if (message.Sender != null) {
        reply.To.Add (message.Sender);
    }

    if (replyToAll) {
        // include all of the other original recipients (removing ourselves from the list)
        reply.To.AddRange (message.To.Mailboxes.Where (x => x.Address != from.Address));
        reply.Cc.AddRange (message.Cc.Mailboxes.Where (x => x.Address != from.Address));
    }

    // set the reply subject
    if (!message.Subject.StartsWith ("Re:", StringComparison.OrdinalIgnoreCase))
        reply.Subject = "Re: " + message.Subject;
    else
        reply.Subject = message.Subject;

    // construct the In-Reply-To and References headers
    if (!string.IsNullOrEmpty (message.MessageId)) {
        reply.InReplyTo = message.MessageId;
        foreach (var id in message.References)
            reply.References.Add (id);
        reply.References.Add (message.MessageId);
    }

    visitor.Visit (message);

    // How do I set my own text if this is overridden by the visitor body??
    // var myTextBody = new BodyBuilder();
    // myTextBody.HtmlBody = "<div>HELLO WORLD</div>";
    // reply.Body = myTextBody.ToMessageBody();

    reply.Body = visitor.Body ?? new TextPart ("plain") { Text = ReplyVisitor.GetOnDateSenderWrote (message) + Environment.NewLine };

    return reply;
}
1

There are 1 best solutions below

0
jstedfast On

The example reply code is only meant to produce the text that you might populate a "New Message" editor window with.

If you want to do this programmatically, you would obviously just prepend it to the result of ReplyVisitor.GetOnDateSenderWrote (message).