• <tfoot id='dUZFx'></tfoot>
    <legend id='dUZFx'><style id='dUZFx'><dir id='dUZFx'><q id='dUZFx'></q></dir></style></legend>
        <bdo id='dUZFx'></bdo><ul id='dUZFx'></ul>

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

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

        使用 MailKit 向 SpecifiedPickupDirectory 发送电子邮件

        时间:2023-10-05

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

            <legend id='dfvyA'><style id='dfvyA'><dir id='dfvyA'><q id='dfvyA'></q></dir></style></legend>
          1. <small id='dfvyA'></small><noframes id='dfvyA'>

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

                  问题描述

                  到目前为止,我一直在使用 SmtpClient 和 ASP.NET MVC 5.为了在本地系统上测试电子邮件发送功能,我使用的是 client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;

                  I was using SmtpClient till now with ASP.NET MVC 5. For testing email send functionality on local system, I was using client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;

                  现在,我想在目前还没有实现 SmtpClient 类的 ASP.NET Core 中做同样的事情.对此的所有搜索都以 MailKit 结束.我使用了他们的发送邮件代码,该代码在 gmail 上运行良好.

                  Now, I want to do the same things in ASP.NET Core which does not have SmtpClient class implemented till now. All search for this ended up on MailKit. I have used their send mail code which is working fine with gmail.

                  我不想每次都发送测试邮件,我的项目中可能有很多场景需要发送邮件.如何通过 MailKit 使用本地电子邮件发送功能.任何链接或少量源代码都会有所帮助.谢谢

                  I do not want to send testing emails each time and there may be a lot of scenarios in my project where I need to send email. How can I use the local email sending functionality with MailKit. Any links or little source code will help. Thanks

                  推荐答案

                  我不确定 SmtpDeliveryMethod.SpecifiedPickupDirectory 的工作原理和具体作用的详细信息,但我怀疑它可能只需将邮件保存在本地 Exchange 服务器定期检查要发送的邮件的目录中即可.

                  I'm not sure on the finer details of how SmtpDeliveryMethod.SpecifiedPickupDirectory works and what it does exactly, but I suspect it might just save the message in a directory where the local Exchange server periodically checks for mail to send out.

                  假设是这样,你可以这样做:

                  Assuming that's the case, you could do something like this:

                  public static void SaveToPickupDirectory (MimeMessage message, string pickupDirectory)
                  {
                      do {
                          // Generate a random file name to save the message to.
                          var path = Path.Combine (pickupDirectory, Guid.NewGuid ().ToString () + ".eml");
                          Stream stream;
                  
                          try {
                              // Attempt to create the new file.
                              stream = File.Open (path, FileMode.CreateNew);
                          } catch (IOException) {
                              // If the file already exists, try again with a new Guid.
                              if (File.Exists (path))
                                  continue;
                  
                              // Otherwise, fail immediately since it probably means that there is
                              // no graceful way to recover from this error.
                              throw;
                          }
                  
                          try {
                              using (stream) {
                                  // IIS pickup directories expect the message to be "byte-stuffed"
                                  // which means that lines beginning with "." need to be escaped
                                  // by adding an extra "." to the beginning of the line.
                                  //
                                  // Use an SmtpDataFilter "byte-stuff" the message as it is written
                                  // to the file stream. This is the same process that an SmtpClient
                                  // would use when sending the message in a `DATA` command.
                                  using (var filtered = new FilteredStream (stream)) {
                                      filtered.Add (new SmtpDataFilter ());
                  
                                      // Make sure to write the message in DOS (<CR><LF>) format.
                                      var options = FormatOptions.Default.Clone ();
                                      options.NewLineFormat = NewLineFormat.Dos;
                  
                                      message.WriteTo (options, filtered);
                                      filtered.Flush ();
                                      return;
                                  }
                              }
                          } catch {
                              // An exception here probably means that the disk is full.
                              //
                              // Delete the file that was created above so that incomplete files are not
                              // left behind for IIS to send accidentally.
                              File.Delete (path);
                              throw;
                          }
                      } while (true);
                  }
                  

                  上面的代码片段使用 Guid.NewGuid () 作为生成临时文件名的一种方式,但是您可以使用任何您想要的方法(例如,您也可以选择使用 message.MessageId + .eml").

                  The above code snippet uses Guid.NewGuid () as a way of generating a temporary filename, but you can use whatever method you want (e.g. you could also opt to use message.MessageId + ".eml").

                  基于微软的 参考源,当使用SpecifiedPickupDirectory时,其实也使用Guid.NewGuid().ToString()+.eml",所以大概是这样去.

                  Based on Microsoft's referencesource, when SpecifiedPickupDirectory is used, they actually also use Guid.NewGuid ().ToString () + ".eml", so that's probably the way to go.

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

                  上一篇:.NET 的 SMTP 和 IMAP 服务器库 下一篇:验证 C# 中是否存在电子邮件地址

                  相关文章

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

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