回复Mailkit中的邮件

编程入门 行业动态 更新时间:2024-10-26 04:20:57
本文介绍了回复Mailkit中的邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在为我的项目使用Mailkit库(Imap).

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

我可以通过SmtpClient轻松发送新消息.

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感谢您的精彩:)

@jstedfast thanks for the wonderful one :)

推荐答案

回复邮件非常简单.在大多数情况下,您将以与创建其他任何消息相同的方式来创建回复消息.仅有一些细微差别:

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:

  • 在回复消息中,如果要回复的消息中还没有前缀Subject头,请在Subject标头前面加上"Re: "(换句话说,如果您回复的是消息) "Re: party tomorrow night!"的Subject,您将 not 加上另一个"Re: "前缀).
  • 您需要将回复消息的In-Reply-To标头设置为原始消息中Message-Id标头的值.
  • 您将要复制原始消息的References标头到回复消息的References标头,然后附加原始消息的Message-Id标头.
  • 您可能希望在回复中引用"原始消息的文本.
  • 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: ").
  • 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.
  • 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.
  • 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; }

    注意:此代码假定message.TextBody不为null.很有可能发生这种情况,尽管这种可能性很小(这意味着该消息不包含text/plain正文).

    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).

    更多推荐

    回复Mailkit中的邮件

    本文发布于:2023-11-06 07:54:22,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1563122.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:邮件   Mailkit

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!