<legend id='NEEfy'><style id='NEEfy'><dir id='NEEfy'><q id='NEEfy'></q></dir></style></legend>
    1. <i id='NEEfy'><tr id='NEEfy'><dt id='NEEfy'><q id='NEEfy'><span id='NEEfy'><b id='NEEfy'><form id='NEEfy'><ins id='NEEfy'></ins><ul id='NEEfy'></ul><sub id='NEEfy'></sub></form><legend id='NEEfy'></legend><bdo id='NEEfy'><pre id='NEEfy'><center id='NEEfy'></center></pre></bdo></b><th id='NEEfy'></th></span></q></dt></tr></i><div id='NEEfy'><tfoot id='NEEfy'></tfoot><dl id='NEEfy'><fieldset id='NEEfy'></fieldset></dl></div>

        <tfoot id='NEEfy'></tfoot>

        <small id='NEEfy'></small><noframes id='NEEfy'>

          <bdo id='NEEfy'></bdo><ul id='NEEfy'></ul>
      1. 使用 MailKit (C#) 转发电子邮件

        Forward email using MailKit (C#)(使用 MailKit (C#) 转发电子邮件)
          <bdo id='kspa5'></bdo><ul id='kspa5'></ul>

                <small id='kspa5'></small><noframes id='kspa5'>

              • <legend id='kspa5'><style id='kspa5'><dir id='kspa5'><q id='kspa5'></q></dir></style></legend>
                <i id='kspa5'><tr id='kspa5'><dt id='kspa5'><q id='kspa5'><span id='kspa5'><b id='kspa5'><form id='kspa5'><ins id='kspa5'></ins><ul id='kspa5'></ul><sub id='kspa5'></sub></form><legend id='kspa5'></legend><bdo id='kspa5'><pre id='kspa5'><center id='kspa5'></center></pre></bdo></b><th id='kspa5'></th></span></q></dt></tr></i><div id='kspa5'><tfoot id='kspa5'></tfoot><dl id='kspa5'><fieldset id='kspa5'></fieldset></dl></div>

                  <tfoot id='kspa5'></tfoot>
                    <tbody id='kspa5'></tbody>
                  本文介绍了使用 MailKit (C#) 转发电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试使用 MailKit 访问 IMAP 帐户(由 jstedfast 创建)

                  I'm trying to access to an IMAP account using MailKit (created by jstedfast)

                  我设法下载了消息(作为 MimeMessage),但有时我需要将其转发"到另一个帐户.

                  I manage to download the message (as a MimeMessage), and at some point I need to "forward" it to another account.

                  最好的方法是如何做到这一点,以保留原始电子邮件的所有信息(地址、标题、正文内容等).

                  How would be the best way to do it, in order to preserve all the information of the original email (adresses, headers, body content, etc).

                  谢谢!

                  推荐答案

                  不同的人说前进"的意思不同,所以我想我会针对我能想到的不同含义提供答案.

                  Different people mean different things when they say "forward", so I guess I'll provide answers to the different meanings that I can think of.

                  p>

                  1.转发(重新发送)消息而不做任何更改.

                  没有变化",我的字面意思是原始消息数据完全没有变化.这样做的方法是:

                  By "no changes", I literally mean no changes at all to the raw message data. The way to do this is to do:

                  var message = FetchMessageFromImapServer ();
                  
                  using (var client = new SmtpClient ()) {
                      client.Connect ("smtp.example.com", 465, true);
                      client.Authenticate ("username", "password");
                  
                      var sender = new MailboxAddress ("My Name", "username@example.com");
                      var recipients = new [] { new MailboxAddress ("John Smith", "john@smith.com") };
                  
                      // This version of the Send() method uses the supplied sender and
                      // recipients rather than getting them from the message's headers.
                      client.Send (message, sender, recipients);
                  
                      client.Disconnect (true);
                  }
                  

                  注意:GMail、Hotmail/Office365 等一些网络邮件提供商可能不允许您使用这种方法,因为他们可能认为这是电子邮件欺骗.这些邮件主机可能会将 From 标头替换为您帐户的名称/电子邮件地址,因此请注意此潜在问题.

                  Note: Some webmail providers such as GMail, Hotmail/Office365, etc. might not allow you to use this approach as they might consider it to be email spoofing. These mail hosts might replace the From header with the name/email address of your account, so be aware of this potential issue.

                  不过,通常人们并不是指这种类型的转发".如果他们想重新发送,通常他们会使用下一种重新发送的方法.

                  Usually people don't mean this type of "forwarding", though. If they want to resend, usually they'll use the next method of resending.

                  <强>2.以自动过滤器可能发送的方式转发(重新发送)消息.

                  var message = FetchMessageFromImapServer ();
                  
                  // clear the Resent-* headers in case this message has already been Resent...
                  message.ResentSender = null;
                  message.ResentFrom.Clear ();
                  message.ResentReplyTo.Clear ();
                  message.ResentTo.Clear ();
                  message.ResentCc.Clear ();
                  message.ResentBcc.Clear ();
                  
                  // now add our own Resent-* headers...
                  message.ResentFrom.Add (new MailboxAddress ("MyName", "username@example.com"));
                  message.ResentReplyTo.Add (new MailboxAddress ("MyName", "username@example.com"));
                  message.ResentTo.Add (new MailboxAddress ("John Smith", "john@smith.com"));
                  message.ResentMessageId = MimeUtils.GenerateMessageId ();
                  message.ResentDate = DateTimeOffset.Now;
                  
                  using (var client = new SmtpClient ()) {
                      client.Connect ("smtp.example.com", 465, true);
                      client.Authenticate ("username", "password");
                  
                      // The Send() method will use the Resent-From/To/Cc/Bcc headers if
                      // they are present.
                      client.Send (message);
                  
                      client.Disconnect (true);
                  }
                  

                  3.通过将邮件(整体)附加到新邮件来转发邮件,某些电子邮件客户端可能会这样做.

                  var messageToForward = FetchMessageFromImapServer ();
                  
                  // construct a new message
                  var message = new MimeMessage ();
                  message.From.Add (new MailboxAddress ("MyName", "username@example.com"));
                  message.ReplyTo.Add (new MailboxAddress ("MyName", "username@example.com"));
                  message.To.Add (new MailboxAddress ("John Smith", "john@smith.com"));
                  message.Subject = "FWD: " + messageToForward.Subject;
                  
                  // now to create our body...
                  var builder = new BodyBuilder ();
                  builder.TextBody = "Hey John,
                  
                  Here's that message I was telling you about...
                  ";
                  builder.Attachments.Add (new MessagePart { Message = messageToForward });
                  
                  message.Body = builder.ToMessageBody ();
                  
                  using (var client = new SmtpClient ()) {
                      client.Connect ("smtp.example.com", 465, true);
                      client.Authenticate ("username", "password");
                  
                      client.Send (message);
                  
                      client.Disconnect (true);
                  }
                  

                  4.转发消息inline";许多其他电子邮件客户端的做法.

                  var messageToForward = FetchMessageFromImapServer ();
                  
                  // construct a new message
                  var message = new MimeMessage ();
                  message.From.Add (new MailboxAddress ("MyName", "username@example.com"));
                  message.ReplyTo.Add (new MailboxAddress ("MyName", "username@example.com"));
                  message.To.Add (new MailboxAddress ("John Smith", "john@smith.com"));
                  message.Subject = "FWD: " + messageToForward.Subject;
                  
                  // now to create our body...
                  var builder = new BodyBuilder ();
                  
                  using (var writer = new StringWriter ()) {
                      var sender = messageToForward.From.Mailboxes.FirstOrDefault () ?? messageToForward.Sender;
                      var senderName = sender != null ? (!string.IsNullOrEmpty (sender.Name) ? sender.Name : sender.Address) : "someone";
                      var text = messageToForward.TextBody ?? string.Empty;
                  
                      writer.WriteLine ("On {0}, {1} wrote:", messageToForward.Date, senderName);
                  
                      using (var reader = new StringReader (text)) {
                          string line;
                  
                          while ((line = reader.ReadLine ()) != null) {
                              writer.Write ("> ");
                              writer.WriteLine (line);
                          }
                      }
                  
                      builder.TextBody = writer.ToString ();
                  }
                  
                  message.Body = builder.ToMessageBody ();
                  
                  using (var client = new SmtpClient ()) {
                      client.Connect ("smtp.example.com", 465, true);
                      client.Authenticate ("username", "password");
                  
                      client.Send (message);
                  
                      client.Disconnect (true);
                  }
                  

                  这篇关于使用 MailKit (C#) 转发电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Populate ListBox with a IEnumrable on another thread (winforms)(在另一个线程(winforms)上使用 IEnumrable 填充 ListBox)
                  listbox selected item give me quot; System.Data.DataRowViewquot; , C# winforms(列表框选择的项目给我quot;System.Data.DataRowView, C# Winforms)
                  Cannot remove items from ListBox(无法从列表框中删除项目)
                  Preventing ListBox scrolling to top when updated(更新时防止列表框滚动到顶部)
                  Drag and drop from list to canvas on windows phone with MVVM(使用 MVVM 在 Windows 手机上从列表拖放到画布)
                  Deselection on a WPF listbox with extended selection mode(具有扩展选择模式的 WPF 列表框上的取消选择)
                      <bdo id='VZfpI'></bdo><ul id='VZfpI'></ul>
                      <legend id='VZfpI'><style id='VZfpI'><dir id='VZfpI'><q id='VZfpI'></q></dir></style></legend>

                      • <i id='VZfpI'><tr id='VZfpI'><dt id='VZfpI'><q id='VZfpI'><span id='VZfpI'><b id='VZfpI'><form id='VZfpI'><ins id='VZfpI'></ins><ul id='VZfpI'></ul><sub id='VZfpI'></sub></form><legend id='VZfpI'></legend><bdo id='VZfpI'><pre id='VZfpI'><center id='VZfpI'></center></pre></bdo></b><th id='VZfpI'></th></span></q></dt></tr></i><div id='VZfpI'><tfoot id='VZfpI'></tfoot><dl id='VZfpI'><fieldset id='VZfpI'></fieldset></dl></div>

                        <tfoot id='VZfpI'></tfoot>
                            <tbody id='VZfpI'></tbody>

                          • <small id='VZfpI'></small><noframes id='VZfpI'>