Reply to a Mail in Mailkit

耗尽温柔 提交于 2019-12-08 15:46:32

问题


i'm using Mailkit library (Imap) for my project.

I can comfortably send a new message by SmtpClient.

currently I'm digging about how to reply to a particular mail. and is it possible to add more recipients to that reply mail ?

@jstedfast thanks for the wonderful one :)


回答1:


Replying to a message is fairly simple. For the most part, you'd just create the reply message the same way you'd create any other message. There are only a few slight differences:

  1. In the reply message, you'll want to prefix the Subject header with "Re: " if the prefix doesn't already exist in the message you are replying to (in other words, if you are replying to a message with a Subject of "Re: party tomorrow night!", you would not prefix it with another "Re: ").
  2. You will want to set the reply message's In-Reply-To header to the value of the Message-Id header in the original message.
  3. You will want to copy the original message's References header into the reply message's References header and then append the original message's Message-Id header.
  4. You will probably want to "quote" the original message's text in the reply.

If this logic were to be expressed in code, it might look something like this:

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

    // 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 - TODO: remove ourselves from these lists
        reply.To.AddRange (message.To);
        reply.Cc.AddRange (message.Cc);
    }

    // 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);
    }

    // quote the original message text
    using (var quoted = new StringWriter ()) {
        var sender = message.Sender ?? message.From.Mailboxes.FirstOrDefault ();

        quoted.WriteLine ("On {0}, {1} wrote:", message.Date.ToString ("f"), !string.IsNullOrEmpty (sender.Name) ? sender.Name : sender.Address);
        using (var reader = new StringReader (message.TextBody)) {
            string line;

            while ((line = reader.ReadLine ()) != null) {
                quoted.Write ("> ");
                quoted.WriteLine (line);
            }
        }

        reply.Body = new TextPart ("plain") {
            Text = quoted.ToString ()
        };
    }

    return reply;
}

Note: This code assumes that message.TextBody is not null. It's possible, although fairly unlikely, that this could happen (meaning that the message does not contain a text/plain body).



来源:https://stackoverflow.com/questions/35102698/reply-to-a-mail-in-mailkit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!